Skip to content

Commit 4ea9e02

Browse files
committed
LeetCode 500~600 mysql
1 parent 69cc60f commit 4ea9e02

File tree

33 files changed

+300
-123
lines changed

33 files changed

+300
-123
lines changed

Diff for: solution/0500-0599/0550.Game Play Analysis IV/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
(select round(count(distinct (b.player_id)) / (select count(distinct (player_id)) from Activity), 2) as fraction
20+
from Activity a
21+
left join (select player_id, min(event_date) as first_date from Activity group by player_id) as b
22+
on a.player_id = b.player_id and datediff(a.event_date, b.first_date) = 1)
2023
```
2124

2225
<!-- tabs:end -->

Diff for: solution/0500-0599/0550.Game Play Analysis IV/README_EN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ None
1414
### **SQL**
1515

1616
```
17-
17+
(select round(count(distinct (b.player_id)) / (select count(distinct (player_id)) from Activity), 2) as fraction
18+
from Activity a
19+
left join (select player_id, min(event_date) as first_date from Activity group by player_id) as b
20+
on a.player_id = b.player_id and datediff(a.event_date, b.first_date) = 1)
1821
```
1922

2023
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(select round(count(distinct (b.player_id)) / (select count(distinct (player_id)) from Activity), 2) as fraction
2+
from Activity a
3+
left join (select player_id, min(event_date) as first_date from Activity group by player_id) as b
4+
on a.player_id = b.player_id and datediff(a.event_date, b.first_date) = 1)

Diff for: solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
SELECT Name
20+
FROM Employee
21+
WHERE Id IN
22+
(SELECT ManagerId
23+
FROM Employee
24+
GROUP BY ManagerId
25+
HAVING count(*) >= 5)
2026
```
2127

2228
<!-- tabs:end -->

Diff for: solution/0500-0599/0570.Managers with at Least 5 Direct Reports/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ None
1414
### **SQL**
1515

1616
```
17-
17+
SELECT Name
18+
FROM Employee
19+
WHERE Id IN
20+
(SELECT ManagerId
21+
FROM Employee
22+
GROUP BY ManagerId
23+
HAVING count(*) >= 5)
1824
```
1925

2026
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT Name
2+
FROM Employee
3+
WHERE Id IN
4+
(SELECT ManagerId
5+
FROM Employee
6+
GROUP BY ManagerId
7+
HAVING count(*) >= 5)

Diff for: solution/0500-0599/0574.Winning Candidate/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
SELECT Name
20+
FROM Candidate,
21+
(SELECT CandidateId,
22+
count(*) AS total
23+
FROM Vote
24+
GROUP BY CandidateId
25+
ORDER BY total DESC limit 1) AS tmp
26+
WHERE Candidate.id = tmp.CandidateId
2027
```
2128

2229
<!-- tabs:end -->

Diff for: solution/0500-0599/0574.Winning Candidate/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ None
1414
### **SQL**
1515

1616
```
17-
17+
SELECT Name
18+
FROM Candidate,
19+
(SELECT CandidateId,
20+
count(*) AS total
21+
FROM Vote
22+
GROUP BY CandidateId
23+
ORDER BY total DESC limit 1) AS tmp
24+
WHERE Candidate.id = tmp.CandidateId
1825
```
1926

2027
<!-- tabs:end -->
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT Name
2+
FROM Candidate,
3+
(SELECT CandidateId,
4+
count(*) AS total
5+
FROM Vote
6+
GROUP BY CandidateId
7+
ORDER BY total DESC limit 1) AS tmp
8+
WHERE Candidate.id = tmp.CandidateId

Diff for: solution/0500-0599/0577.Employee Bonus/README.md

+9-16
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,16 @@ None
1313

1414
<!-- tabs:start -->
1515

16-
### **Python3**
17-
<!-- 这里可写当前语言的特殊实现逻辑 -->
16+
### **SQL**
1817

19-
```python
20-
21-
```
22-
23-
### **Java**
24-
<!-- 这里可写当前语言的特殊实现逻辑 -->
25-
26-
```java
27-
28-
```
29-
30-
### **...**
3118
```
32-
19+
SELECT name,
20+
bonus
21+
FROM Employee e
22+
LEFT JOIN Bonus b
23+
ON e.empId = b.empId
24+
WHERE (b.bonus < 1000
25+
OR b.bonus is null)
3326
```
3427

35-
<!-- tabs:end -->
28+
<!-- tabs:end -->

Diff for: solution/0500-0599/0577.Employee Bonus/README_EN.md

+9-14
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,16 @@ None
1111

1212
<!-- tabs:start -->
1313

14-
### **Python3**
14+
### **SQL**
1515

16-
```python
17-
18-
```
19-
20-
### **Java**
21-
22-
```java
23-
24-
```
25-
26-
### **...**
2716
```
28-
17+
SELECT name,
18+
bonus
19+
FROM Employee e
20+
LEFT JOIN Bonus b
21+
ON e.empId = b.empId
22+
WHERE (b.bonus < 1000
23+
OR b.bonus is null)
2924
```
3025

31-
<!-- tabs:end -->
26+
<!-- tabs:end -->

Diff for: solution/0500-0599/0577.Employee Bonus/Solution.sql

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SELECT name,
2+
bonus
3+
FROM Employee e
4+
LEFT JOIN Bonus b
5+
ON e.empId = b.empId
6+
WHERE (b.bonus < 1000
7+
OR b.bonus is null)

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,27 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
SELECT
20+
E1.id,
21+
E1.month,
22+
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary
23+
FROM
24+
(SELECT
25+
id, MAX(month) AS month
26+
FROM
27+
Employee
28+
GROUP BY id
29+
HAVING COUNT(*) > 1) AS maxmonth
30+
LEFT JOIN
31+
Employee E1 ON (maxmonth.id = E1.id
32+
AND maxmonth.month > E1.month)
33+
LEFT JOIN
34+
Employee E2 ON (E2.id = E1.id
35+
AND E2.month = E1.month - 1)
36+
LEFT JOIN
37+
Employee E3 ON (E3.id = E1.id
38+
AND E3.month = E1.month - 2)
39+
ORDER BY id ASC , month DESC
2040
```
2141

2242
<!-- tabs:end -->

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,27 @@ None
1414
### **SQL**
1515

1616
```
17-
17+
SELECT
18+
E1.id,
19+
E1.month,
20+
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary
21+
FROM
22+
(SELECT
23+
id, MAX(month) AS month
24+
FROM
25+
Employee
26+
GROUP BY id
27+
HAVING COUNT(*) > 1) AS maxmonth
28+
LEFT JOIN
29+
Employee E1 ON (maxmonth.id = E1.id
30+
AND maxmonth.month > E1.month)
31+
LEFT JOIN
32+
Employee E2 ON (E2.id = E1.id
33+
AND E2.month = E1.month - 1)
34+
LEFT JOIN
35+
Employee E3 ON (E3.id = E1.id
36+
AND E3.month = E1.month - 2)
37+
ORDER BY id ASC , month DESC
1838
```
1939

2040
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SELECT
2+
E1.id,
3+
E1.month,
4+
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary
5+
FROM
6+
(SELECT
7+
id, MAX(month) AS month
8+
FROM
9+
Employee
10+
GROUP BY id
11+
HAVING COUNT(*) > 1) AS maxmonth
12+
LEFT JOIN
13+
Employee E1 ON (maxmonth.id = E1.id
14+
AND maxmonth.month > E1.month)
15+
LEFT JOIN
16+
Employee E2 ON (E2.id = E1.id
17+
AND E2.month = E1.month - 1)
18+
LEFT JOIN
19+
Employee E3 ON (E3.id = E1.id
20+
AND E3.month = E1.month - 2)
21+
ORDER BY id ASC , month DESC

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

+13-16
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@ None
1313

1414
<!-- tabs:start -->
1515

16-
### **Python3**
17-
<!-- 这里可写当前语言的特殊实现逻辑 -->
16+
### **SQL**
1817

19-
```python
20-
21-
```
22-
23-
### **Java**
24-
<!-- 这里可写当前语言的特殊实现逻辑 -->
25-
26-
```java
27-
28-
```
29-
30-
### **...**
3118
```
32-
19+
SELECT dept_name,
20+
ifnull(total,
21+
0) AS student_number
22+
FROM department
23+
LEFT JOIN
24+
(SELECT dept_id,
25+
count(*) AS total
26+
FROM student
27+
GROUP BY dept_id) tmp
28+
ON department.dept_id = tmp.dept_id
29+
ORDER BY tmp.total desc
3330
```
3431

35-
<!-- tabs:end -->
32+
<!-- tabs:end -->

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

+13-14
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ None
1111

1212
<!-- tabs:start -->
1313

14-
### **Python3**
14+
### **SQL**
1515

16-
```python
17-
18-
```
19-
20-
### **Java**
21-
22-
```java
23-
24-
```
25-
26-
### **...**
2716
```
28-
17+
SELECT dept_name,
18+
ifnull(total,
19+
0) AS student_number
20+
FROM department
21+
LEFT JOIN
22+
(SELECT dept_id,
23+
count(*) AS total
24+
FROM student
25+
GROUP BY dept_id) tmp
26+
ON department.dept_id = tmp.dept_id
27+
ORDER BY tmp.total desc
2928
```
3029

31-
<!-- tabs:end -->
30+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
SELECT dept_name,
2+
ifnull(total,
3+
0) AS student_number
4+
FROM department
5+
LEFT JOIN
6+
(SELECT dept_id,
7+
count(*) AS total
8+
FROM student
9+
GROUP BY dept_id) tmp
10+
ON department.dept_id = tmp.dept_id
11+
ORDER BY tmp.total desc

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
SELECT name
20+
FROM customer
21+
WHERE referee_id != 2
22+
OR referee_id is null
2023
```
2124

2225
<!-- tabs:end -->

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ None
1414
### **SQL**
1515

1616
```
17-
17+
SELECT name
18+
FROM customer
19+
WHERE referee_id != 2
20+
OR referee_id is null
1821
```
1922

2023
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT name
2+
FROM customer
3+
WHERE referee_id != 2
4+
OR referee_id is null

Diff for: solution/0500-0599/0586.Customer Placing the Largest Number of Orders/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ None
1616
### **SQL**
1717

1818
```
19-
19+
SELECT customer_number
20+
FROM
21+
(SELECT customer_number,
22+
count(*) AS total
23+
FROM orders
24+
GROUP BY customer_number
25+
ORDER BY total DESC limit 1) tmp
2026
```
2127

2228
<!-- tabs:end -->

0 commit comments

Comments
 (0)