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 88aa72d

Browse files
feat: add solutions to lc problems (doocs#1190)
1 parent 3da8b15 commit 88aa72d

File tree

27 files changed

+459
-319
lines changed

27 files changed

+459
-319
lines changed

‎solution/0600-0699/0601.Human Traffic of Stadium/README.md‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,18 @@ id</strong> 为 5、6、7、8 的四行 id 连续,并且每行都有 &gt;= 100
7373
```sql
7474
# Write your MySQL query statement below
7575
WITH
76-
s AS (
77-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
76+
S AS (
77+
SELECT
78+
*,
79+
id - (row_number() OVER (ORDER BY id)) AS rk
7880
FROM Stadium
7981
WHERE people >= 100
8082
),
81-
t AS (
82-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
83-
FROM s
84-
)
83+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
8584
SELECT id, visit_date, people
86-
FROM t
85+
FROM T
8786
WHERE cnt >= 3
88-
ORDER BY visit_date;
87+
ORDER BY 1;
8988
```
9089

9190
<!-- tabs:end -->

‎solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,18 @@ The rows with ids 2 and 3 are not included because we need at least three consec
6868
```sql
6969
# Write your MySQL query statement below
7070
WITH
71-
s AS (
72-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
71+
S AS (
72+
SELECT
73+
*,
74+
id - (row_number() OVER (ORDER BY id)) AS rk
7375
FROM Stadium
7476
WHERE people >= 100
7577
),
76-
t AS (
77-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
78-
FROM s
79-
)
78+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
8079
SELECT id, visit_date, people
81-
FROM t
80+
FROM T
8281
WHERE cnt >= 3
83-
ORDER BY visit_date;
82+
ORDER BY 1;
8483
```
8584

8685
<!-- tabs:end -->
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Write your MySQL query statement below
22
WITH
3-
s AS (
4-
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
3+
S AS (
4+
SELECT
5+
*,
6+
id - (row_number() OVER (ORDER BY id)) AS rk
57
FROM Stadium
68
WHERE people >= 100
79
),
8-
t AS (
9-
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
10-
FROM s
11-
)
10+
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
1211
SELECT id, visit_date, people
13-
FROM t
12+
FROM T
1413
WHERE cnt >= 3
15-
ORDER BY visit_date;
14+
ORDER BY 1;

‎solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,17 @@ RequestAccepted 表:
7171
### **SQL**
7272

7373
```sql
74-
SELECT
75-
ids AS id,
76-
COUNT(*) AS num
77-
FROM
78-
(
79-
SELECT
80-
requester_id AS ids
81-
FROM RequestAccepted
82-
UNION ALL
83-
SELECT
84-
accepter_id
85-
FROM RequestAccepted
86-
) AS t
87-
GROUP BY ids
88-
ORDER BY num DESC
74+
# Write your MySQL query statement below
75+
WITH
76+
T AS (
77+
SELECT requester_id, accepter_id FROM RequestAccepted
78+
UNION
79+
SELECT accepter_id, requester_id FROM RequestAccepted
80+
)
81+
SELECT requester_id AS id, count(accepter_id) AS num
82+
FROM T
83+
GROUP BY 1
84+
ORDER BY 2 DESC
8985
LIMIT 1;
9086
```
9187

‎solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,17 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends
6060
### **SQL**
6161

6262
```sql
63-
SELECT
64-
ids AS id,
65-
COUNT(*) AS num
66-
FROM
67-
(
68-
SELECT
69-
requester_id AS ids
70-
FROM RequestAccepted
71-
UNION ALL
72-
SELECT
73-
accepter_id
74-
FROM RequestAccepted
75-
) AS t
76-
GROUP BY ids
77-
ORDER BY num DESC
63+
# Write your MySQL query statement below
64+
WITH
65+
T AS (
66+
SELECT requester_id, accepter_id FROM RequestAccepted
67+
UNION
68+
SELECT accepter_id, requester_id FROM RequestAccepted
69+
)
70+
SELECT requester_id AS id, count(accepter_id) AS num
71+
FROM T
72+
GROUP BY 1
73+
ORDER BY 2 DESC
7874
LIMIT 1;
7975
```
8076

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
SELECT
2-
ids AS id,
3-
COUNT(*) AS num
4-
FROM
5-
(
6-
SELECT
7-
requester_id AS ids
8-
FROM RequestAccepted
9-
UNION ALL
10-
SELECT
11-
accepter_id
12-
FROM RequestAccepted
13-
) AS t
14-
GROUP BY ids
15-
ORDER BY num DESC
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT requester_id, accepter_id FROM RequestAccepted
5+
UNION
6+
SELECT accepter_id, requester_id FROM RequestAccepted
7+
)
8+
SELECT requester_id AS id, count(accepter_id) AS num
9+
FROM T
10+
GROUP BY 1
11+
ORDER BY 2 DESC
1612
LIMIT 1;

‎solution/0600-0699/0603.Consecutive Available Seats/README.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,23 @@ Cinema 表:
6666
SELECT DISTINCT a.seat_id
6767
FROM
6868
Cinema AS a
69-
JOIN Cinema AS b
70-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
71-
ORDER BY a.seat_id;
69+
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
70+
ORDER BY 1;
71+
```
72+
73+
```sql
74+
# Write your MySQL query statement below
75+
WITH
76+
T AS (
77+
SELECT
78+
seat_id,
79+
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
80+
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
81+
FROM Cinema
82+
)
83+
SELECT seat_id
84+
FROM T
85+
WHERE a = 2 OR b = 2;
7286
```
7387

7488
<!-- tabs:end -->

‎solution/0600-0699/0603.Consecutive Available Seats/README_EN.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,23 @@ Cinema table:
6363
SELECT DISTINCT a.seat_id
6464
FROM
6565
Cinema AS a
66-
JOIN Cinema AS b
67-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
68-
ORDER BY a.seat_id;
66+
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
67+
ORDER BY 1;
68+
```
69+
70+
```sql
71+
# Write your MySQL query statement below
72+
WITH
73+
T AS (
74+
SELECT
75+
seat_id,
76+
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
77+
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
78+
FROM Cinema
79+
)
80+
SELECT seat_id
81+
FROM T
82+
WHERE a = 2 OR b = 2;
6983
```
7084

7185
<!-- tabs:end -->
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Write your MySQL query statement below
2-
SELECT DISTINCT a.seat_id
3-
FROM
4-
Cinema AS a
5-
JOIN Cinema AS b
6-
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
7-
ORDER BY a.seat_id;
2+
WITH
3+
T AS (
4+
SELECT
5+
seat_id,
6+
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
7+
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
8+
FROM Cinema
9+
)
10+
SELECT seat_id
11+
FROM T
12+
WHERE a = 2 OR b = 2;

‎solution/0600-0699/0607.Sales Person/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,19 @@ WHERE
149149
);
150150
```
151151

152+
```sql
153+
# Write your MySQL query statement below
154+
SELECT name
155+
FROM SalesPerson
156+
WHERE
157+
sales_id NOT IN (
158+
SELECT sales_id
159+
FROM
160+
Company AS c
161+
JOIN Orders AS o USING (com_id)
162+
GROUP BY sales_id
163+
HAVING sum(name = 'RED') > 0
164+
);
165+
```
166+
152167
<!-- tabs:end -->

0 commit comments

Comments
(0)

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