Reputation: 11
I have two tables in SQL, one with information on customers and orders placed by them (columns include customerid, contactname, orderid, quantity, to name a few). My second table is simply a list of all customer ids, and my task is to determine which customerID didn't make a purchase. Some of the customer ids made multiple purchases, so I am unsure of how to use SELECT DISTINCT to compare the two tables.
Upvotes: 1
Views: 194
Reputation: 521
join the second table and filter the results
SELECT DISTINCT t1.customerid, t1.contactname
FROM table1 t1
JOIN table2 t2
ON t1.customerid = t2.customerid
WHERE t1.customerid = t2.customerid
Upvotes: 1
Reputation: 1270401
Use not exists
:
select t2.customerid
from table2 t2
where not exists (select 1 from table1 t1 where t1.customerid = t2.customerid);
Upvotes: 0