1

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?

David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

3
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
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
0

try this

 SELECT product_id, count(*) as count  from orders GROUP BY product_id
echo_Me
  • 37,078
  • 5
  • 58
  • 78