Correlated subqueries are a powerful concept in SQL that allow dynamic interaction between the inner and outer queries. Unlike regular subqueries, correlated subqueries reference columns from the outer query, creating a dependency that evaluates the subquery for each row processed by the outer query.
A correlated subquery is a subquery that references one or more columns from the outer query. It executes once for every row processed by the outer query.
Correlated subqueries are used when:
SELECT e1.name, e1.salary
FROM employees e1
WHERE e1.salary > (
SELECT AVG(e2.salary)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
e2.salary
) depends on the e1.department_id
from the outer query.Aspect | Correlated Subqueries | Non-Correlated Subqueries |
---|---|---|
Dependency | References columns from the outer query | Executes independently |
Execution | Executes once per row in the outer query | Executes only once, regardless of rows |
Performance | Generally slower due to repeated execution | Faster because of single execution |
SELECT column1, column2
FROM table1 alias1
WHERE column3 OPERATOR (
SELECT aggregate_function(column4)
FROM table2 alias2
WHERE alias2.column5 = alias1.column6
);
Filter rows dynamically based on related table data.
Example: Find employees earning above the average salary of their department:
SELECT name, salary, department_id
FROM employees e1
WHERE salary > (
SELECT AVG(salary)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
Explanation:
Output:
Name | Salary | Department_ID |
---|---|---|
Alice | 75000 | 101 |
Bob | 90000 | 102 |
Find rows based on group-level calculations.
Example: Identify products priced higher than the average price in their category:
SELECT product_name, price, category_id
FROM products p1
WHERE price > (
SELECT AVG(price)
FROM products p2
WHERE p2.category_id = p1.category_id
);
Product_Name | Price | Category_ID |
---|---|---|
Laptop | 1200 | 1 |
Smartphone | 800 | 2 |
Update rows based on related table data.
Example: Increase salaries of employees earning less than the average salary of their department
UPDATE employees e1
SET salary = salary * 1.10
WHERE salary < (
SELECT AVG(salary)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
Delete rows dynamically based on related data.
Example: Remove employees whose salary is the lowest in their department
DELETE FROM employees e1
WHERE salary = (
SELECT MIN(salary)
FROM employees e2
WHERE e2.department_id = e1.department_id
);
Name | Salary | Department_ID |
---|---|---|
John | 30000 | 101 |
Alice | 75000 | 101 |
Name | Salary | Department_ID |
---|---|---|
Alice | 75000 | 101 |
Correlated subqueries execute for every row in the outer query, which can slow performance, especially for large datasets.
Example: Equivalent join for a correlated subquery
SELECT e1.name, e1.salary
FROM employees e1
JOIN (
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
) avg_dept ON e1.department_id = avg_dept.department_id
WHERE e1.salary > avg_dept.avg_salary;
MAX
, MIN
) or ensure unique values.Correlated subqueries are indispensable when querying data dynamically with interdependencies between rows and tables. While they offer flexibility, their row-by-row execution can impact performance. Happy Coding!❤️