O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

COUNT

  1. Find the count of male and female employees:
mysql> SELECT gender, COUNT(*) AS count FROM employees GROUP BY gender;+--------+--------+| gender | count  |+--------+--------+| M      | 179973 || F      | 120051 |+--------+--------+2 rows in set (0.14 sec)
  1. You want to find the 10 most common first names of the employees. You can use GROUP BY first_name to group all the first names, then COUNT(first_name) to find the count inside the group, and finally the ORDER BY count to sort the results. LIMIT these results to the top 10:
mysql> SELECT first_name, COUNT(first_name) AS count FROM employees GROUP BY first_name ORDER BY count DESC LIMIT 10;+-------------+-------+| first_name  | count |+-------------+-------+| Shahab      |   295 || Tetsushi | 291 | ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required