T-SQL Tutorial

Correlated Subquery


In SQL Server, a correlated subquery is a type of subquery that uses values from the outer query in its own query. This means that the subquery is evaluated for each row returned by the outer query, and the result of the subquery is dependent on the values of the current row in the outer query.

Correlated subqueries are useful when you need to perform a query that requires information from two or more related tables. For example, if you want to retrieve all the customers who have placed an order for a particular product, you might use a correlated subquery to link the Orders table with the Customers table.


First example

Here is an example of a correlated subquery:

SELECT CustomerName
FROM Customers
WHERE EXISTS (
SELECT *
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID
AND Orders.ProductName = 'Widget'
)

In the first example, the subquery is correlated with the outer query by the CustomerID column. The subquery checks whether there is at least one order for the 'Widget' product for each customer in the Customers table. If there is, the CustomerName is returned in the result set.


Second example

SELECT *
FROM Orders o
WHERE o.OrderDate = (
SELECT MAX(OrderDate)
FROM Orders
WHERE CustomerID = o.CustomerID
);

In the second example, the outer query selects all columns from the Orders table where the OrderDate matches the result of the subquery. The subquery returns the maximum OrderDate for the current row's CustomerID in the outer query. The subquery is dependent on the CustomerID value from the outer query, making it a correlated subquery.

Correlated subqueries can be more resource-intensive than non-correlated subqueries because they must be executed for each row returned by the outer query. Therefore, it's important to optimize your query to ensure that it runs efficiently. One way to do this is to use indexes on the columns used in the subquery to speed up the query execution.

Correlated subqueries can be useful for performing complex calculations and filtering operations on data in SQL Server. However, they can also be performance-intensive, so it's important to optimize queries that use correlated subqueries to ensure efficient execution.