-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.sql
39 lines (39 loc) · 933 Bytes
/
Solution.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Write your MySQL query statement below
WITH
S AS (
SELECT contest_id, gold_medal AS user_id, 1 AS type
FROM Contests
UNION
SELECT contest_id, silver_medal AS user_id, 2 AS type
FROM Contests
UNION
SELECT contest_id, bronze_medal AS user_id, 3 AS type
FROM Contests
),
T AS (
SELECT
user_id,
(
contest_id - row_number() OVER (
PARTITION BY user_id
ORDER BY contest_id
)
) AS diff
FROM S
),
P AS (
SELECT user_id
FROM S
WHERE type = 1
GROUP BY user_id
HAVING count(1) >= 3
UNION
SELECT DISTINCT user_id
FROM T
GROUP BY user_id, diff
HAVING count(1) >= 3
)
SELECT name, mail
FROM
P AS p
LEFT JOIN Users AS u ON p.user_id = u.user_id;