Subqueries, also known as nested queries, are one of the most powerful features in SQL. A subquery is a query embedded within another query, typically in the WHERE
, FROM
, or SELECT
clauses. They allow you to perform more complex operations and retrieve data based on the results of another query, enabling you to work with multiple layers of data in a single SQL statement.
A subquery is a SQL query nested inside another query. The outer query is the main query, and the subquery provides the values needed to execute the main query. Subqueries can be used to:
Subqueries are often used to simplify complex queries that would otherwise require multiple steps or joins.
SELECT column1, column2
FROM table
WHERE column1 = (SELECT column1 FROM another_table WHERE condition);
Subqueries can be classified based on where they are used and their behavior. Here are the most common types:
A single-row subquery returns a single value, which is typically used in comparisons (=
, >
, <
, <=
, >=
, <>
) in the WHERE
clause.
You want to find the employee with the highest salary.
SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Explanation:
(SELECT MAX(salary) FROM employees)
finds the highest salary.Result:
first_name | last_name | salary |
---|---|---|
John | Doe | 120000 |
A multi-row subquery returns more than one value, and is used with operators like IN
, NOT IN
, ANY
, or ALL
.
To find employees who work in departments located in New York:
SELECT first_name, last_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'New York');
Explanation:
Result:
first_name | last_name |
---|---|
Alice | Johnson |
Bob | Smith |
A correlated subquery refers to columns from the outer query. It is evaluated once for each row processed by the outer query.
Find employees who earn more than the average salary in their respective departments:
SELECT first_name, last_name, department_id, salary
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id);
Explanation:
e.department_id
, which comes from the outer query.Result:
first_name | last_name | department_id | salary |
---|---|---|---|
Alice | Johnson | 1 | 80000 |
Bob | Smith | 2 | 90000 |
Subqueries can be used in different parts of a SQL query:
You can use subqueries in the SELECT
clause to return a value derived from another table.
SELECT first_name, last_name,
(SELECT department_name FROM departments WHERE department_id = employees.department_id) AS department_name
FROM employees;
Explanation:
SELECT
clause fetches the department name for each employee based on their department_id
.The WHERE
clause is the most common place to use subqueries. Subqueries in the WHERE
clause are typically used to filter records based on the results of another query.
Find employees who earn more than the average salary:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Subqueries in the FROM
clause are often called inline views or derived tables. These subqueries return a result set that is treated like a table.
SELECT department_id, MAX(salary) AS highest_salary
FROM (SELECT department_id, salary FROM employees WHERE salary > 50000) AS filtered_employees
GROUP BY department_id;
Explanation:
FROM
clause creates a temporary table (filtered_employees
) with only employees earning more than 50,000.EXISTS
or IN
where possible.