Correlated Subqueries in SQL
A correlated subquery is a subquery that references columns from the outer query. A regular subquery is executed once, and its results are used by the outer query. In contrast, a correlated subquery is executed repeatedly for each row in the outer query.
Key Characteristics
- Dependency:
- The subquery depends on the outer query for its values.
- Execution:
- The subquery is re-evaluated for every row processed by the outer query.
- Performance:
- Can be less efficient than joins or regular subqueries due to repeated execution.
Syntax
Example Scenario
You have two tables:
- Employees: Contains employee details.
- Departments: Contains department details.
You want to find employees whose salary is greater than the average salary of their respective departments.
Table Structures
Employees:
EmployeeID | Name | Salary | DepartmentID |
---|---|---|---|
1 | Alice | 90000 | 1 |
2 | Bob | 75000 | 1 |
3 | Charlie | 80000 | 2 |
4 | David | 72000 | 2 |
Departments:
DepartmentID | DepartmentName |
---|---|
1 | HR |
2 | IT |
Correlated Subquery
SELECT e.Name, e.Salary, d.DepartmentName FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID WHERE e.Salary > ( SELECT AVG(Salary) FROM Employees WHERE DepartmentID = e.DepartmentID );
How It Works
- For each row in the
Employees
table:- The subquery calculates the average salary for the department (
DepartmentID = e.DepartmentID
).
- The subquery calculates the average salary for the department (
- The outer query compares the employee’s salary with this average.
- Only employees meeting the condition are returned.
Result
Name | Salary | DepartmentName |
---|---|---|
Alice | 90000 | HR |
Use Cases of Correlated Subqueries
- Row-specific Aggregations:
- Compare a row’s value with an aggregate computed over a subset of rows.
- Filtering Data:
- Identify rows based on conditions involving other rows.
- Complex Relationships:
- Handle relationships not easily modeled using joins.
Performance Considerations
- Correlated subqueries can be slower than joins or non-correlated subqueries because they execute for each row of the outer query.
- Optimization Tips:
- Rewrite using joins when possible.
- Use indexes to improve the performance of the subquery.
Conclusion
Correlated subqueries are a powerful SQL feature for handling row-specific comparisons and complex conditions. While they can simplify logic for certain queries, careful use is required to manage performance, especially on large datasets.
Discover more from Technology with Vivek Johari
Subscribe to get the latest posts sent to your email.