0% found this document useful (0 votes)
97 views4 pages

Part 1 Multiple Queries

The document contains a series of SQL queries for an HR department: 1. The first few queries produce reports on department addresses, all employees, and employees in Toronto. 2. Later queries display employee and manager information, include employees without managers, and find colleagues in the same department. 3. The document also describes the JOBS table structure and produces a report on job titles and salaries. 4. The remaining queries use subqueries to find employees hired after others, earn more than average, work with someone with 'u' in their name, and report to or are in the executive department.

Uploaded by

JuanMig Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views4 pages

Part 1 Multiple Queries

The document contains a series of SQL queries for an HR department: 1. The first few queries produce reports on department addresses, all employees, and employees in Toronto. 2. Later queries display employee and manager information, include employees without managers, and find colleagues in the same department. 3. The document also describes the JOBS table structure and produces a report on job titles and salaries. 4. The remaining queries use subqueries to find employees hired after others, earn more than average, work with someone with 'u' in their name, and report to or are in the executive department.

Uploaded by

JuanMig Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

SELECT location_id, street_address, city, state_province, country_name


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);

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.

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';

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);
5. Modify question#4 to display all employees including King, who has no manager. Order the results by the
employee number.

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);

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;

7. The HR department needs a report on job title and salaries. To familiarize yourself with the JOBS table, first
show the structure of the JOBS table. Then create a query that displays the name, job title, department
name, and salary for all employees.

DESC JOB_GRADES
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);

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
SELECT e.last_name, e.hire_date
FROM employees e JOIN employees davies
ON (davies.last_name = 'Davies')
WHERE davies.hire_date < e.hire_date;
9. name they supply (excluding that employee). For example, if the user enters Zlotkey, find all employees who
work with Zlotkey (excluding Zlotkey).

10. 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 e.number, e.lastname, e.salary


FROM employees
WHERE SALARY > AVERAGE SALARY
ORDER BY ASC

11. 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.

SELECT e.number, e.lastname


FROM DEPARTMENT
WHERE LIKE ‘%u’

12. 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 e.lastname, dept.number, job_ID


FROM DEPARTMENT
WHERE locID = 1700

13. Create a report for HR that displays the last name and salary of every employee who reports to King.

14. Create a report for HR that displays the department number, last name, and job ID for every employee in
the Executive department.
SELECT *
FROM DEPARTMENT;

I. QUESTION AND ANSWER

1. What is the difference between a Join and a Natural Join?

In Natural Join, If there is no condition specifies then it returns the rows based on the common column
while In Inner Join, only those records will return which exists in both the tables.

II. REFERENCES

 Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.

You might also like