I have a list of classes and categories for the classes. I would like to display how many classes each category has. Something like this:
category | # of classes
Sports | 12
Fitness | 32
Climbing | 8
The table is set up so that each class is on a line that includes the category.
category | class_name
fitness | Learn to Skate
I have the following SQL, which generates a list of distinct categories:
select
distinct category
from classes
order by category asc
; Output:
; Fitness
; Climbing
; Recreation
; ...
I also want to display how many classes there are. I figured this would be straightforward:
select
distinct category,
count(distinct category)
from classes
order by category asc
; Output:
; Fitness | 11
The output almost works, but it only produces one row. Without the count()
it produces 11 rows, as expected. Why does this only produce one row? How can I achieve this?