You are given two tables: Student and Department.
The Student table contains information about students including their ID, name, gender, and department ID. The Department table contains department information with department ID and name.
Write a SQL query to report the department name and number of students in each department for all departments, even those with no current students.
Requirements:
- Include all departments from the Department table
- Count the number of students in each department
- Order results by student count in descending order
- If counts are equal, order by department name alphabetically
Table Schema
| Column Name | Type | Description |
|---|---|---|
student_id
PK
|
int | Primary key, unique student identifier |
student_name
|
varchar | Name of the student |
gender
|
varchar | Gender of the student |
dept_id
|
int | Foreign key referencing Department table |
| Column Name | Type | Description |
|---|---|---|
dept_id
PK
|
int | Primary key, unique department identifier |
dept_name
|
varchar | Name of the department |
Input & Output
| student_id | student_name | gender | dept_id |
|---|---|---|---|
| 1 | Jack | M | 1 |
| 2 | Jane | F | 1 |
| 3 | Mark | M | 2 |
| dept_id | dept_name |
|---|---|
| 1 | Engineering |
| 2 | Science |
| 3 | Law |
| dept_name | student_number |
|---|---|
| Engineering | 2 |
| Science | 1 |
| Law | 0 |
Engineering has 2 students (Jack and Jane), Science has 1 student (Mark), and Law has 0 students. Results are ordered by student count descending: 2, 1, 0.
| student_id | student_name | gender | dept_id |
|---|---|---|---|
| 1 | Alice | F | 2 |
| 2 | Bob | M | 3 |
| dept_id | dept_name |
|---|---|
| 1 | Mathematics |
| 2 | Physics |
| 3 | Chemistry |
| dept_name | student_number |
|---|---|
| Chemistry | 1 |
| Physics | 1 |
| Mathematics | 0 |
Chemistry and Physics both have 1 student each. Since counts are equal, they are ordered alphabetically: Chemistry comes before Physics. Mathematics has 0 students.
| student_id | student_name | gender | dept_id |
|---|
| dept_id | dept_name |
|---|---|
| 1 | Art |
| 2 | Music |
| dept_name | student_number |
|---|---|
| Art | 0 |
| Music | 0 |
No students exist in the Student table. All departments have 0 students and are ordered alphabetically: Art, Music.
Constraints
-
1 ≤ student_id, dept_id ≤ 1000 -
student_nameanddept_nameconsist of English letters -
genderis either'M'or'F' - Each department in Student table exists in Department table