0% found this document useful (0 votes)
35 views7 pages

SQL Sub Queries

A subquery is a query nested inside another query. There are several types of subqueries that serve different purposes: scalar subqueries return a single value, single-row subqueries return one row, multiple-row subqueries return multiple rows, correlated subqueries reference columns in the outer query, and EXISTS/NOT EXISTS subqueries test for the existence of rows. The document provides examples of each type of subquery and how they can be used in the SELECT, FROM, WHERE, and HAVING clauses.

Uploaded by

bbddbbd2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views7 pages

SQL Sub Queries

A subquery is a query nested inside another query. There are several types of subqueries that serve different purposes: scalar subqueries return a single value, single-row subqueries return one row, multiple-row subqueries return multiple rows, correlated subqueries reference columns in the outer query, and EXISTS/NOT EXISTS subqueries test for the existence of rows. The document provides examples of each type of subquery and how they can be used in the SELECT, FROM, WHERE, and HAVING clauses.

Uploaded by

bbddbbd2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

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:

A scalar subquery is a subquery that returns a single value. It can be used in a


SELECT, WHERE, or HAVING clause.

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.

SELECT FIRST_NAME, LAST_NAME, SALARY,


SALARY - (SELECT AVG(SALARY) FROM oehr_employees WHERE JOB_ID =
'IT_PROG') AS SALARY_DIFF
FROM oehr_employees
WHERE JOB_ID = 'IT_PROG';

Example 2:
Find the employees whose hire date is later than the hire date of the employee with
EMPLOYEE_ID 114.

SELECT FIRST_NAME, LAST_NAME, HIRE_DATE


FROM oehr_employees
WHERE HIRE_DATE > (SELECT HIRE_DATE FROM oehr_employees WHERE
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.

SELECT FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees
WHERE SALARY = (SELECT MAX(SALARY) FROM oehr_employees WHERE JOB_ID
= 'IT_PROG');

Example 2:
Find the manager for the employee with EMPLOYEE_ID 110.

SELECT FIRST_NAME, LAST_NAME, MANAGER_ID


FROM oehr_employees
WHERE EMPLOYEE_ID = (SELECT MANAGER_ID FROM oehr_employees WHERE
EMPLOYEE_ID = 110);

Example 3:
Get the first name, last name, and hire date of the employee with the lowest salary.

SELECT FIRST_NAME, LAST_NAME, HIRE_DATE


FROM oehr_employees
WHERE SALARY = (SELECT MIN(SALARY) FROM oehr_employees);

3. Multiple Row Subquery:

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.

SELECT FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees
WHERE SALARY > ALL (SELECT SALARY FROM oehr_employees WHERE JOB_ID =
'FI_ACCOUNT');

Example 3:
Get the employees who have the same job as the employee with EMPLOYEE_ID 120
and work in the same department.

SELECT FIRST_NAME, LAST_NAME, JOB_ID


FROM oehr_employees
WHERE JOB_ID = (SELECT JOB_ID FROM oehr_employees WHERE EMPLOYEE_ID
= 120)
AND DEPARTMENT_ID = (SELECT DEPARTMENT_ID FROM oehr_employees
WHERE EMPLOYEE_ID = 120);

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.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees e
WHERE SALARY > (SELECT AVG(SALARY) FROM oehr_employees WHERE
DEPARTMENT_ID = e.DEPARTMENT_ID);
Example 2:
Retrieve employees who have a salary greater than the average salary of their
department and joined the company after their manager.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees e
WHERE SALARY > (SELECT AVG(SALARY) FROM oehr_employees WHERE
DEPARTMENT_ID = e.DEPARTMENT_ID)
AND HIRE_DATE > (SELECT HIRE_DATE FROM oehr_employees WHERE
EMPLOYEE_ID = e.MANAGER_ID);

5. EXISTS and NOT EXISTS Subqueries:

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.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME


FROM oehr_employees e
WHERE EXISTS (SELECT 1 FROM oehr_employees WHERE MANAGER_ID =
e.EMPLOYEE_ID);

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.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME


FROM oehr_employees e
WHERE NOT EXISTS (SELECT 1 FROM oehr_employees WHERE MANAGER_ID =
e.EMPLOYEE_ID);

Example 3:
Get the employees who have at least one direct report with a higher salary.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME


FROM oehr_employees e
WHERE EXISTS (SELECT 1 FROM oehr_employees WHERE MANAGER_ID =
e.EMPLOYEE_ID AND SALARY > e.SALARY);

6. ANY and ALL Subqueries:

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.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees
WHERE SALARY > ANY (SELECT SALARY FROM oehr_employees WHERE JOB_ID
= 'SA_REP');

Example 2:
Retrieve employees who have a salary less than or equal to all employees in the
PU_CLERK department.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees
WHERE SALARY <= ALL (SELECT SALARY FROM oehr_employees WHERE JOB_ID
= 'PU_CLERK');

Example 3:
Retrieve employees who have a salary greater than any employee in the company,
except those in the SA_REP department.

SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY


FROM oehr_employees
WHERE SALARY > ANY (SELECT SALARY FROM oehr_employees) AND JOB_ID !=
'SA_REP';

Practice

Exercise 1: Scalar Subquery

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.

Exercise 2: Single Row Subquery

1) Find the employee with the highest salary and display their details along with the name of
their department.

Exercise 3: Multiple Row Subquery

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.

Exercise 4: Correlated Subquery

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.

Exercise 5: EXISTS and NOT EXISTS Subqueries

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.

Exercise 6: ANY and ALL Subqueries

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.

You might also like