Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions solution/0500-0599/0577.Employee Bonus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ empId 是这张表单的主关键字
### **SQL**

```sql
SELECT
e.name,
b.bonus
# Write your MySQL query statement below
SELECT name, bonus
FROM
Employee AS e
LEFT JOIN Bonus AS b ON e.empid = b.empid
WHERE b.bonus < 1000 OR b.bonus IS NULL;
Employee
LEFT JOIN Bonus USING (empId)
WHERE ifnull(bonus, 0) < 1000;
```

<!-- tabs:end -->
11 changes: 5 additions & 6 deletions solution/0500-0599/0577.Employee Bonus/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ Bonus table:
### **SQL**

```sql
SELECT
e.name,
b.bonus
# Write your MySQL query statement below
SELECT name, bonus
FROM
Employee AS e
LEFT JOIN Bonus AS b ON e.empid = b.empid
WHERE b.bonus < 1000 OR b.bonus IS NULL;
Employee
LEFT JOIN Bonus USING (empId)
WHERE ifnull(bonus, 0) < 1000;
```

<!-- tabs:end -->
11 changes: 5 additions & 6 deletions solution/0500-0599/0577.Employee Bonus/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SELECT
e.name,
b.bonus
# Write your MySQL query statement below
SELECT name, bonus
FROM
Employee AS e
LEFT JOIN Bonus AS b ON e.empid = b.empid
WHERE b.bonus < 1000 OR b.bonus IS NULL;
Employee
LEFT JOIN Bonus USING (empId)
WHERE ifnull(bonus, 0) < 1000;
25 changes: 21 additions & 4 deletions solution/0500-0599/0578.Get Highest Answer Rate Question/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,28 @@ SurveyLog table:
### **SQL**

```sql
# Write your MySQL query statement below
SELECT question_id AS survey_log
FROM SurveyLog
GROUP BY 1
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
LIMIT 1;
FROM SurveyLog
GROUP BY 1
ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1
LIMIT 1;
```

```sql
WITH
T AS (
SELECT
question_id AS survey_log,
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
sum(action = 'show') OVER (PARTITION BY question_id)
) AS ratio
FROM SurveyLog
)
SELECT survey_log
FROM T
ORDER BY ratio DESC, 1
LIMIT 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,28 @@ Question 285 has the highest answer rate.</pre>
### **SQL**

```sql
# Write your MySQL query statement below
SELECT question_id AS survey_log
FROM SurveyLog
GROUP BY 1
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
LIMIT 1;
FROM SurveyLog
GROUP BY 1
ORDER BY sum(action = 'answer') / sum(action = 'show') DESC, 1
LIMIT 1;
```

```sql
WITH
T AS (
SELECT
question_id AS survey_log,
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
sum(action = 'show') OVER (PARTITION BY question_id)
) AS ratio
FROM SurveyLog
)
SELECT survey_log
FROM T
ORDER BY ratio DESC, 1
LIMIT 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
SELECT question_id AS survey_log
FROM SurveyLog
GROUP BY 1
ORDER BY SUM(action = 'answer') / SUM(action = 'show') DESC
WITH
T AS (
SELECT
question_id AS survey_log,
(sum(action = 'answer') OVER (PARTITION BY question_id)) / (
sum(action = 'show') OVER (PARTITION BY question_id)
) AS ratio
FROM SurveyLog
)
SELECT survey_log
FROM T
ORDER BY ratio DESC, 1
LIMIT 1;
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,28 @@ WHERE
ORDER BY id, month DESC;
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
id,
month,
sum(salary) OVER (
PARTITION BY id
ORDER BY month
RANGE 2 PRECEDING
) AS salary,
rank() OVER (
PARTITION BY id
ORDER BY month DESC
) AS rk
FROM Employee
)
SELECT id, month, salary
FROM T
WHERE rk > 1
ORDER BY 1, 2 DESC;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,28 @@ WHERE
ORDER BY id, month DESC;
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
id,
month,
sum(salary) OVER (
PARTITION BY id
ORDER BY month
RANGE 2 PRECEDING
) AS salary,
rank() OVER (
PARTITION BY id
ORDER BY month DESC
) AS rk
FROM Employee
)
SELECT id, month, salary
FROM T
WHERE rk > 1
ORDER BY 1, 2 DESC;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Write your MySQL query statement below
SELECT
id,
month,
sum(salary) OVER (
PARTITION BY id
ORDER BY month
RANGE 2 PRECEDING
) AS Salary
FROM employee
WHERE
(id, month) NOT IN (
WITH
T AS (
SELECT
id,
max(month)
month,
sum(salary) OVER (
PARTITION BY id
ORDER BY month
RANGE 2 PRECEDING
) AS salary,
rank() OVER (
PARTITION BY id
ORDER BY month DESC
) AS rk
FROM Employee
GROUP BY id
)
ORDER BY id, month DESC;
SELECT id, month, salary
FROM T
WHERE rk > 1
ORDER BY 1, 2 DESC;
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,18 @@ Department 表:
### **SQL**

```sql
SELECT
department.dept_name,
COUNT(student.dept_id) AS student_number
# Write your MySQL query statement below
WITH
S AS (
SELECT dept_id, count(1) AS cnt
FROM Student
GROUP BY dept_id
)
SELECT dept_name, ifnull(cnt, 0) AS student_number
FROM
Student
RIGHT JOIN Department ON student.dept_id = department.dept_id
GROUP BY dept_name
ORDER BY student_number DESC, dept_name;
S
RIGHT JOIN Department USING (dept_id)
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ Department table:
### **SQL**

```sql
SELECT
department.dept_name,
COUNT(student.dept_id) AS student_number
# Write your MySQL query statement below
WITH
S AS (
SELECT dept_id, count(1) AS cnt
FROM Student
GROUP BY dept_id
)
SELECT dept_name, ifnull(cnt, 0) AS student_number
FROM
Student
RIGHT JOIN Department ON student.dept_id = department.dept_id
GROUP BY dept_name
ORDER BY student_number DESC, dept_name;
S
RIGHT JOIN Department USING (dept_id)
ORDER BY 2 DESC, 1;
```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
SELECT
department.dept_name,
COUNT(student.dept_id) AS student_number
# Write your MySQL query statement below
WITH
S AS (
SELECT dept_id, count(1) AS cnt
FROM Student
GROUP BY dept_id
)
SELECT dept_name, ifnull(cnt, 0) AS student_number
FROM
Student
RIGHT JOIN Department ON student.dept_id = department.dept_id
GROUP BY dept_name
ORDER BY student_number DESC, dept_name;
S
RIGHT JOIN Department USING (dept_id)
ORDER BY 2 DESC, 1;
15 changes: 3 additions & 12 deletions solution/0500-0599/0584.Find Customer Referee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,10 @@
### **SQL**

```sql
SELECT
name
# Write your MySQL query statement below
SELECT name
FROM Customer
WHERE referee_id != 2 OR referee_id IS NULL;
```

MySQL 可使用 `IFNULL()`:

```sql
SELECT
name
FROM customer
WHERE IFNULL(referee_id, 0) != 2;
WHERE ifnull(referee_id, 0) != 2;
```

<!-- tabs:end -->
15 changes: 3 additions & 12 deletions solution/0500-0599/0584.Find Customer Referee/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,10 @@ Customer table:
### **SQL**

```sql
SELECT
name
# Write your MySQL query statement below
SELECT name
FROM Customer
WHERE referee_id != 2 OR referee_id IS NULL;
```

MySQL can use `IFNULL()`:

```sql
SELECT
name
FROM customer
WHERE IFNULL(referee_id, 0) != 2;
WHERE ifnull(referee_id, 0) != 2;
```

<!-- tabs:end -->
6 changes: 3 additions & 3 deletions solution/0500-0599/0584.Find Customer Referee/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SELECT
name
# Write your MySQL query statement below
SELECT name
FROM Customer
WHERE referee_id != 2 OR referee_id IS NULL;
WHERE ifnull(referee_id, 0) != 2;