m8 Formdoc Bautista Ishmael
m8 Formdoc Bautista Ishmael
m8 Formdoc Bautista Ishmael
CCS0021L
(INFORMATION MANAGEMENT)
EXERCISE
8
ADVANCED SQL
Student Name / Bautista, Ishmael Jehoshaphat
Group Name: J.
Name Role
Members (if Group):
CCS0021L – TN21
Section:
• Create SQL statements that retrieve information requirements of the organization needed for reports
generation. [CLO: 4]
Write the appropriate SQL statement for the following queries. The result of the queries will be
checked from your computer.
• 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:
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.
SELECT LOCATION_ID, STREET_ADDRESS, CITY, STATE_PROVINCE, COUNTRY_ID
FROM LOCATIONS
NATURAL JOIN COUNTRIES;
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.
SELECT LAST_NAME, DEPARTMENT_ID, DEPARTMENT_NAME
FROM EMPLOYEES
JOIN DEPARTMENTS
USING(DEPARTMENT_ID);
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.
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);
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.
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;
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).
SELECT LAST_NAME, HIRE_DATE
FROM EMPLOYEES
WHERE DEPARTMENT_ID = (
SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = '&&ENTER_NAME')
AND LAST_NAME <> '&ENTER_NAME';
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.
SELECT EMPLOYEE_ID, LAST_NAME, SALARY
FROM EMPLOYEES
WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEES)
ORDER BY SALARY ASC;
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.
SELECT LAST_NAME, DEPARTMENT_ID, JOB_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (SELECT DEPARTMENT_ID
FROM DEPARTMENTS
WHERE LOCATION_ID = 1700);
13. Create a report for HR that displays the department number, last name, and job ID for
every employee in the Executive department.
VIII. REFERENCES
of 10
CCS0021L-Information Management Page 11