SQL Sub Queries
SQL Sub Queries
A subquery in SQL, also known as a nested query or inner query, is a query nested inside
another query. Subqueries are used to retrieve data that will be used in the main query as a
condition to further restrict the data to be retrieved. Subqueries can be used in various parts of a
SQL statement, such as the SELECT, FROM, WHERE, and HAVING clauses.
There are several types of subqueries, and they serve different purposes.
1. Scalar Subquery:
Example 1:
Retrieve the first name, last name, and the difference between an employee's salary and
the average salary in the IT_PROG department.
Example 2:
Find the employees whose hire date is later than the hire date of the employee with
EMPLOYEE_ID 114.
Example 3:
Get the total number of employees in the department where employee 120 works.
SELECT COUNT(*)
FROM oehr_employees
WHERE DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM oehr_employees
WHERE EMPLOYEE_ID = 120);
2. Single Row Subquery:
A single-row subquery returns one row of data to the outer SQL statement. It can be
used in a WHERE or HAVING clause.
Example 1:
Retrieve the employee with the highest salary in the IT_PROG department.
Example 2:
Find the manager for the employee with EMPLOYEE_ID 110.
Example 3:
Get the first name, last name, and hire date of the employee with the lowest salary.
A multiple-row subquery returns multiple rows of data to the outer SQL statement. It can
be used with comparison operators such as IN, ANY, or ALL.
Example 1:
Retrieve employees who have a salary greater than the average salary of their
department and have a commission percentage.
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM oehr_employees e
WHERE SALARY > (SELECT AVG(SALARY) FROM oehr_employees WHERE
DEPARTMENT_ID = e.DEPARTMENT_ID)
AND COMMISSION_PCT IS NOT NULL;
Example 2:
Find employees who have a higher salary than any employee in the FI_ACCOUNT
department.
Example 3:
Get the employees who have the same job as the employee with EMPLOYEE_ID 120
and work in the same department.
4. Correlated Subquery:
A correlated subquery refers to a subquery that references columns from the outer
query. It is evaluated once for each row processed by the outer query.
Example 1:
Retrieve employees who have a salary greater than the average salary of their
department.
These subqueries are used to test for the existence of rows that meet certain criteria.
Example 1:
Find employees who have at least one direct report.
Explanation:
Direct Report: A "direct report" refers to an employee who directly reports to another
employee, typically a manager. For example, if employee A is the manager of employee
B, then B is a direct report to A. In the context of the query, it is finding employees who
are managers (WHERE MANAGER_ID = e.EMPLOYEE_ID) and have at least one
employee reporting to them.
SELECT 1: In SQL, SELECT 1 is a common way to check for the existence of a record
without retrieving specific column values. It simply selects the literal value 1 for each row
that satisfies the conditions in the subquery. The EXISTS clause checks if there is at
least one row returned by the subquery.
Example 2:
Retrieve employees who don't have any direct reports.
Example 3:
Get the employees who have at least one direct report with a higher salary.
These subqueries are used with comparison operators and are often used in
combination with aggregate functions.
Example 1:
Find employees who have a salary greater than any employee in the SA_REP
department.
Example 2:
Retrieve employees who have a salary less than or equal to all employees in the
PU_CLERK department.
Example 3:
Retrieve employees who have a salary greater than any employee in the company,
except those in the SA_REP department.
Practice
1) Retrieve the names of employees who have a salary greater than the average salary of their
department but less than the highest salary in the company. Display the employee names along
with their salary and the difference between their salary and the average salary of their
department.
2) Find the employees who have a commission percentage greater than the average
commission percentage of the Sales department. Display the employee details along with the
average commission percentage of the Sales department.
1) Find the employee with the highest salary and display their details along with the name of
their department.
1) Retrieve the employees who have a salary greater than the average salary of their
department and have joined the company after the average hire date of their department.
Display the employee details along with the department name.
2) Find the employees who have a salary greater than the average salary of their department
and have a job in the same department. Display the employee details along with the department
name.
1) Retrieve the first name, last name, and hire date of employees hired after their respective
manager was hired. Display the employee details along with the name of their manager.
1) Find the employees who have a manager. Display the employee details along with the name
of their manager.
2) Find the employees who have a manager and have not received any commissions. Display
the employee details along with the name of their manager.
1) Find the employees whose salary is greater than any employee in the Finance department
and less than all employees in the Sales department. Display employee details along with the
department name.