More Business Intelligence Methods
From the previous lesson, we needed to find what products haven't been ordered yet. What about customers that haven't ordered anything?
select
id,
name
from
customers
where
-- find which customers are not in the list returned by this inner select
id not in (
select
c.id
from
customerOrders co
join
customers c
on
co.customerId = c.id
group by
c.id
);
We also need to use group by in our query above because one customer could have multiple customerOrders. We don't want to return any duplicates.
We're introducing a new type of join here. The join key word used in this select works a lot like doing a join when we use where id=id. This is yet another way of doing it.
We use this join to try to connect any customers to customerOrders. The results are the same when returned back to the main outer select. We get a list of ids back from the inner select, which is then fed into the outer main select that then retrieves any records that are not in the set.