Web Analytics Made Easy - Statcounter

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

  1. Dependency:
    • The subquery depends on the outer query for its values.
  2. Execution:
    • The subquery is re-evaluated for every row processed by the outer query.
  3. Performance:
    • Can be less efficient than joins or regular subqueries due to repeated execution.

Syntax

Example Scenario

You have two tables:

  1. Employees: Contains employee details.
  2. Departments: Contains department details.

You want to find employees whose salary is greater than the average salary of their respective departments.

Table Structures

Employees:

EmployeeIDNameSalaryDepartmentID
1Alice900001
2Bob750001
3Charlie800002
4David720002

Departments:

DepartmentIDDepartmentName
1HR
2IT

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

  1. For each row in the Employees table:
    • The subquery calculates the average salary for the department (DepartmentID = e.DepartmentID).
  2. The outer query compares the employee’s salary with this average.
  3. Only employees meeting the condition are returned.

Result

NameSalaryDepartmentName
Alice90000HR

Use Cases of Correlated Subqueries

  1. Row-specific Aggregations:
    • Compare a row’s value with an aggregate computed over a subset of rows.
  2. Filtering Data:
    • Identify rows based on conditions involving other rows.
  3. 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.

By vivekjohari

I am currently working as a Senior Database Professional and have around 18 years of experience in database. Degree:- Master Degree in Computer(MCA) Certification course in Data Science & Machine Learning from Indian Institute of Technology (IIT), Delhi Work experience:- Designing of the database. Database Optimization. Writing Complex Stored Procedures,Functions,Triggers etc. Designing and developing SSIS & DTS packages. Designing SQL Reports using SSRS. Database Server Maintenance. Certification:- MCTS: DA-100: Analysing Data with Microsoft Power BI MCTS: DP-300: Administering Relational Databases on Microsoft Azure Microsoft certified Sql DBA in Sql server 2008 (MCTS). Microsoft certified BI professional in Sql server 2008 (MCTS). Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:- Technologies with Vivek Johari Guest Author and Blogger at sqlservercentral.com

Leave a Reply

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading