Vous êtes sur la page 1sur 3

ITE

407 ADVANCED DATABASE MANAGEMENT SYSTEMS Laboratory Exercise 0.2

Theory: Set Operators in SQL

Set operators combine the results of two or more component queries into one result. Queries
containing set operators are called compound queries. All set operators have equal
precedence. If a SQL statement contains multiple set operators, the SQL engine evaluates them
from left (top) to right (bottom) if no parentheses explicitly specify another order. You should
use parentheses to specify the order of evaluation explicitly in queries that use the INTERSECT
operator with other set operators.

Objective

The objective of this exercise is to demonstrate set operators with SQL.

Tools/Applications

- Oracle Express covers a lot of set operators for this exercise. MySQL may also be used.
MS Access may not be suitable for this exercise.
- A script that creates an HR Schema. This can be obtained from your Instructor. If you
use Oracle Express, it is part of the installation.

Lab Activity:

1. Open Oracle Express or MySQL.


2. If you are using MySQL, run the Dump_HR.sql script to create the HR Schema. This script
also populates all the tables in the schema

3. Demonstrate the UNION operator: Show all employee_id and job_id in EMPLOYEES table in
a UNION with employee_id and job_id in the JOB_HISTORY table.

Solution:



4. Demonstrate the UNION ALL operator: Modify the query in Question 3 above to use the
UNION ALL operator. In each part of the query, display the department_id as well. Order
your output by the employee_id. What do you notice about this querys results?

Solution:








Rewrite this query with the UNION operator.

Solution:







5. Demonstrate the INTERSECT operator: display the intersected employee_id, and job_id in
the EMPLOYEES and JOB_HISTORY table.
Note that MySQL does not have the INTERSECT Operator. If you are using Oracle Express
you can use the INTERSECT. Your Instructor will show you how to write a query in MySQL
which produces the same result using another method.

Solution:





6. Demonstrate the MINUS Operator: Display the employee_id in EMPLOYEES table that are
not in JOB_HISTORY.

Solution:
7. Use the ORDER BY with the column you desire to order on: Show the ascending order of the
employee_id, job_id, and salary from the employees table as a union with the job_history
table ordered on the second column.

Solution:















Reflection

1. What would a RIGHT JOIN produce? What set operator could you possibly associate
this to?

2. What would the effect be of omitting the ORDER BY clause in the result you worked
out in Question 4 above?

3. Use Oracle Express to validate the INTERSET, and MINUS SQL constructs you have
used in this lab exercise; or employ the solutions from Oracle Express in a MySQL
session to validate the results obtained.




Tuesday, February 14, 2017

Vous aimerez peut-être aussi