Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 264df17

Browse files
feat: add sql solutions to lc problems (doocs#1195)
* No.0619.Biggest Single Number * No.0620.Not Boring Movies * No.1045.Customers Who Bought All Products * No.1068.Product Sales Analysis I * No.1069.Product Sales Analysis II * No.1070.Product Sales Analysis III * No.1075.Project Employees I
1 parent ec0b8a3 commit 264df17

File tree

26 files changed

+191
-123
lines changed

26 files changed

+191
-123
lines changed

‎solution/0600-0699/0619.Biggest Single Number/README.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,28 @@ MyNumbers table:
9696
### **SQL**
9797

9898
```sql
99-
SELECT MAX(a.num) AS num
99+
# Write your MySQL query statement below
100+
SELECT max(num) AS num
100101
FROM
101102
(
102103
SELECT num
103104
FROM MyNumbers
104105
GROUP BY num
105-
HAVING count(*) = 1
106-
) AS a;
106+
HAVING count(1) = 1
107+
) AS t;
108+
```
109+
110+
```sql
111+
# Write your MySQL query statement below
112+
SELECT
113+
CASE
114+
WHEN count(1) = 1 THEN num
115+
ELSE NULL
116+
END AS num
117+
FROM MyNumbers
118+
GROUP BY num
119+
ORDER BY 1 DESC
120+
LIMIT 1;
107121
```
108122

109123
<!-- tabs:end -->

‎solution/0600-0699/0619.Biggest Single Number/README_EN.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,28 @@ MyNumbers table:
8484
### **SQL**
8585

8686
```sql
87-
SELECT MAX(a.num) AS num
87+
# Write your MySQL query statement below
88+
SELECT max(num) AS num
8889
FROM
8990
(
9091
SELECT num
9192
FROM MyNumbers
9293
GROUP BY num
93-
HAVING count(*) = 1
94-
) AS a;
94+
HAVING count(1) = 1
95+
) AS t;
96+
```
97+
98+
```sql
99+
# Write your MySQL query statement below
100+
SELECT
101+
CASE
102+
WHEN count(1) = 1 THEN num
103+
ELSE NULL
104+
END AS num
105+
FROM MyNumbers
106+
GROUP BY num
107+
ORDER BY 1 DESC
108+
LIMIT 1;
95109
```
96110

97111
<!-- tabs:end -->
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
SELECT MAX(a.num) AS num
2-
FROM
3-
(
4-
SELECT num
5-
FROM MyNumbers
6-
GROUP BY num
7-
HAVING count(*) = 1
8-
) AS a;
1+
# Write your MySQL query statement below
2+
SELECT
3+
CASE
4+
WHEN count(1) = 1 THEN num
5+
ELSE NULL
6+
END AS num
7+
FROM MyNumbers
8+
GROUP BY num
9+
ORDER BY 1 DESC
10+
LIMIT 1;

‎solution/0600-0699/0620.Not Boring Movies/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@
4848
### **SQL**
4949

5050
```sql
51+
# Write your MySQL query statement below
5152
SELECT *
52-
FROM cinema
53-
WHERE description NOT LIKE'%boring%' AND mod(id, 2) = 1
53+
FROM Cinema
54+
WHERE description !='boring' AND id % 2 = 1
5455
ORDER BY rating DESC;
5556
```
5657

‎solution/0600-0699/0620.Not Boring Movies/README_EN.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ We have three movies with odd-numbered IDs: 1, 3, and 5. The movie with ID = 3 i
6161
### **SQL**
6262

6363
```sql
64+
# Write your MySQL query statement below
6465
SELECT *
65-
FROM cinema
66-
WHERE description NOT LIKE'%boring%' AND mod(id, 2) = 1
66+
FROM Cinema
67+
WHERE description !='boring' AND id % 2 = 1
6768
ORDER BY rating DESC;
6869
```
6970

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
# Write your MySQL query statement below
12
SELECT *
2-
FROM cinema
3-
WHERE description NOT LIKE'%boring%' AND mod(id, 2) = 1
3+
FROM Cinema
4+
WHERE description !='boring' AND id % 2 = 1
45
ORDER BY rating DESC;

‎solution/0600-0699/0626.Exchange Seats/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,17 @@ from
103103
seat
104104
```
105105

106+
```sql
107+
# Write your MySQL query statement below
108+
SELECT
109+
CASE
110+
WHEN id & 1 = 0 THEN id - 1
111+
WHEN row_number() OVER (ORDER BY id) != count(id) OVER () THEN id + 1
112+
ELSE id
113+
END AS id,
114+
student
115+
FROM Seat
116+
ORDER BY 1;
117+
```
118+
106119
<!-- tabs:end -->

‎solution/0600-0699/0626.Exchange Seats/README_EN.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,17 @@ from
9999
seat
100100
```
101101

102+
```sql
103+
# Write your MySQL query statement below
104+
SELECT
105+
CASE
106+
WHEN id & 1 = 0 THEN id - 1
107+
WHEN row_number() OVER (ORDER BY id) != count(id) OVER () THEN id + 1
108+
ELSE id
109+
END AS id,
110+
student
111+
FROM Seat
112+
ORDER BY 1;
113+
```
114+
102115
<!-- tabs:end -->

‎solution/1000-1099/1045.Customers Who Bought All Products/README.md‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,20 @@ Result 表:
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:GROUP BY + HAVING**
70+
71+
我们将 `Customer` 表按照 `customer_id` 进行分组,然后使用 `HAVING` 子句筛选出购买了所有产品的客户。
72+
6973
<!-- tabs:start -->
7074

7175
### **SQL**
7276

7377
```sql
7478
# Write your MySQL query statement below
75-
SELECT
76-
customer_id
79+
SELECT customer_id
7780
FROM Customer
78-
GROUP BY customer_id
79-
HAVING
80-
COUNT(DISTINCT product_key) = (
81-
SELECT
82-
COUNT(1)
83-
FROM Product
84-
);
81+
GROUP BY 1
82+
HAVING count(DISTINCT product_key) = (SELECT count(1) FROM Product);
8583
```
8684

8785
<!-- tabs:end -->

‎solution/1000-1099/1045.Customers Who Bought All Products/README_EN.md‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,10 @@ The customers who bought all the products (5 and 6) are customers with IDs 1 and
7979

8080
```sql
8181
# Write your MySQL query statement below
82-
SELECT
83-
customer_id
82+
SELECT customer_id
8483
FROM Customer
85-
GROUP BY customer_id
86-
HAVING
87-
COUNT(DISTINCT product_key) = (
88-
SELECT
89-
COUNT(1)
90-
FROM Product
91-
);
84+
GROUP BY 1
85+
HAVING count(DISTINCT product_key) = (SELECT count(1) FROM Product);
9286
```
9387

9488
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /