0% found this document useful (0 votes)
33 views

Assignment1 - 94089

Uploaded by

amrsalatif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Assignment1 - 94089

Uploaded by

amrsalatif
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Advanced Database DBII CS471 (SQL/PLSQL)

Assignment one ch1 .. ch3


Name : Mariam Medhat Id : 94089
Chapter one:
1. Initiate an iSQL*Plus session using the user ID and password
that are provided by the instructor.

2. iSQL*Plus commands access the database.


True/False

3. The following SELECT statement executes successfully:


SELECT last_name, job_id, salary AS Sal FROM employees;
True/False

4. The following SELECT statement executes successfully:


SELECT * FROM job_grades;
True/False

5. There are four coding errors in the following statement.


Can you identify them?
SELECT employee_id, last_name sal x 12 ANNUAL SALARY
FROM employees;
1- Missing comma
2- Incorrect expression for multiply
3- Incorrect alias syntax 'ANNUAL SALARY'
4- Incorrect Keyword for Alias as

SELECT employee_id, last_name, salary * 12 AS "ANNUAL SALARY"


FROM employees;

1
7. You need to determine the structure of the EMPLOYEES table.
 DESCRIBE EMPLOYEES;

9. The HR department needs a query to display all unique job


codes from the EMPLOYEES table.
 SELECT DISTINCT job_code
FROM EMPLOYEES;

10. The HR department wants more descriptive column headings


for its report on employees. Copy the statement from lab_01_07.sql
to the iSQL*Plus text box. Name the column headings Emp #,
Employee, Job, and Hire Date, respectively. Then run your query
again.
 SELECT employee_id AS "Emp #",
employee_name AS "Employee",
job_code AS "Job",
hire_date AS "Hire Date"
FROM EMPLOYEES;

11. The HR department has requested a report of all employees


and their job IDs. Display the last name concatenated with the job
ID (separated by a comma and space) and name the column
Employee and Title.
 SELECT last_name || ', ' || job_id AS "Employee and Title"
FROM EMPLOYEES;

2
Chapter two:
The HR department needs your assistance with creating some
queries.
1. Due to budget issues, the HR department needs a report that
displays the last name and salary of employees who earn more
than $12,000. Place your SQL statement in a text file named
lab_02_01.sql. Run your query.
 SELECT last_name, salary
FROM EMPLOYEES
WHERE salary > 12000;

2. Create a report that displays the last name and department


number for employee number 176.

 SELECT last_name, department_id


FROM EMPLOYEES
WHERE employee_id = 176;

3. The HR departments needs to find high-salary and low-salary


employees. Modify lab_02_01.sql to display the last name and
salary for any employee whose salary is not in the range of
$5,000 to $12,000. Place your SQL statement in a text file
named lab_02_03.sql.
 SELECT last_name, salary
FROM EMPLOYEES
WHERE salary < 5000 OR salary > 12000;

3
4. Create a report to display the last name, job ID, and start date
for the employees with the last names of Matos and Taylor.
Order the query in ascending order by the start date.
 SELECT last_name, job_id, start_date
FROM EMPLOYEES
WHERE last_name IN ('Matos', 'Taylor')
ORDER BY start_date ASC;

5. Display the last name and department number of all employees


in departments 20 and 50 in ascending alphabetical order by
name.
 SELECT last_name, department_id
FROM EMPLOYEES
WHERE department_id IN (20, 50)
ORDER BY last_name ASC;

6. Modify lab_02_03.sql to display the last name and salary of


employees who earn between $5,000 and $12,000 and are in
department 20 or 50. Label the columns Employee and Monthly
Salary, respectively. Resave lab_02_03.sql as lab_02_06.sql. Run the
statement in lab_02_06.sql.
 SELECT last_name AS "Employee", salary AS "Monthly Salary"
FROM EMPLOYEES
WHERE (salary BETWEEN 5000 AND 12000) AND(department_id IN (20, 50));

4
6. The HR department needs a report that displays the last name
and hire date for all employees who were hired in 1994.

 SELECT last_name, hire_date


FROM EMPLOYEES
WHERE hire_date BETWEEN TO_DATE('1994-01-01', 'YYYY-MM-
DD') AND TO_DATE('1994-12-31', 'YYYY-MM-DD');

7. Create a report to display the last name and job title of all
employees who do not have a manager.
 SELECT e.last_name, j.job_title
FROM EMPLOYEES e
JOIN JOBS j ON e.job_id = j.job_id
WHERE e.manager_id IS NULL;

8. Create a report to display the last name, salary, and commission


of all employees who earn commissions. Sort data in
descending order of salary and commissions.
 SELECT last_name, salary, commission_pct
FROM EMPLOYEES
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC, commission_pct DESC;

10. Members of the HR department want to have more flexibility


with the queries that you are writing. They would like a report that
5
displays the last name and salary of employees who earn more than
an amount that the user specifies after a prompt. (You can use the
query that you created in practice exercise 1 and modify it.) Save
this query to a file named lab_02_10.sql. If you enter 12000 when
prompted, the report displays the following results
 ACCEPT salary_threshold NUMBER PROMPT 'Enter the minimum salary threshold:
'
SELECT last_name, salary
FROM EMPLOYEES
WHERE salary > &salary_threshold;

11. The HR department wants to run reports based on a manager.


Create a query that prompts the user for a manager ID and generates
the employee ID, last name, salary, and department for that manager’s
employees. The HR department wants the ability to sort the report on a
selected column. You can test the data with the following values:
manager ID = 103, sorted by employee last name:
manager ID = 201, sorted by salary:
manager ID = 124, sorted by employee ID:

 ACCEPT manager_id_input NUMBER PROMPT 'Enter Manager ID: '


ACCEPT sort_column CHAR PROMPT 'Enter Sort Column (employee_id, last_name,
salary, department_id): '

SELECT employee_id, last_name, salary, department_id


FROM EMPLOYEES
WHERE manager_id = &manager_id_input
ORDER BY
CASE
WHEN UPPER('&sort_column') = 'LAST_NAME' THEN last_name
WHEN UPPER('&sort_column') = 'SALARY' THEN salary
WHEN UPPER('&sort_column') = 'DEPARTMENT_ID' THEN department_id
ELSE employee_id
END;

6
12. Display all employee last names in which the third letter of the
name is a.
 SELECT last_name
FROM EMPLOYEES
WHERE SUBSTR(last_name, 3, 1) = 'a';

13. Display the last name of all employees who have both an a and
an e in their last name.
 SELECT last_name
FROM EMPLOYEES
WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';

14. Display the last name, job, and salary for all employees whose
job is sales representative or stock clerk and whose salary is not
equal to $2,500, $3,500, or $7,000.
 SELECT last_name, job, salary
FROM EMPLOYEES
WHERE (job = 'sales representative' OR job = 'stock clerk')
AND salary NOT IN (2500, 3500, 7000);

15. Modify lab_02_06.sql to display the last name, salary, and


commission for all employees whose commission amount is 20%.
Resave lab_02_06.sql as lab_02_15.sql. Rerun the statement in
lab_02_15.sql.

 SELECT last_name, salary, commission_pct


FROM EMPLOYEES
WHERE commission_pct = 0.20;

7
Chapter three: Part 1
1. Write a query to display the current date. Label the column
Date.
 SELECT SYSDATE AS "Date" FROM DUAL;

2. The HR department needs a report to display the employee


number, last name, salary, and salary increased by 15.5%
(expressed as a whole number) for each employee. Label the
column New Salary. Place your SQL statement in a text file named
lab_03_02.sql.
 SELECT employee_id, last_name, salary, ROUND(salary * 1.155)
AS "New Salary"
FROM EMPLOYEES;

3. Modify your query lab_03_02.sql to add a column that


subtracts the old salary from the new salary. Label the column
Increase. Save the contents of the file as lab_03_04.sql. Run the
revised query.
 SELECT employee_id, last_name, salary, ROUND(salary * 1.155)
AS "New Salary", ROUND(salary * 0.155) AS "Increase"
FROM EMPLOYEES;

4. Write a query that displays the last name (with the first letter
uppercase and all other letters lowercase) and the length of the last
name for all employees whose name starts with the letters J, A, or M.
Give each column an appropriate label. Sort the results by the
employees’ last names.
 SELECT INITCAP(last_name) AS "Last Name", LENGTH(last_name) AS "Last Name
Length"
FROM EMPLOYEES
WHERE last_name LIKE 'J%' OR last_name LIKE 'A%' OR last_name LIKE 'M%'
ORDER BY last_name;

8
5. Rewrite the query so that the user is prompted to enter a letter
that starts the last name. For example, if the user enters H when
prompted for a letter, then the output should show all employees
whose last name starts with the letter H.
 ACCEPT start_letter CHAR PROMPT 'Enter the starting letter of
the last name: '
SELECT INITCAP(last_name) AS "Last Name",
LENGTH(last_name) AS "Last Name Length"
FROM EMPLOYEES
WHERE last_name LIKE UPPER('&start_letter%')
ORDER BY last_name;

6. The HR department wants to find the length of employment for


each employee. For each employee, display the last name and
calculate the number of months between today and the date on
which the employee was hired. Label the column
MONTHS_WORKED. Order your results by the number of months
employed. Round the number of months up to the closest whole
number.
 SELECT last_name,
CEIL(MONTHS_BETWEEN(SYSDATE, hire_date)) AS"MONTHS_WORKED"
FROM employees
ORDER BY "MONTHS_WORKED";

Chapter three: Part 2


8. Create a report that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3
times salary>. Label the column Dream Salaries.
 SELECT last_name || ' earns $' || salary || ' monthly but wants $' || (salary
* 3) || '.' AS "Dream Salaries"
FROM employees;

9
9. Create a query to display the last name and salary for all
employees. Format the salary to be 15 characters long, left-padded
with the $ symbol. Label the column SALARY.
 SELECT last_name, LPAD('$' || TO_CHAR(salary), 15, '$') AS
"SALARY"
FROM employees;

10. Display each employee’s last name, hire date, and salary review
date, which is the first Monday after six months of service. Label the
column REVIEW. Format the dates to appear in the format similar to
“Monday, the Thirty-First of July, 2000.”
 SELECT last_name, hire_date,
NEXT_DAY(ADD_MONTHS(hire_date, 6), 'MONDAY') AS
"REVIEW"
FROM employees;

11. Display the last name, hire date, and day of the week on which the
employee started. Label the column DAY. Order the results by the day of the
week, starting with Monday.
 SELECT last_name, hire_date, TO_CHAR(hire_date, 'Day') AS
"DAY"
FROM employees
ORDER BY TO_CHAR(hire_date, 'D');

12. Create a query that displays the employees’ last names and commission
amounts. If an employee does not earn commission, show “No
Commission.” Label the column COMM.
 SELECT last_name,
CASE
WHEN commission_pct IS NOT NULL THEN commission_pct
ELSE 'No Commission'
END AS "COMM"
FROM employees;
10
13. Create a query that displays the first eight characters of the
employees’ last names and indicates the amounts of their salaries
with asterisks. Each asterisk signifies a thousand dollars. Sort the
data in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.
 SELECT
SUBSTR(last_name, 1, 8) || RPAD('*', ROUND(salary / 1000),
'*') AS "EMPLOYEES_AND_THEIR_SALARIES"
FROM employees
ORDER BY salary DESC;

14. Using the DECODE function, write a query that displays the
grade of all employees based on the value of the column JOB_ID,
using the following data:
a. Job Grade
b. AD_PRES A
c. ST_MAN B
d. IT_PROG C
e. SA_REP D
f. ST_CLERK E
g. None of the above 0

 SELECT last_name, job_id,


DECODE(job_id,
'AD_PRES', 'A',
'ST_MAN', 'B',
'IT_PROG', 'C',
'SA_REP', 'D',
'ST_CLERK', 'E',
'0') AS "Grade"
FROM employees;

11
15. Rewrite the statement in the preceding exercise using the CASE
syntax.
 SELECT
last_name,
job_id,
CASE job_id
WHEN 'AD_PRES' THEN 'A'
WHEN 'ST_MAN' THEN 'B'
WHEN 'IT_PROG' THEN 'C'
WHEN 'SA_REP' THEN 'D'
WHEN 'ST_CLERK' THEN 'E'
ELSE '0'
END AS "Grade"
FROM
employees;

Good Luck
Dr. Esmat Mohamamed

12

You might also like