Sub Queries
Sub Queries
A subquery is a query that is nested within another query. Think of it as a query inside a query.
The subquery runs first and provides its result to the outer query. This allows you to perform
more complex data retrieval operations, which would otherwise require multiple steps or more
complicated joins.
Subqueries can be placed in different parts of a SQL query:
WHERE Clause – for filtering records based on dynamic values.
SELECT Clause – for retrieving data calculated from another query.
FROM Clause – when you need to use the result of a subquery as a virtual table.
HAVING Clause – for filtering results after applying aggregate functions.
We will use two tables, departments and employees, for our subquery.
Employees Table – Contains employee data, including names, salaries, and their respective
department IDs.
INSERT INTO employees (name, salary, department_id) VALUES ('John', 50000, 1),
('Jane', 70000, 2), ('Sam', 60000, 3), ('Sara', 80000, 2),('Mike', 75000, 4);
General Syntax of Subqueries
Subqueries can be used in several parts of a SQL query, and here’s a look at the general syntax
for each scenario:
General Syntax:
SELECT column_name FROM table_name WHERE column_name operator (SELECT
column_name FROM another_table WHERE condition);
Find the names of employees whose salary is greater than the average salary of all employees.
SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
The subquery (SELECT AVG(salary) FROM employees) calculates the average salary of all
employees.
The outer query filters the employees whose salary is greater than the average calculated by
the subquery.
The SELECT clause subquery allows you to retrieve additional data or perform calculations for
each row selected by the outer query.
General Syntax:
SELECT column_name, (SELECT column_name FROM another_table WHERE condition) AS
alias FROM table_name;
In the FROM clause, the subquery acts as a virtual table, and the outer query joins it.
General Syntax:
SELECT column_name FROM (SELECT column_name FROM table_name WHERE condition)
AS alias;
Find the average salary in each department and display the department name along with its
average salary.
4. Correlated Subquery
A correlated subquery references a column from the outer query. This means the subquery is
evaluated for each row in the outer query.
General Syntax:
SELECT column_name FROM table_name outer_alias WHERE column_name operator
(SELECT column_name FROM another_table inner_alias WHERE condition =
outer_alias.column_name);
Find the names of employees whose salary is greater than the average salary within their
respective departments.
SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM
employees WHERE department_id = e.department_id);
Find all employees who work in departments where at least one employee earns more than
70,000.
The subquery (SELECT department_id FROM employees WHERE salary > 70000) returns the
department IDs where any employee has a salary greater than $70,000.
The outer query then selects all employees who belong to those departments.