You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -231,6 +231,7 @@ Because this project necessitated a framework to enable consistent daily practic
231
231
| 195 |[Game Play Analysis III](https://leetcode.com/problems/game-play-analysis-iii/description/)|[Solution](solutions/195_game_play_analysis_iii.md)| LeetCode | Medium ||
232
232
| 196 |[Winning Candidate](https://leetcode.com/problems/winning-candidate/description/)|[Solution](solutions/196_winning_candidate.md)| LeetCode | Medium ||
| 198 |[Count Student Number in Departments](https://leetcode.com/problems/count-student-number-in-departments/description/)|[Solution](solutions/198_count_student_number_in_departments.md)| LeetCode | Medium ||
234
235
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
Write a solution to report the respective department name and number of students majoring in each department for all departments in the `Department` table (even ones with no current students).
11
+
12
+
Return the result table ordered by `student_number`**in descending order**. In case of a tie, order them by `dept_name`**alphabetically**. [[Full Description](https://leetcode.com/problems/count-student-number-in-departments/description/)]
13
+
14
+
## Submitted Solution
15
+
16
+
```sql
17
+
-- Submitted Solution
18
+
WITH cte AS (
19
+
SELECT
20
+
d.dept_name
21
+
,COUNT(DISTINCT student_id) AS student_number
22
+
FROM Student AS s
23
+
JOIN Department AS d ONs.dept_id=d.dept_id
24
+
GROUP BYd.dept_name
25
+
)
26
+
SELECT
27
+
d.dept_name
28
+
,COALESCE(student_number, 0) AS student_number
29
+
FROM Department AS d
30
+
LEFT JOIN cte AS c ONd.dept_name=c.dept_name
31
+
ORDER BY student_number DESC, d.dept_nameASC
32
+
;
33
+
```
34
+
35
+
## Site Solution
36
+
37
+
```sql
38
+
-- LeetCode Solution
39
+
SELECT
40
+
dept_name, COUNT(student_id) AS student_number
41
+
FROM
42
+
department
43
+
LEFT OUTER JOIN
44
+
student ONdepartment.dept_id=student.dept_id
45
+
GROUP BYdepartment.dept_name
46
+
ORDER BY student_number DESC , department.dept_name
0 commit comments