sql - Return records from a table which have a match on all records of another table -


having following 3 tables in dbms:

customer(id, name, city), product(id, name, price), orders(cust_id, prod_id, date) 

what query fetches customers (if any) have ordered all products?

select c.id customer c inner join orders o on o.cust_id = c.id inner join product p on p.id = o.prod_id group c.id having count(distinct p.id) = (select count(id) product) 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -