laika222
10/28/2016 - 9:37 PM

EXISTS allows you to execute a query if a subquery returns any result. Lines 1 and 2 show the query to be executed, if the subquery in lines

EXISTS allows you to execute a query if a subquery returns any result. Lines 1 and 2 show the query to be executed, if the subquery in lines 4-6 returns any results. If the subquery returns any number of results, the main query in lines 1-2 executes for those customers returned in the subquery (nothing is pulled for the customers not returned by the subquery). If this subquery returns no results, then nothing is returned.

SELECT LastName, FirstName
FROM Customers
WHERE EXISTS
  (SELECT CustomerID 
  FROM Orders
	WHERE Orders.CustomerID=Customers.CustomerID);