SELECT Last - Name, Job - Id FROM Employees WHERE Job - Id (SELECT Job - Id FROM Employees WHERE Employee - Id 141)
SELECT Last - Name, Job - Id FROM Employees WHERE Job - Id (SELECT Job - Id FROM Employees WHERE Employee - Id 141)
SELECT Last - Name, Job - Id FROM Employees WHERE Job - Id (SELECT Job - Id FROM Employees WHERE Employee - Id 141)
DATABASE SYSTEMS
LAB 08
You can place the sub query in a number of SQL clauses, including the following:
WHERE, FROM and HAVING
Types of Subqueries
Single-row Subqueries: Queries that return only one row from the inner SELECT statement.
Multiple-row Subqueries: Queries that return more than one row from the inner SELECT
statement. If sub query returns multiple rows then we will use IN, ALL and ANY operators.
Single-row Subqueries Examples:
1- Display the employees whose job ID is the same as that of employee 141:
SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees
WHERE employee_id = 141);
2- Displays the employee last name, job ID, and salary of all employees whose salary is equal to
the minimum salary.
3- Find the job with the lowest average salary.
SELECT job_id, AVG(salary) FROM employees GROUP BY job_id HAVING AVG(salary)=(SELECT
MIN(AVG(salary)) FROM employees GROUP BY job_id);
<ANY means less than the maximum. >ANY means more than the minimum. =ANY is
equivalent to IN.
3- Using the ALL Operator:
>ALL means more than the maximum, and <ALL means less than the minimum.
LAB TASKS
1- The HR department needs a query that displays the last name and hire date of any employee in
the same department as the employee whose name is Zlotkey(excluding Zlotkey) i.e. find all
employees who work with Zlotkey (excluding Zlotkey).
2- 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.
3- 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.
4- 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.
5- Create a report for HR that displays the last name and salary of every employee who reports to
King.
6- Create a report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.
7- display the employee number, last name, and salary of all employees who earn more than the
average salary and who work in a department with any employee whose last name contains a u.
Examples:
Display the current and previous job details of all employees. Display each employee only once.
Display the employee IDs and job IDs of those employees who currently have a job title that is
the same as their job title when they were initially hired (that is, they changed jobs but have now
gone back to doing their original job).
Display the employee IDs of those employees who have not changed their jobs even once.
LAB TASKS
1- The HR department needs a list of department IDs for departments that do not contain the job
ID ST_CLERK. Use set operators to create this report.
2- The HR department needs a list of countries that have no departments located in them.
Display the country ID and the name of the countries. Use set operators to create this report.
3- Produce a list of jobs for departments 10, 50, and 20, in that order. Display job ID and
department ID using set operators.
4- Create a report that lists the employee IDs and job IDs of those employees who currently have
a job title that is the same as their job title when they were initially hired by the company (that is,
they changed jobs but have now gone back to doing their original job).