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
Post a Comment