File tree 15 files changed +175
-92
lines changed
0578.Get Highest Answer Rate Question
0579.Find Cumulative Salary of an Employee
0580.Count Student Number in Departments
0584.Find Customer Referee
15 files changed +175
-92
lines changed Original file line number Diff line number Diff line change @@ -52,13 +52,12 @@ empId 是这张表单的主关键字
52
52
### ** SQL**
53
53
54
54
``` sql
55
- SELECT
56
- e .name ,
57
- b .bonus
55
+ # Write your MySQL query statement below
56
+ SELECT name, bonus
58
57
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 ;
62
61
```
63
62
64
63
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -81,13 +81,12 @@ Bonus table:
81
81
### ** SQL**
82
82
83
83
``` sql
84
- SELECT
85
- e .name ,
86
- b .bonus
84
+ # Write your MySQL query statement below
85
+ SELECT name, bonus
87
86
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 ;
91
90
```
92
91
93
92
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
- SELECT
2
- e .name ,
3
- b .bonus
1
+ # Write your MySQL query statement below
2
+ SELECT name, bonus
4
3
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 ;
Original file line number Diff line number Diff line change @@ -73,11 +73,28 @@ SurveyLog table:
73
73
### ** SQL**
74
74
75
75
``` sql
76
+ # Write your MySQL query statement below
76
77
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 ;
81
98
```
82
99
83
100
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -64,11 +64,28 @@ Question 285 has the highest answer rate.</pre>
64
64
### ** SQL**
65
65
66
66
``` sql
67
+ # Write your MySQL query statement below
67
68
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 ;
72
89
```
73
90
74
91
<!-- tabs:end -->
Original file line number Diff line number Diff line change 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
5
13
LIMIT 1 ;
Original file line number Diff line number Diff line change @@ -109,4 +109,28 @@ WHERE
109
109
ORDER BY id, month DESC ;
110
110
```
111
111
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
+
112
136
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -134,4 +134,28 @@ WHERE
134
134
ORDER BY id, month DESC ;
135
135
```
136
136
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
+
137
161
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
1
# 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 (
13
4
SELECT
14
5
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
16
16
FROM Employee
17
- GROUP BY id
18
17
)
19
- ORDER BY id, month DESC ;
18
+ SELECT id, month, salary
19
+ FROM T
20
+ WHERE rk > 1
21
+ ORDER BY 1 , 2 DESC ;
Original file line number Diff line number Diff line change @@ -84,14 +84,18 @@ Department 表:
84
84
### ** SQL**
85
85
86
86
``` 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
90
95
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 ;
95
99
```
96
100
97
101
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -81,14 +81,18 @@ Department table:
81
81
### ** SQL**
82
82
83
83
``` 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
87
92
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 ;
92
96
```
93
97
94
98
<!-- tabs:end -->
Original file line number Diff line number Diff line change 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
4
9
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 ;
Original file line number Diff line number Diff line change 45
45
### ** SQL**
46
46
47
47
``` sql
48
- SELECT
49
- name
48
+ # Write your MySQL query statement below
49
+ SELECT name
50
50
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 ;
61
52
```
62
53
63
54
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -60,19 +60,10 @@ Customer table:
60
60
### ** SQL**
61
61
62
62
``` sql
63
- SELECT
64
- name
63
+ # Write your MySQL query statement below
64
+ SELECT name
65
65
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 ;
76
67
```
77
68
78
69
<!-- tabs:end -->
Original file line number Diff line number Diff line change 1
- SELECT
2
- name
1
+ # Write your MySQL query statement below
2
+ SELECT name
3
3
FROM Customer
4
- WHERE referee_id != 2 OR referee_id IS NULL ;
4
+ WHERE ifnull( referee_id, 0 ) != 2 ;
You can’t perform that action at this time.
0 commit comments