Javilingys
Javilingys

Reputation: 23

How to make this query on PostgreSQL?

I have three tables:

Customer table

enter image description here

Product Table

enter image description here

Customer_Product Table

enter image description here

I need to make query: Search for customers who bought a certain product (for example, name = "toilet paper") at least 2 times I don't understand how.. I'm noob in databases.. Please help

Upvotes: 1

Views: 36

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50173

You need aggregation :

select c.name
from customer c inner join
     customer_product cp
     on cp.customer_id = c.customer_id inner join
     product p
     on p.product_id = cp.product_id
where p.lable = 'toilet paper'
group by c.name
having count(p.lable) > 1;

Upvotes: 1

Related Questions