Skip to content

Instantly share code, notes, and snippets.

@francisngo
Created December 29, 2017 02:22
Show Gist options
  • Save francisngo/459e43a24ad6312d85430f4b6ee75b56 to your computer and use it in GitHub Desktop.
Save francisngo/459e43a24ad6312d85430f4b6ee75b56 to your computer and use it in GitHub Desktop.
A SQL select statement with multiple joins
/*
Sample Data
-----------
movie
------------------------------------------------
id title yr director budget gross
actor
-----------
id name
casting
-----------------------
movieid actorid ord
*/
-- List the film title and the leading actor for all of the films 'John Travolta' played in.
SELECT movie.title, actor.name
FROM movie JOIN casting ON (movie.id = casting.movieid AND casting.ord = 1)
JOIN actor ON (actor.id = casting.actorid)
WHERE movie.id IN (SELECT casting.movieid FROM casting WHERE casting.actorid IN (SELECT actor.id FROM actor WHERE name = 'John Travolta'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment