Hi Friends,
In a multi-valued, self-conatined subquery, the inner query (also called as the inner select) returns multiple values. The outer query consumes and validates the list of values returned by the subquery using IN clause. Subqueries can also be nested. Here is an example:-
Use AdventureWorks; GO SELECT LastName, FirstName FROM Person.Contact WHERE ContactID IN (SELECT ContactID FROM HumanResources.Employee WHERE EmployeeID IN (SELECT SalesPersonID FROM Sales.SalesPerson)) Go
The innermost query returns the sales person IDs. The query at the next higher level is evaluated with these sales person IDs and returns the contact ID numbers of the employees. Finally, the outer query uses the contact IDs to find the names of the employees.
The above query can also be written as a join:
Use AdventureWorks; GO SELECT LastName, FirstName FROM Person.Contact c INNER JOIN HumanResources.Employee e ON c.ContactID = e.ContactID JOIN Sales.SalesPerson s ON e.EmployeeID = s.SalesPersonID GO
So friends, what about performance? Run both the queries as a batch and see the relative costs. Which one you think performs better? Comments?