SQL: Joining three tables (junction tables)
Opened this issue · 0 comments
sophryu99 commented
Case: Find 10 actors/actresses in the most movies.
Approach
- Join TITLE and ROLE with key
tConst
- Join NAME and ROLE with key
nConst
- GROUP BY name to count the number of movies for each actors/actress
- ORDER BY descending value
SELECT
n.primaryName as name,
COUNT(t.primaryTitle) as total
FROM name n
JOIN role r
ON n.nConst = r.nConst
JOIN title t
ON t.tConst = r.tConst
WHERE ((r.category = "actor" OR r.category = "actress") AND t.titleType = "movie")
GROUP BY name
ORDER BY total DESC
LIMIT 10;