← Back to Window Functions
Question 57
Each Department's Share of the Students
Each Department's Share of the Students
Report how the student body splits across majors: returndepartment(the department name, orUndeclaredfor the four students with no major),students(how many) andpct_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 bystudentsdescending, thendepartment.
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
Sign in to use hintsTopics:
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.