MySQL Count and Group By

Last updated on

If you have a table called colors:

id         color
----------------
1          red
2          red
3          green
3          green
4          green
5          blue

And want to group and count all colors to create this:

color      count
----------------
red        2
green      3
blue       1

Try the following.

SELECT color, COUNT( * ) 
FROM colors
GROUP BY color
ORDER BY COUNT( * ) DESC 

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

Your email address will not be published. Required fields are marked *