CJCoding With Joseph
15free / daySign inCreate account

Each Department's Share of the Students

Report how the student body splits across majors: return department (the department name, or Undeclared for the four students with no major), students (how many) and pct_of_total (that count as a percentage of all 30 students, rounded to 1 place). Compute the percentage with a window aggregate over the grouped result, SUM(COUNT(*)) OVER (), rather than a subquery. Sort by students descending, then department.

Expected result:

+------------------+----------+--------------+
| department       | students | pct_of_total |
+------------------+----------+--------------+
| Computer Science |        8 |         26.7 |
| Mathematics      |        5 |         16.7 |
| Biology          |        4 |         13.3 |
| Business         |        4 |         13.3 |
| English          |        4 |         13.3 |
| Undeclared       |        4 |         13.3 |
| Fine Arts        |        1 |          3.3 |
+------------------+----------+--------------+
7 rows
Hints4 available
Topics:
Window Functions
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (sel, ins, desc)

Results

Run your SQL to see every statement's output here, then how it compares with the expected answer.

Not sure what is in a table? SHOW TABLES lists them and DESCRIBE students shows the columns — or open the database browser on the left.