Skip to content

1811. Find Interview Candidates 👍

 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
WITH
  UserToContest AS (
    SELECT gold_medal AS user_id, contest_id FROM Contests
    UNION ALL
    SELECT silver_medal AS user_id, contest_id FROM Contests
    UNION ALL
    SELECT bronze_medal AS user_id, contest_id FROM Contests
  ),
  UserToContestWithGroupId AS (
    SELECT
      user_id,
      contest_id - ROW_NUMBER() OVER(
        PARTITION BY user_id
        ORDER BY contest_id
      ) AS group_id
    FROM UserToContest
  ),
  CandidateUserIds AS (
    -- consecutive medal winners
    SELECT user_id
    FROM UserToContestWithGroupId
    GROUP BY user_id, group_id
    HAVING COUNT(*) >= 3
    UNION DISTINCT
    -- gold medal winners
    SELECT gold_medal AS user_id
    FROM Contests
    GROUP BY user_id
    HAVING COUNT(*) >= 3
  )
SELECT
  Users.name,
  Users.mail
FROM CandidateUserIds
INNER JOIN Users
  USING (user_id);