Skip to content

Commit 977818b

Browse files
committed
Upload 198
1 parent 38332cc commit 977818b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-197-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-198-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -231,6 +231,7 @@ Because this project necessitated a framework to enable consistent daily practic
231231
| 195 | [Game Play Analysis III](https://leetcode.com/problems/game-play-analysis-iii/description/) | [Solution](solutions/195_game_play_analysis_iii.md) | LeetCode | Medium | |
232232
| 196 | [Winning Candidate](https://leetcode.com/problems/winning-candidate/description/) | [Solution](solutions/196_winning_candidate.md) | LeetCode | Medium | |
233233
| 197 | [Get Highest Answer Rate Question](https://leetcode.com/problems/get-highest-answer-rate-question/) | [Solution](solutions/197_get_highest_answer_rate_question.md) | LeetCode | Medium | |
234+
| 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 | |
234235
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
235236
<!--- cSpell:enable --->
236237

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SQL Everyday \#198
2+
3+
## Count Student Number in Departments
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
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 ON s.dept_id = d.dept_id
24+
GROUP BY d.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 ON d.dept_name = c.dept_name
31+
ORDER BY student_number DESC, d.dept_name ASC
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 ON department.dept_id = student.dept_id
45+
GROUP BY department.dept_name
46+
ORDER BY student_number DESC , department.dept_name
47+
;
48+
```
49+
50+
## Notes
51+
52+
TBD
53+
54+
## NB
55+
56+
TBD
57+
58+
Go to [Index](../?tab=readme-ov-file#index)\
59+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)