Download Introduction to Oracle9i: SQL - Practice Exercises and Solutions and more Lab Reports Computer Science in PDF only on Docsity!
Practice Solutions
Practice 1 Solutions (continued)
- Run your query in the file lab1_7.sql.
SELECT employee_id, last_name, job_id, hire_date FROM employees;
- Create a query to display unique job codes from the EMPLOYEES table.
SELECT DISTINCT job_id FROM employees;
If you have time, complete the following exercises:
- Copy the statement from lab1_7.sql into the i SQL*Plus Edit window. Name the column headings Emp #, Employee, Job, and Hire Date, respectively. Run your query again. SELECT employee_id "Emp #", last_name "Employee", job_id "Job", hire_date "Hire Date" FROM employees;
- 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 "Employee and Title" FROM employees;
If you want an extra challenge, complete the following exercise:
- Create a query to display all the data from the EMPLOYEES table. Separate each column by a comma. Name the column THE_OUTPUT. SELECT employee_id || ',' || first_name || ',' || last_name || ',' || email || ',' || phone_number || ','|| job_ id || ',' || manager_id || ',' || hire_date || ',' || salary || ',' || commission_pct || ',' || department _id THE_OUTPUT FROM employees;
Practice 2 Solutions
- Create a query to display the last name and salary of employees earning more than $12,000. Place your SQL statement in a text file named lab2_1.sql. Run your query.
SELECT last_name, salary FROM employees WHERE salary > 12000;
- Create a query to display the employee last name and department number for employee number
SELECT last_name, department_id FROM employees WHERE employee_id = 176;
- Modify lab2_1.sql to display the last name and salary for all employees whose salary is not in the range of $5,000 and $12,000. Place your SQL statement in a text file named lab2_3.sql.
SELECT last_name, salary FROM employees WHERE salary NOT BETWEEN 5000 AND 12000;
- Display the employee last name, job ID, and start date of employees hired between February 20, 1998, and May 1, 1998. Order the query in ascending order by start date.
SELECT last_name, job_id, hire_date FROM employees WHERE hire_date BETWEEN '20-Feb-1998' AND '01-May-1998' ORDER BY hire_date;
Practice 2 Solutions (continued)
If you have time, complete the following exercises.
- Display the last names of all employees where the third letter of the name is an a.
SELECT last_name FROM employees WHERE last_name LIKE '__a%';
- Display the last name of all employees who have an a and an e in their last name.
SELECT last_name FROM employees WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
If you want an extra challenge, complete the following exercises:
- 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_id, salary FROM employees WHERE job_id IN ('SA_REP', 'ST_CLERK') AND salary NOT IN (2500, 3500, 7000);
- Modify lab2_6.sql to display the last name, salary, and commission for all employees whose commission amount is 20%. Resave lab2_6.sql as lab2_13.sql. Rerun the statement in lab2_13.sql. . SELECT last_name "Employee", salary "Monthly Salary", commission_pct FROM employees WHERE commission_pct = .20;
Practice 3 Solutions
- Write a query to display the current date. Label the column Date.
SELECT sysdate "Date" FROM dual;
- For each employee, display the employee number, last_name, salary, and salary increased by 15% and expressed as a whole number. Label the column New Salary. Place your SQL statement in a text file named lab3_2.sql.
SELECT employee_id, last_name, salary, ROUND(salary * 1.15, 0) "New Salary" FROM employees;
- Run your query in the file lab3_2.sql.
SELECT employee_id, last_name, salary, ROUND(salary * 1.15, 0) "New Salary" FROM employees;
- Modify your query lab3_2.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 lab3_4.sql. Run the revised query. SELECT employee_id, last_name, salary, ROUND(salary * 1.15, 0) "New Salary", ROUND(salary * 1.15, 0) - salary "Increase" FROM employees;
- Write a query that displays the employee’s last names with the first letter capitalized and all other letters lowercase and the length of the name for all employees whose name starts with J , A , or M. Give each column an appropriate label. Sort the results by the employees’ last names. SELECT INITCAP(last_name) "Name", LENGTH(last_name) "Length" FROM employees WHERE last_name LIKE 'J%' OR last_name LIKE 'M%' OR last_name LIKE 'A%' ORDER BY last_name;
Practice 3 Solutions (continued)
If you want an extra challenge, complete the following exercises:
- Create a query that displays the employees’ last names and commission amounts. If an employee does not earn commission, put “ No Commission.” Label the column COMM. SELECT last_name, NVL(TO_CHAR(commission_pct), 'No Commission') COM M FROM employees;
- Create a query that displays the employees’ last names and indicates the amounts of their annual salaries with asterisks. Each asterisk signifies a thousand doll ars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
SELECT rpad(last_name, 8)||' '|| rpad(' ', salary/1000+1, '*') EMPLOYEES_AND_THEIR_SALARIES FROM employees ORDER BY salary DESC;
- Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, as per the following data: JOB GRADE AD_PRES A ST_MAN B IT_PROG C SA_REP D ST_CLERK E None of the above 0
SELECT job_id, decode (job_id, 'ST_CLERK', 'E', 'SA_REP', 'D', 'IT_PROG', 'C', 'ST_MAN', 'B', 'AD_PRES', 'A', '0')GRADE FROM employees;
Practice 3 Solutions (continued)
- Rewrite the statement in the preceding question using the CASE syntax.
SELECT job_id, CASE job_id WHEN 'ST_CLERK' THEN 'E' WHEN 'SA_REP' THEN 'D' WHEN 'IT_PROG' THEN 'C' WHEN 'ST_MAN' THEN 'B' WHEN 'AD_PRES' THEN 'A' ELSE '0' END GRADE FROM employees;
Practice 4 Solutions (continued)
- Write a query to display the last name, job, department number, and department name for all employees who work in Toronto. SELECT e.last_name, e.job_id, e.department_id, d.department_name FROM employees e JOIN departments d ON (e.department_id = d.department_id) JOIN locations l ON (d.location_id = l.location_id) WHERE LOWER(l.city) = 'toronto';
- Display the employee last name and employee number along with their manager’s last name and manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively. Place your SQL statement in a text file named lab4_6.sql. SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#" FROM employees w join employees m ON (w.manager_id = m.employee_id);
Practice 4 Solutions (continued)
- Modify lab4_6.sql to display all employees including King, who has no manager. Place your SQL statement in a text file named lab4_7.sql. Run the query in lab4_7.sql SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#" FROM employees w LEFT OUTER JOIN employees m ON (w.manager_id = m.employee_id);
If you have time, complete the following exercises.
- Create a query that displays employee last names, department numbers, and all the employees who work in the same department as a given employee. Give each column an appropriate label. SELECT e.department_id department, e.last_name employee, c.last_name colleague FROM employees e JOIN employees c ON (e.department_id = c.department_id) WHERE e.employee_id <> c.employee_id ORDER BY e.department_id, e.last_name, c.last_name;
- Show the structure of the JOB_GRADES table. Create a query that displays the name, job, department name, salary, and grade for all employees. DESC JOB_GRADES SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level FROM employees e, departments d, job_grades j WHERE e.department_id = d.department_id AND e.salary BETWEEN j.lowest_sal AND j.highest_sal; -- OR SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level FROM employees e JOIN departments d ON (e.department_id = d.department_id) JOIN job_grades j ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal);
Practice 5 Solutions
Determine the validity of the following three statements. Circle either True or False.
- Group functions work across many rows to produce one result. True
- Group functions include nulls in calculations. False. Group functions ignore null values. If you want to include null values, use the NVL function.
- The WHERE clause restricts rows prior to inclusion in a group calculation. True
- Display the highest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest whole number. Place your SQL statement in a text file named lab5_6.sql.
SELECT ROUND(MAX(salary),0) "Maximum", ROUND(MIN(salary),0) "Minimum", ROUND(SUM(salary),0) "Sum", ROUND(AVG(salary),0) "Average" FROM employees;
- Modify the query in lab5_4.sql to display the minimum, maximum, sum, and average salary for each job type. Resave lab5_4.sql to lab5_5.sql. Run the statement in lab5_5.sql.
SELECT job_id, ROUND(MAX(salary),0) "Maximum", ROUND(MIN(salary),0) "Minimum", ROUND(SUM(salary),0) "Sum", ROUND(AVG(salary),0) "Average" FROM employees GROUP BY job_id;
Practice 5 Solutions (continued)
- Write a query to display the number of people with the same job. SELECT job_id, COUNT(*) FROM employees GROUP BY job_id;
- Determine the number of managers without listing them. Label the column Number of Managers. Hint : Use the MANAGER_ID column to determine the number of managers.
SELECT COUNT(DISTINCT manager_id) "Number of Managers" FROM employees;
- Write a query that displays the difference between the highest a nd lowest salaries. Label the column DIFFERENCE.
SELECT MAX(salary) - MIN(salary) DIFFERENCE FROM employees;
If you have time, complete the following exercises.
- Display the manager number and the salary of the lowest paid employee for that manager. Exclude anyone whose manager is not known. Exclude any groups where the minimum salary is less than $6,000. Sort the output in descending order of salary.
SELECT manager_id, MIN(salary) FROM employees WHERE manager_id IS NOT NULL GROUP BY manager_id HAVING MIN(salary) > 6000 ORDER BY MIN(salary) DESC;
- Write a query to display each department’s name, location, number of employees, and the average salary for all employees in that department. Label the columns Name, Location, Number of People, and Salary, respectively. Round the average salary to two decimal places.
SELECT d.department_name "Name", d.location_id "Location ", COUNT(*) "Number of People", ROUND(AVG(salary),2) "Salary" FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name, d.location_id;
Practice 6 Solutions
- Write a query to display the last name and hire date of any employee in the same department as Zlotkey. Exclude Zlotkey.
SELECT last_name, hire_date FROM employees WHERE department_id = (SELECT department_id FROM employees WHERE last_name = 'Zlotkey') AND last_nae <> 'Zlotkey';
- Create a query to display the employee numbers and last names of all employees who earn more than the average salary. Sort the results in descending order of salary. SELECT employee_id, last_name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
- Write a query that displays the employee numbers and last names of all employees who work in a department with any employee whose last name contains a u. Place your SQL statement in a text file named lab6_3.sql. Run your query.
SELECT employee_id, last_name FROM employees WHERE department_id IN (SELECT department_id FROM employees WHERE last_name like '%u%');
- Display the last name, department number, and job ID of all employees whose department location ID is 1700. SELECT last_name, department_id, job_id FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700);
Practice 6 Solutions (continued)
- Display the last name and salary of every employee who reports to King. SELECT last_name, salary FROM employees WHERE manager_id = (SELECT employee_id FROM employees WHERE last_name = 'King');
- Display the department number, last name, and job ID for every employee in the Executive department. SELECT department_id, last_name, job_id FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'Executive');
If you have time, complete the following exercises:
- Modify the query in lab6_3.sql to display the employee numbers, last names, and salaries of all employees who earn more than the average salary and who work in a department with any employee with a u in their name. Resave lab6_3.sql to lab6_7.sql. Run the statement in lab6_7.sql. SELECT employee_id, last_name, salary FROM employees WHERE department_id IN (SELECT department_id FROM employees WHERE last_name like '%u%') AND salary > (SELECT AVG(salary) FROM employees);