Skip to content

Commit f1f56bc

Browse files
authored
feat: update sql solutions to lc problems (doocs#1187)
* No.0577.Employee Bonus * No.0578.Get Highest Answer Rate Question * No.0579.Find Cumulative Salary of an Employee * No.0584.Find Customer Referee
1 parent d0c5c7d commit f1f56bc

File tree

15 files changed

+175
-92
lines changed

15 files changed

+175
-92
lines changed

solution/0500-0599/0577.Employee Bonus/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ empId 是这张表单的主关键字
5252
### **SQL**
5353

5454
```sql
55-
SELECT
56-
e.name,
57-
b.bonus
55+
# Write your MySQL query statement below
56+
SELECT name, bonus
5857
FROM
59-
Employee AS e
60-
LEFT JOIN Bonus AS b ON e.empid = b.empid
61-
WHERE b.bonus < 1000 OR b.bonus IS NULL;
58+
Employee
59+
LEFT JOIN Bonus USING (empId)
60+
WHERE ifnull(bonus, 0) < 1000;
6261
```
6362

6463
<!-- tabs:end -->

solution/0500-0599/0577.Employee Bonus/README_EN.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ Bonus table:
8181
### **SQL**
8282

8383
```sql
84-
SELECT
85-
e.name,
86-
b.bonus
84+
# Write your MySQL query statement below
85+
SELECT name, bonus
8786
FROM
88-
Employee AS e
89-
LEFT JOIN Bonus AS b ON e.empid = b.empid
90-
WHERE b.bonus < 1000 OR b.bonus IS NULL;
87+
Employee
88+
LEFT JOIN Bonus USING (empId)
89+
WHERE ifnull(bonus, 0) < 1000;
9190
```
9291

9392
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
SELECT
2-
e.name,
3-
b.bonus
1+
# Write your MySQL query statement below
2+
SELECT name, bonus
43
FROM
5-
Employee AS e
6-
LEFT JOIN Bonus AS b ON e.empid = b.empid
7-
WHERE b.bonus < 1000 OR b.bonus IS NULL;
4+
Employee
5+
LEFT JOIN Bonus USING (empId)
6+
WHERE ifnull(bonus, 0) < 1000;

solution/0500-0599/0578.Get Highest Answer Rate Question/README.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,28 @@ SurveyLog table:
7373
### **SQL**
7474

7575
```sql
76+
# Write your MySQL query statement below
7677
SELECT question_id AS survey_log
77-
FROM SurveyLog
78-
GROUP BY 1
79-
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
80-
LIMIT 1;
78+
FROM SurveyLog
79+
GROUP BY 1
80+
ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1
81+
LIMIT 1;
82+
```
83+
84+
```sql
85+
WITH
86+
T AS (
87+
SELECT
88+
question_id AS survey_log,
89+
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
90+
sum(action = 'show') OVER (PARTITION BY question_id)
91+
) AS ratio
92+
FROM SurveyLog
93+
)
94+
SELECT survey_log
95+
FROM T
96+
ORDER BY ratio DESC, 1
97+
LIMIT 1;
8198
```
8299

83100
<!-- tabs:end -->

solution/0500-0599/0578.Get Highest Answer Rate Question/README_EN.md

+21-4
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,28 @@ Question 285 has the highest answer rate.</pre>
6464
### **SQL**
6565

6666
```sql
67+
# Write your MySQL query statement below
6768
SELECT question_id AS survey_log
68-
FROM SurveyLog
69-
GROUP BY 1
70-
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
71-
LIMIT 1;
69+
FROM SurveyLog
70+
GROUP BY 1
71+
ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1
72+
LIMIT 1;
73+
```
74+
75+
```sql
76+
WITH
77+
T AS (
78+
SELECT
79+
question_id AS survey_log,
80+
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
81+
sum(action = 'show') OVER (PARTITION BY question_id)
82+
) AS ratio
83+
FROM SurveyLog
84+
)
85+
SELECT survey_log
86+
FROM T
87+
ORDER BY ratio DESC, 1
88+
LIMIT 1;
7289
```
7390

7491
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
SELECT question_id AS survey_log
2-
FROM SurveyLog
3-
GROUP BY 1
4-
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
1+
WITH
2+
T AS (
3+
SELECT
4+
question_id AS survey_log,
5+
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
6+
sum(action = 'show') OVER (PARTITION BY question_id)
7+
) AS ratio
8+
FROM SurveyLog
9+
)
10+
SELECT survey_log
11+
FROM T
12+
ORDER BY ratio DESC, 1
513
LIMIT 1;

solution/0500-0599/0579.Find Cumulative Salary of an Employee/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,28 @@ WHERE
109109
ORDER BY id, month DESC;
110110
```
111111

112+
```sql
113+
# Write your MySQL query statement below
114+
WITH
115+
T AS (
116+
SELECT
117+
id,
118+
month,
119+
sum(salary) OVER (
120+
PARTITION BY id
121+
ORDER BY month
122+
RANGE 2 PRECEDING
123+
) AS salary,
124+
rank() OVER (
125+
PARTITION BY id
126+
ORDER BY month DESC
127+
) AS rk
128+
FROM Employee
129+
)
130+
SELECT id, month, salary
131+
FROM T
132+
WHERE rk > 1
133+
ORDER BY 1, 2 DESC;
134+
```
135+
112136
<!-- tabs:end -->

solution/0500-0599/0579.Find Cumulative Salary of an Employee/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,28 @@ WHERE
134134
ORDER BY id, month DESC;
135135
```
136136

137+
```sql
138+
# Write your MySQL query statement below
139+
WITH
140+
T AS (
141+
SELECT
142+
id,
143+
month,
144+
sum(salary) OVER (
145+
PARTITION BY id
146+
ORDER BY month
147+
RANGE 2 PRECEDING
148+
) AS salary,
149+
rank() OVER (
150+
PARTITION BY id
151+
ORDER BY month DESC
152+
) AS rk
153+
FROM Employee
154+
)
155+
SELECT id, month, salary
156+
FROM T
157+
WHERE rk > 1
158+
ORDER BY 1, 2 DESC;
159+
```
160+
137161
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# Write your MySQL query statement below
2-
SELECT
3-
id,
4-
month,
5-
sum(salary) OVER (
6-
PARTITION BY id
7-
ORDER BY month
8-
RANGE 2 PRECEDING
9-
) AS Salary
10-
FROM employee
11-
WHERE
12-
(id, month) NOT IN (
2+
WITH
3+
T AS (
134
SELECT
145
id,
15-
max(month)
6+
month,
7+
sum(salary) OVER (
8+
PARTITION BY id
9+
ORDER BY month
10+
RANGE 2 PRECEDING
11+
) AS salary,
12+
rank() OVER (
13+
PARTITION BY id
14+
ORDER BY month DESC
15+
) AS rk
1616
FROM Employee
17-
GROUP BY id
1817
)
19-
ORDER BY id, month DESC;
18+
SELECT id, month, salary
19+
FROM T
20+
WHERE rk > 1
21+
ORDER BY 1, 2 DESC;

solution/0500-0599/0580.Count Student Number in Departments/README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,18 @@ Department 表:
8484
### **SQL**
8585

8686
```sql
87-
SELECT
88-
department.dept_name,
89-
COUNT(student.dept_id) AS student_number
87+
# Write your MySQL query statement below
88+
WITH
89+
S AS (
90+
SELECT dept_id, count(1) AS cnt
91+
FROM Student
92+
GROUP BY dept_id
93+
)
94+
SELECT dept_name, ifnull(cnt, 0) AS student_number
9095
FROM
91-
Student
92-
RIGHT JOIN Department ON student.dept_id = department.dept_id
93-
GROUP BY dept_name
94-
ORDER BY student_number DESC, dept_name;
96+
S
97+
RIGHT JOIN Department USING (dept_id)
98+
ORDER BY 2 DESC, 1;
9599
```
96100

97101
<!-- tabs:end -->

solution/0500-0599/0580.Count Student Number in Departments/README_EN.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ Department table:
8181
### **SQL**
8282

8383
```sql
84-
SELECT
85-
department.dept_name,
86-
COUNT(student.dept_id) AS student_number
84+
# Write your MySQL query statement below
85+
WITH
86+
S AS (
87+
SELECT dept_id, count(1) AS cnt
88+
FROM Student
89+
GROUP BY dept_id
90+
)
91+
SELECT dept_name, ifnull(cnt, 0) AS student_number
8792
FROM
88-
Student
89-
RIGHT JOIN Department ON student.dept_id = department.dept_id
90-
GROUP BY dept_name
91-
ORDER BY student_number DESC, dept_name;
93+
S
94+
RIGHT JOIN Department USING (dept_id)
95+
ORDER BY 2 DESC, 1;
9296
```
9397

9498
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
SELECT
2-
department.dept_name,
3-
COUNT(student.dept_id) AS student_number
1+
# Write your MySQL query statement below
2+
WITH
3+
S AS (
4+
SELECT dept_id, count(1) AS cnt
5+
FROM Student
6+
GROUP BY dept_id
7+
)
8+
SELECT dept_name, ifnull(cnt, 0) AS student_number
49
FROM
5-
Student
6-
RIGHT JOIN Department ON student.dept_id = department.dept_id
7-
GROUP BY dept_name
8-
ORDER BY student_number DESC, dept_name;
10+
S
11+
RIGHT JOIN Department USING (dept_id)
12+
ORDER BY 2 DESC, 1;

solution/0500-0599/0584.Find Customer Referee/README.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,10 @@
4545
### **SQL**
4646

4747
```sql
48-
SELECT
49-
name
48+
# Write your MySQL query statement below
49+
SELECT name
5050
FROM Customer
51-
WHERE referee_id != 2 OR referee_id IS NULL;
52-
```
53-
54-
MySQL 可使用 `IFNULL()`
55-
56-
```sql
57-
SELECT
58-
name
59-
FROM customer
60-
WHERE IFNULL(referee_id, 0) != 2;
51+
WHERE ifnull(referee_id, 0) != 2;
6152
```
6253

6354
<!-- tabs:end -->

solution/0500-0599/0584.Find Customer Referee/README_EN.md

+3-12
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,10 @@ Customer table:
6060
### **SQL**
6161

6262
```sql
63-
SELECT
64-
name
63+
# Write your MySQL query statement below
64+
SELECT name
6565
FROM Customer
66-
WHERE referee_id != 2 OR referee_id IS NULL;
67-
```
68-
69-
MySQL can use `IFNULL()`:
70-
71-
```sql
72-
SELECT
73-
name
74-
FROM customer
75-
WHERE IFNULL(referee_id, 0) != 2;
66+
WHERE ifnull(referee_id, 0) != 2;
7667
```
7768

7869
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SELECT
2-
name
1+
# Write your MySQL query statement below
2+
SELECT name
33
FROM Customer
4-
WHERE referee_id != 2 OR referee_id IS NULL;
4+
WHERE ifnull(referee_id, 0) != 2;

0 commit comments

Comments
 (0)