@@ -3,30 +3,30 @@ Solutions for [SQL 50 Study Plan](https://leetcode.com/studyplan/top-sql-50/) on
3
3
4
4
---
5
5
6
- 1757 - Recyclable and Low Fat Products
6
+ [ 1757 - Recyclable and Low Fat Products] ( https://leetcode.com/problems/recyclable-and-low-fat-products/ )
7
7
``` sql
8
8
SELECT product_id
9
9
FROM Products
10
10
WHERE low_fats = ' Y'
11
11
AND recyclable = ' Y'
12
12
```
13
13
14
- 584 - Find Customer Referee
14
+ [ 584 - Find Customer Referee] ( https://leetcode.com/problems/find-customer-referee )
15
15
``` sql
16
16
SELECT name
17
17
FROM Customer
18
18
WHERE referee_id != 2 OR referee_id IS null
19
19
```
20
20
21
- 595 - Big Countries
21
+ [ 595 - Big Countries] ( https://leetcode.com/problems/big-countries/ )
22
22
``` sql
23
23
SELECT name, population, area
24
24
FROM WORLD
25
25
WHERE area >= 3000000
26
26
OR population >= 25000000
27
27
```
28
28
29
- 1148 - Article Views I
29
+ [ 1148 - Article Views I] ( https://leetcode.com/problems/article-views-i )
30
30
``` sql
31
31
SELECT DISTINCT author_id as id
32
32
FROM Views
@@ -35,39 +35,38 @@ AND author_id = viewer_id
35
35
ORDER BY author_id
36
36
```
37
37
38
- 1683 - Invalid Tweets
38
+ [ 1683 - Invalid Tweets] ( https://leetcode.com/problems/invalid-tweets/ )
39
39
``` sql
40
40
SELECT tweet_id
41
41
FROM Tweets
42
42
WHERE length(content) > 15
43
43
```
44
44
45
- 1378 - Replace Employee ID With The Unique
46
- Identifier
45
+ [ 1378 - Replace Employee ID With The Unique Identifier] ( https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier )
47
46
``` sql
48
47
SELECT unique_id, name
49
48
FROM Employees e
50
49
LEFT JOIN EmployeeUNI eu
51
50
ON e .id = eu .id
52
51
```
53
52
54
- 1068 - Product Sales Analysis I
53
+ [ 1068 - Product Sales Analysis I] ( https://leetcode.com/problems/product-sales-analysis-i/ )
55
54
``` sql
56
55
SELECT product_name, year, price
57
56
FROM Sales s
58
57
LEFT JOIN Product p
59
58
ON s .product_id = p .product_id
60
59
```
61
60
62
- 1581 - Customer Who Visited but Did Not Make Any Transactions
61
+ [ 1581 - Customer Who Visited but Did Not Make Any Transactions] ( https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/ )
63
62
``` sql
64
63
SELECT customer_id, COUNT (* ) as count_no_trans
65
64
FROM Visits
66
65
WHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions)
67
66
GROUP BY customer_id
68
67
```
69
68
70
- 197 . Rising Temperature
69
+ [ 197 - Rising Temperature] ( https://leetcode.com/problems/rising-temperature/ )
71
70
``` sql
72
71
SELECT w1 .id
73
72
FROM Weather w1, Weather w2
@@ -81,7 +80,7 @@ WHERE w1.temperature > w2.temperature
81
80
AND SUBDATE(w1 .recordDate , 1 ) = w2 .recordDate
82
81
```
83
82
84
- 1661 . Average Time of Process per Machine
83
+ [ 1661 - Average Time of Process per Machine] ( https://leetcode.com/problems/average-time-of-process-per-machine/ )
85
84
``` sql
86
85
SELECT machine_id, ROUND(AVG (end - start), 3 ) AS processing_time
87
86
FROM
93
92
GROUP BY machine_id
94
93
```
95
94
96
- 577 . Employee Bonus
95
+ [ 577 - Employee Bonus] ( https://leetcode.com/problems/employee-bonus/solutions/ )
97
96
``` sql
98
97
SELECT name, bonus
99
98
FROM Employee e
@@ -103,7 +102,7 @@ WHERE bonus < 1000
103
102
OR bonus IS NULL
104
103
```
105
104
106
- 1280 . Students and Examinations
105
+ [ 1280 - Students and Examinations] ( https://leetcode.com/problems/students-and-examinations/ )
107
106
``` sql
108
107
SELECT a .student_id , a .student_name , b .subject_name , COUNT (c .subject_name ) AS attended_exams
109
108
FROM Students a
@@ -113,4 +112,23 @@ ON a.student_id = c.student_id
113
112
AND b .subject_name = c .subject_name
114
113
GROUP BY 1 , 3
115
114
ORDER BY 1 , 3
115
+ ```
116
+ [ 570. Managers with at Least 5 Direct Reports] ( https://leetcode.com/problems/managers-with-at-least-5-direct-reports )
117
+ ``` sql
118
+ SELECT name
119
+ FROM Employee
120
+ WHERE id IN
121
+ (SELECT managerId
122
+ FROM Employee
123
+ GROUP BY managerId
124
+ HAVING COUNT (* ) >= 5
125
+ )
126
+
127
+ -- OR
128
+ SELECT a .name
129
+ FROM Employee a
130
+ JOIN Employee b
131
+ WHERE a .id = b .managerId
132
+ GROUP BY b .managerId
133
+ HAVING COUNT (* ) >= 5
116
134
```
0 commit comments