David542
David542

Reputation: 110512

Count up different products

I have the following table:

order_id  product_id
1         102
2         105
3         102
4         96
5         96

How would I get a count of the product_ids. The result I am looking to attain is:

product_id    count
96            2
102           2
105           1

The query I have tried is:

SELECT product_id, sum(product_id) from orders

But this of course produces an incorrect result with only one row. What would be the correct query here?

Upvotes: 1

Views: 177

Answers (2)

echo_Me
echo_Me

Reputation: 37243

try this

 SELECT product_id, count(*) as count  from orders GROUP BY product_id

Upvotes: 0

doitlikejustin
doitlikejustin

Reputation: 6363

SELECT product_id, COUNT(*) FROM orders GROUP BY product_id

Or if you want to name it...

SELECT product_id, COUNT(*) as product_count FROM orders GROUP BY product_id

Upvotes: 3

Related Questions