Try this
SELECT CompanyName
FROM Customers
WHERE Country='USA' AND
Region NOT IN
(SELECT DISTINCT Region
FROM Employees
WHERE Region IS NOT NULL);
Output
CompanyName
----------------------------------------
Great Lakes Food Market
Hungry Coyote Import Store
Let's Stop N Shop
Lonesome Pine Restaurant
Old World Delicatessen
Rattlesnake Canyon Grocery
Save-a-lot Markets
Split Rail Beer & Ale
The Big Cheese
The Cracker Box
(10 row(s) affected)
In your your question you said that you need Company Name of all U.S.-based customers who are NOT located in the same state (or region) as any of the employees. But in your query you were trying to get regions from Customers table instead.
UPDATE: I believe that you know that but just in case:
The explicit check for NULL
values in the subquery (WHERE Region IS NOT NULL
) is crucial. Without it the query will run successfully but won't return any rows.
SELECT CompanyName
FROM Customers
WHERE Country='USA' AND
Region NOT IN
(SELECT DISTINCT Region
FROM Employees);
Output:
CompanyName
----------------------------------------
(0 row(s) affected)
I guess that exercise that you were trying to accomplish was meant to make sure that you know about NULLs in subqueries.