Skip to content
Advertisement

SELECT id HAVING maximum count of id

Have a products table with item_id and color_id. I’m trying to get the color_id with the most non-null instances.

This fails:

with

This

Returns

I am looking for color_id 3, which has the most instances.

Is there a quick and easy way of getting what I want without 2 queries?

Advertisement

Answer

This will give you the color_id and the count on that color_id ordered by the count from greatest to least. I think this is what you want.


for your edit…

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement