Coding Test

Leetcode - 1050. Actors and Directors Who Cooperated At Least Three Times

Indo Yoon

문제

Loading...
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

정답

SELECT actor_id, director_id
FROM (
    SELECT actor_id, director_id, COUNT(pair) as counts
    FROM (
        SELECT actor_id, director_id, CONCAT(actor_id, ",", director_id) AS pair
        FROM ActorDirector
    ) AS New
    GROUP BY pair
) AS New2
WHERE counts >= 3

temp table을 두 번 썼는데, 이걸 사용하지 않고 푸는 방법은 없을까? HAVING clause를 쓰면 더 간단하다.

SELECT actor_id, director_id
FROM ActorDirector
GROUP BY actor_id, director_id
HAVING COUNT(actor_id) >= 3