(INFORMATION MANAGEMENT)
EXERCISE
7
Advance SQL
Name of Student Name of Professor
Data Performed Date Submitted
I. OBJECTIVES
At the end of this exercise, students must be able to:
a) Join tables using an equijoin
b) Performing outer and self-joins
c) Adding conditions
d) Creating subqueries to query values based on unknown criteria
e) Using subqueries to find out which values exist in one set of data and not in another
II. BACKGROUND INFORMATION
Write the appropriate SQL statement for the following queries. The result of the queries will be checked
from your computer.
• Use a join to query data from more than one table:
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];
• Join–a relational operation that causes two or more tables with a common domain to be
combined into a single table or view
• Equi-join–a join in which the joining condition is based on equality between values in the
common columns; common columns appear redundantly in the result table
• Natural join–an equi-join in which one of the duplicate columns is eliminated in the result table
• Outer join–a join in which rows that do not have matching values in common columns are
nonetheless included in the result table (as opposed to inner join, in which rows must have
matching values in order to appear in the result table)
• Union join–includes all columns from each table in the join, and an instance for each row of each
table
• The common columns in joined tables are usually the primary key of the dominant table and the
foreign key of the dependent table in 1:M relationships.
Example:
Processing Multiple Tables Using Subqueries
• Subquery–placing an inner query (SELECT statement) inside an outer query
• Options:
– In a condition of the WHERE clause
– As a “table” of the FROM clause
– Within the HAVING clause
• Subqueries can be:
– Noncorrelated–executed once for the entire outer query
– Correlated–executed once for each row returned by the outer query
Example
III. EXPERIMENTAL PROCEDURE
INSTRUCTIONS:
Part 1 Multiple Queries
1. Write a query for the HR department to produce the addresses of all the departments. Use the
LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output. Use a NATURAL JOIN to produce the results.
2. The HR department needs a report of all employees. Write a query to display the last name,
department number, and department name for all the employees.
3. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and the department name for all employees who work in Toronto.
4. Create a report to display employees’ last name and employee number along with their manager’s
last name and manager number. Label the columns Employee, Emp#, Manager, and Mgr#,
respectively.
5. Modify question#4 to display all employees including King, who has no manager. Order the
results by the employee number.
6. Create a report for the HR department 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.
7. The HR department needs a report on job grades and salaries. To familiarize yourself with the
JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a query
that displays the name, job, department name, salary, and grade for all employees.
Part 2 Subqueries
8. The HR department needs a query that prompts the user for an employee last name. The query
then displays the last name and hire date of any employee in the same department as the
employee whose name they supply (excluding that employee). For example, if the user enters
Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).
9. Create a report that displays the employee number, last name, and salary of all employees who
earn more than the average salary. Sort the results in order of ascending salary.
10. Write a query that displays the employee number and last name of all employees who work in a
department with any employee whose last name contains a u.
11. The HR department needs a report that displays the last name, department number, and job ID of
all employees whose department location ID is 1700.
Modify the query so that the user is prompted for a location ID.
12. Create a report for HR that displays the last name and salary of every employee who reports to
King.
Create a report for HR that displays the department number, last name, and job ID for
every employee in the Executive department.
IV. ASSESSMENT
Department Information Technology
Subject Code CCS0014
Description Information Management
Term/Academic Year 1 / SY20182019
Topic Advance SQL
Lab Activity No 7
Lab Activity Advance SQL
CLO 4
Note: The following rubrics/metrics will be used to grade students’ output in the lab
exercise.
Criteria Descriptions Points
JOIN SQL statements that uses all JOIN 50
keywords
Subqueries SQL statements that uses subqueries inside 50
the statement
Total 100