SELECT name
FROM
records a
JOIN teams b on a.team_id = b.id
JOIN athletes c ON a.athlete_id = c.id
JOIN games d ON a.game_id = d.id
WHERE
d.year >= 2000 and medal is not null
GROUP BY
athlete_id
HAVING
count(DISTINCT team_id) >= 2
ORDER BY 1 asc
2개 이상의 국적으로 메달을 수상한 기록이 있어야 한다고 해서, teams 테이블도 join했다.
그런데 나라 이름은 필요하지 않고, records 테이블에 선수의 국적 id가 나와있으므로 따로 join하지 않아도 된다.
SELECT
name
FROM
records a
JOIN athletes c ON a.athlete_id = c.id
JOIN games d ON a.game_id = d.id
WHERE
d.year >= 2000
and medal is not null
GROUP BY
athlete_id
HAVING
count(DISTINCT team_id) >= 2
ORDER BY
1 asc
teams 테이블 join 하지 않았을 때의 코드이다.