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.

p.s. I increased my AdSense revenue by 200% using AI 🤖. Read my Ezoic review to find out how.

Leave a reply

Your email address will not be published.