I have a correlated subquery like this (from BOL):
SELECT DISTINCT c.LastName, c.FirstName, e.BusinessEntityID
FROM Person.Person AS c JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = c.BusinessEntityID
WHERE 5000.00 IN
(SELECT Bonus
FROM Sales.SalesPerson sp
WHERE e.BusinessEntityID = sp.BusinessEntityID) ;
GO
When I rewrite this query using joins
select c.LastName, c.FirstName, e.BusinessEntityID, d.Bonus
from Person.Person as c
inner join HumanResources.Employee as e on e.BusinessEntityID = c.BusinessEntityID
inner join Sales.SalesPerson as d on d.BusinessEntityID = c.BusinessEntityID
where Bonus = 5000.00
And look the actual execution plan, it looks exactly the same in both queries. Why? I was thinking that correlated subquery is much slower because of the nested loop and the execution plan looks different? Is it because there is not much data in these tables?
Best Answer
The two queries are logically identical and do produce the same plan. The simplification phase of the Query Optimizer handles this.
They're identical because of the constraints that are on the tables - foreign keys, uniqueness, nullability...