← Back to Joins
Question 106
Which Cities Each Department Reaches
Which Cities Each Department Reaches
For each department, count the distinct students who have ever enrolled in one of its courses (dropped rows count too) and the distinct home cities those students come from. Returndepartment(thename, aliased),studentsandcities. Fine Arts has a course nobody takes and must appear with 0 and 0. Sort bystudentsdescending, thendepartment.
Expected result:
+------------------+----------+--------+ | department | students | cities | +------------------+----------+--------+ | Mathematics | 14 | 8 | | Computer Science | 13 | 4 | | English | 12 | 6 | | Business | 10 | 5 | | Biology | 5 | 3 | | Fine Arts | 0 | 0 | +------------------+----------+--------+ 6 rows
Hints4 available
Sign in to use hintsTopics:
Joins
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.