Skip to content

Commit cdfb55a

Browse files
committed
Upload 134
1 parent 029fb78 commit cdfb55a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ The problems and both submitted and site solutions are documented in individual
166166
| 131 | [Exchange Seats](https://leetcode.com/problems/exchange-seats/description/) | [Solution](solutions/131_exchange_seats.md) | LeetCode | Medium | `CASE` w/ `LAG` w/ `COALESCE` |
167167
| 132 | [Movie Rating](https://leetcode.com/problems/movie-rating/description/) | [Solution](solutions/132_movie_rating.md) | LeetCode | Medium | |
168168
| 133 | [Restaurant Growth](https://leetcode.com/problems/restaurant-growth/description/) | [Solution](solutions/133_restaurant_growth.md) | LeetCode | Medium | |
169+
| 134 | [Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/) | [Solution](solutions/134_friend_requests_ii_who_has_the_most_friends.md) | LeetCode | Medium | `UNION ALL` |
169170
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
170171

171172
## Author(s)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# SQL Everyday \#134
2+
3+
## Friend Requests II: Who Has the Most Friends
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Medium
7+
8+
## Problem
9+
10+
Write a solution to find the people who have the most friends and the most friends number.
11+
12+
The test cases are generated so that only one person has the most friends. [[Full Description](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte1 AS (
19+
SELECT
20+
accepter_id AS id
21+
,COALESCE(COUNT(DISTINCT requester_id), 0) AS total
22+
FROM RequestAccepted
23+
GROUP BY accepter_id
24+
),
25+
cte2 AS (
26+
SELECT
27+
requester_id AS id
28+
,COALESCE(COUNT(DISTINCT accepter_id), 0) AS total
29+
FROM RequestAccepted
30+
GROUP BY requester_id
31+
),
32+
cte3 AS (
33+
SELECT
34+
*
35+
FROM cte1
36+
UNION ALL
37+
SELECT
38+
*
39+
FROM cte2
40+
)
41+
SELECT
42+
id
43+
,SUM(total) AS num
44+
FROM cte3
45+
GROUP BY id
46+
ORDER BY num DESC
47+
LIMIT 1
48+
;
49+
```
50+
51+
## Site Solution
52+
53+
```sql
54+
-- LeetCode Solution
55+
WITH all_ids AS (
56+
SELECT requester_id AS id
57+
FROM RequestAccepted
58+
UNION ALL
59+
SELECT accepter_id AS id
60+
FROM RequestAccepted)
61+
SELECT id, num
62+
FROM
63+
(
64+
SELECT id,
65+
COUNT(id) AS num,
66+
RANK () OVER(ORDER BY COUNT(id) DESC) AS rnk
67+
FROM all_ids
68+
GROUP BY id
69+
)t0
70+
WHERE rnk=1
71+
```
72+
73+
## Notes
74+
75+
TBD
76+
77+
## NB
78+
79+
`UNION ALL`
80+
81+
Go to [Index](../?tab=readme-ov-file#index)\
82+
Go to [Overview](../?tab=readme-ov-file)
83+

0 commit comments

Comments
 (0)