Majors Headcount
Faculty budgets at Fairhaven University are allocated per subject, so before the dean can argue for another lecturer she needs the shape of the student body: how many students each major is carrying. The student table holds one row per person with their major written on it, so the majors are there — just scattered across the rows rather than listed anywhere.
Two majors may well be carrying the same number of students. The dean reads this list into the minutes, so the sequence has to come out the same way every time it is run rather than depending on which of the two the engine happened to reach first.
students — one row per enrolled student. gpa is the grade point average already on file for that student, on a 0-4 scale.
| id | name | major | gpa |
|---|---|---|---|
| 1 | Nora Fischer | Computer Science | 3.8 |
| 2 | Omar Haddad | Statistics | 3.4 |
| 3 | Petra Novakova | Computer Science | 3.9 |
| 4 | Quinn Walsh | Mathematics | 3.1 |
| 5 | Ravi Shah | Statistics | 3.6 |
The table already exists in the database — there is nothing to create or load.
Task: Write a query that returns one row per major, with major and a column n holding how many students that major has. Busiest major first; if two majors carry the same number of students, the one whose name comes first alphabetically goes above the other.
Example output
Shape only — these majors and headcounts are invented:
| major | n |
|---|---|
| Physics | 9 |
| Chemistry | 4 |
| Economics | 4 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in