Reputation: 110512
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_id
s. 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
Reputation: 37243
try this
SELECT product_id, count(*) as count from orders GROUP BY product_id
Upvotes: 0
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