Sergey Kozlov
Sergey Kozlov

Reputation: 452

Postgresql select join sort by count

I have two tables.

Goods:

                          Table "public.goods"
     Column      |           Type           |         Modifiers         
-----------------+--------------------------+---------------------------
 goods_id        | character varying        | not null
 goods_owner_id  | character varying        | not null

Sellers:

                          Table "public.sellers"
     Column      |           Type           |         Modifiers         
-----------------+--------------------------+---------------------------
 sellers_id      | character varying        | not null
 sellers_name    | character varying        | not null

How to make a request that will show sellers with the most quantity of goods? Thank you.

Upvotes: 0

Views: 28

Answers (1)

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5573

Here is sql query:

SELECT sellers.sellers_id 
    FROM goods JOIN sellers ON goods.goods_owner_id = sellers.sellers_id
    GROUP BY sellers.sellers_id
    ORDER BY COUNT(goods.goods_id) DESC 
    LIMIT 1 

Upvotes: 1

Related Questions