SQL Count amount of items in a few different groups

Let's say I have the following data:

Food
----
Taco
Taco
Taco
Pasta
Pasta
Egg

And I want the following result:

Food  | Count
------|-------
Taco  |   3
Pasta |   2
Egg   |   1

How would I achieve this using SQL? I'm assuming I use count functions, but I only know how to count everything:

SELECT food, COUNT(food)
FROM menus 

1 answer

  • answered 2018-10-11 19:53 juergen d

    If you group your data then aggregation functions like count() apply to each unique value in the group and not the complete result set

    SELECT food, COUNT(*)
    FROM menus 
    group by food