customers:
+-----------+-----------+
| cid | name |
+-----------+-----------+
| 1 | a |
| 2 | b |
| 3 | c |
+-----------+-----------+
pizza:
+-----------+-----------+
| pid | type |
+-----------+-----------+
| 1 | sausage |
| 2 | cheese |
| 3 | veggies |
| 4 | sausage |
| 5 | veggies |
| 6 | sausage |
| 7 | sausage |
+-----------+-----------+
orders:
+-----------+-----------+-----------+
| oid | cid | pid |
+-----------+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 3 | 4 |
| 5 | 1 | 5 |
| 6 | 3 | 6 |
| 7 | 3 | 7 |
+-----------+-----------+-----------+
I'm having some trouble wrapping my head around sql logic. How do I find the customers who did not order all 3 types of pizza? The three types are sausage, cheese and veggies. Do I need to use NOT EXIST?
You could use a having clause to look for customers that have ordered less than 3 pizza types:
select c.name
from customers c
join orders c
on c.cid = c.id
join pizza p
on p.id = c.pid
group by
c.name
having
count(distinct p.type) < 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With