Week 5 Lab - SQL DML Part 2
Week 5 Lab – SQL DML Part 2
References:
Coronel, Morris, & Rob, Database Systems: Design, Implementation & Management, 10th Edition 2013, Thomson Course
Technology. Chapter 7 (7.3 - 7.6) and 8 (8.1 - 8.4)
or
Coronel, Morris, & Rob, Database Systems: Design, Implementation & Management, 9th Edition 2011, Thomson Course
Technology. Chapter 7 (7.3 - 7.6) and 8 (8.1 - 8.4)
Review Questions
Coronel, Morris, & Rob, Database Systems: Design, Implementation & Management Chapter 7:
1. In a SELECT query, what is the difference between a WHERE clause and a HAVING clause?
2. Explain why the following command would create an error, and what changes could be made to fix the error.
SELECT V_CODE, SUM(P_QOH) FROM PRODUCT;
3. Rewrite the following WHERE clause without the use of the IN special operator: WHERE V_STATE IN (‘TN’, ‘FL’,
‘GA’)
4. Explain the difference between an ORDER BY clause and a GROUP BY clause.
5. Explain why the two following commands produce different results.
SELECT DISTINCT COUNT (V_CODE) FROM PRODUCT;
SELECT COUNT (DISTINCT V_CODE) FROM PRODUCT;
Coronel, Morris, & Rob, Database Systems: Design, Implementation & Management Chapter 8:
6. What is a subquery, and what are its basic characteristics?
7. What are the three types of results a subquery can return?
8. What does it mean to say that SQL operators are set-oriented?
9. The relational set operators UNION, INTERSECT, and MINUS work properly only if the relations are union-compatible.
What does union-compatible mean, and how would you check for this condition?
10. What is the difference between UNION and UNION ALL? Write the syntax for each.
11. Suppose that you have two tables, EMPLOYEE and EMPLOYEE_1. The EMPLOYEE table contains the records for
three employees: Alice Cordoza, John Cretchakov, and Anne McDonald. The EMPLOYEE_1 table contains the records for
employees John Cretchakov and Mary Chen. Given that information, what is the query output for the UNION query? (List the
query output.)
12. Given the employee information in Question 11, what is the query output for the UNION ALL query? (List the query
output.)
13. Given the employee information in Question 11, what is the query output for the INTERSECT query? (List the query
output.)
14. Given the employee information in Question 1, what is the query output for the MINUS query? (List the query output.)
15. Why does the order of the operands (tables) matter in a MINUS query but not in a UNION query?
16. What Oracle function should you use to calculate the number of days between the current date and January 25, 1999?
17. Suppose that a PRODUCT table contains two attributes, PROD_CODE and VEND_CODE. Those two attributes have
values of ABC, 125, DEF, 124, GHI, 124, and JKL, 123, respectively. The VENDOR table contains a single attribute,
VEND_CODE, with values 123, 124, 125, and 126, respectively. (The VEND_CODE attribute in the PRODUCT table is a
foreign key to the VEND_CODE in the VENDOR table.) Given that information, what would be the query output for:
a. A UNION query based on these two tables?
b. A UNION ALL query based on these two tables?
c. An INTERSECT query based on these two tables?
d. A MINUS query based on these two tables?
18. What string function should you use to list the first three characters of a company’s EMP_LNAME values? Give an
1 of 3
Week 5 Lab - SQL DML Part 2
example, using a table named EMPLOYEE.
SQL Queries Lab Work
Employee Logical Database Model:
For this weeks tutorial we will be using the same scenario as last weeks tutorial. Download the archive payroll.zip (this is the same archive
which was available last week) and create and load the tables in your Oracle account. As the tables are now in your Oracle account, you
do not need to preface the tablename with payroll as you did for week 4. By default if you supply no owner name Oracle will assume you
are referring to a table in your own account.
1. Display all employees, their current annual salary (not including commission) and what their annual salary would be if they were
given a 10% pay rise.
2. Display the name of all employees, their birthdate and their age in years.
3. Display all employees, their job and their current annual salary (including commission).
4. Display all employees’ details in the following format: EMPLOYEE N. Smith IS A Trainer AND WORKS IN THE Training
DEPARTMENT.
5. Display the name of all employees, their birthdate and their age in months.
6. Display the employees who were born in February.
7. Display the employees (using the GREATEST function) who earn more commission than their monthly salary.
8. Display the name of all employees and their birthdate in the following format: EMPLOYEE N. Smith was born on FRIDAY the 17 of
DECEMBER , 1965
9. Display the name of the employees who have registered for a course and the number of times they have registered.
10. Who is the oldest employee?
11. For each department list the department number and name, the number of employees, the minimum and maximum monthly salary,
the total monthly salary and the average salary paid to their employees. Name the columns: NbrOfEmployees, MinSalary, MaxSalary,
TotalSalary, AvgSalary
12. Display the jobs in each department and the total monthly salary paid for each job.
13. Which employee earns more than the average salary?
14. Which department has the greatest average monthly salary?
15. Which course has the most offerings?
16. Display the name of employees who perform the same job as SCOTT and were born in the same year. Do not include SCOTT in the
output.
17. Using the MINUS statement, which employees have never registered in a course.
18. Using the INTERSECT statement, which employees have both registered for and conducted courses.
19. Add the following details to the employee table. Employee number: 9999, name: H.B. Bear, birthdate: 21st July 1965, monthly
2 of 3
Week 5 Lab - SQL DML Part 2
salary: $3500, department same as MILLER
20. Update employee H B. Bear's monthly salary to the same amount as KING's, his job to the same as CLARK's and his department to
SALES.
21. Delete employee HB. Bear.
3 of 3