I was practicing some SQL when this hit me. I wanted to see how many times a certain commodity came up and from there get the commodity which came up the most.
This shows how many times each commodity comes up:
mysql> SELECT commodity, COUNT(commodity) count FROM orders GROUP BY commodity ORDER BY count;
+----------------------+------------+
| commodity | count |
+----------------------+------------+
| PERSIAN MELON | 4 |
| BEANS | 6 |
| CASABA | 10 |
| ASPARAGUS | 11 |
| EGGPLANT | 12 |
| TOMATOES, CHERRY | 16 |
| GALIA MELON | 18 |
+-----------------------------------+
I’m trying to get the row with the highest but it’s all wrong:
mysql> SELECT commodity, MAX(COUNT(commodity)) count FROM orders GROUP BY commodity ORDER BY count;
What’s the right way of doing this?
CAUTION: the query will not handle duplicate records having the maximum
COUNTBut this will,