SQL
  • Tips
  • SQL
    • Database Basics
    • SQL Basics
    • SQL Syntax
    • Retrieve Data: SELECT
    • Sort Data: ORDER BY
    • Filter Data: WHERE
    • Calculated Fields
    • Aggregate Functions
    • Group Data: GROUP BY
    • Subqueries
    • Join Tables
    • Combine Queries: UNION
    • Control Flow Statements
    • IF function
    • Handle NULL
    • Date
    • Numeric
    • String
    • Notes
  • Table/Database
    • Insert
    • Delete
    • Update
    • Table
    • Database
    • Stored Procedure
  • Misc
    • SQL vs NoSQL
    • 大数据
    • Why SQL instead of Excel + VBA?
  • sqlzoo
    • world table
    • nobel table
    • football data
    • movie data
    • Teacher Department Data
    • Edinburgh Buses data
  • Leetcode
    • 175_Combine Two Tables
    • 176_Second Highest Salary
    • 177_Nth Highest Salary
    • 178_Rank Scores
    • 180_Consecutive Numbers
    • 181_Employees Earning More Than Their Managers
    • 182_Duplicate Emails
    • 183_Customers Who Never Order
    • 184_Department Highest Salary
    • 185_Department Top Three Salaries
    • 196_Delete Duplicate Emails
    • 197_Rising Temperature
    • 570_Managers with at Least 5 Direct Reports
    • 578_Get Highest Answer Rate Question
    • 579_Find Cumulative Salary of an Employee
    • 584_Find Customer Referee
    • 586_Customer Placing the Largest Number of Orders
    • 595_Big Countries
    • 596_Classes More Than 5 Students
    • 597_Friend Requests I: Overall Acceptance Rate
    • 601_Human Traffic of Stadium
    • 602_Friend Requests II: Who Has the Most Friends
    • 603_Consecutive Available Seats
    • 607_Sales Person
    • 608_Tree Node
    • 610_Triangle Judgement
    • 612_Shortest Distance in a Plane
    • 613_Shortest Distance in a Line
    • 619_Biggest Single Number
    • 620_Not Boring Movies
    • 626_Exchange Seats
    • 627_Swap Salary
  • Facebook 面经题
    • spam filter
    • marketplace
    • instagram
    • session
    • message confirmation
Powered by GitBook
On this page
  • 1. 1962 movies
  • 2. When was Citizen Kane released?
  • 3. Star Trek movies
  • 4. id for actor Glenn Close
  • 5. id for Casablanca
  • 6. Cast list for Casablanca
  • 7. Alien cast list
  • 8. Harrison Ford movies
  • 9. Harrison Ford as a supporting actor
  • 10. Lead actors in 1962 movies
  • 11. Busy years for John Travolta
  • 12. Lead actor in Julie Andrews movies
  • 13. Actors with 30 leading roles
  • 14.
  • 15.

Was this helpful?

  1. sqlzoo

movie data

Previousfootball dataNextTeacher Department Data

Last updated 5 years ago

Was this helpful?

This tutorial introduces the notion of a join. The database consists of three tablesmovie,actorandcasting.

movie:

id

title

yr

director

budget

gross

actor:

id

casting:

movieid

actorid

ord

</div>

1. 1962 movies

List the films where the yr is 1962 [Show id, title]

SELECT id, title
FROM movie
WHERE yr=1962;

2. When was Citizen Kane released?

Give year of 'Citizen Kane'.

SELECT yr
FROM movie
WHERE title = 'Citizen Kane';

3. Star Trek movies

List all of the Star Trek movies, include the id, title and yr (all of these movies include the words Star Trek in the title). Order results by year.

SELECT id, title, yr
FROM movie
WHERE title LIKE '%Star Trek%'
ORDER BY yr;

4. id for actor Glenn Close

What id number does the actor 'Glenn Close' have?

SELECT id
FROM actor
WHERE name = 'Glenn Close';

5. id for Casablanca

What is the id of the film 'Casablanca'

SELECT id
FROM movie
WHERE title = 'Casablanca';

6. Cast list for Casablanca

Obtain the cast list for 'Casablanca'.

Use movieid=11768, (or whatever value you got from the previous question)

SELECT name
FROM casting 
    INNER JOIN actor ON actorid = id
WHERE movieid = 11768;

7. Alien cast list

Obtain the cast list for the film 'Alien'

SELECT name
FROM casting 
    INNER JOIN actor ON actorid = actor.id
WHERE movieid =
                (
                SELECT id 
                FROM movie 
                WHERE title='Alien'
                );

8. Harrison Ford movies

List the films in which 'Harrison Ford' has appeared

SELECT title
FROM movie JOIN casting
ON id = movieid
WHERE actorid = 
                (
                SELECT id 
                FROM actor 
                WHERE name = 'Harrison Ford'
                );

9. Harrison Ford as a supporting actor

List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

SELECT title
FROM movie 
    JOIN casting ON id = movieid
WHERE actorid = 
            (
            SELECT id 
            FROM actor 
            WHERE name = 'Harrison Ford'
            ) 
    AND ord != 1;

10. Lead actors in 1962 movies

List the films together with the leading star for all 1962 films.

SELECT title, name
FROM movie 
     JOIN casting ON movie.id = movieid
     JOIN actor ON actor.id = actorid
WHERE yr = 1962 AND ord = 1;

11. Busy years for John Travolta

Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made more than 2 movies.

# using subquery to get the id of John Travolta
SELECT yr, COUNT(title)
FROM movie 
    JOIN casting ON id = movieid
WHERE actorid = (
                SELECT id 
                FROM actor 
                WHERE name = 'John Travolta'
                )
GROUP BY yr
HAVING COUNT(title)>2;

# use 2 inner join
SELECT yr, count(title)
FROM casting JOIN actor ON actorid = actor.id
             JOIN movie ON movie.id = movieid
WHERE name = 'John Travolta'
GROUP BY yr
HAVING count(title) > 2
ORDER BY yr;

12. Lead actor in Julie Andrews movies

List the film title and the leading actor for all of the films 'Julie Andrews' played in.

# use join and subquery
SELECT title, name
FROM movie
    JOIN casting ON movie.id = movieid
    JOIN actor ON actor.id = actorid
WHERE movieid IN (
                SELECT movieid 
                FROM casting 
                WHERE actorid = (
                                SELECT id 
                                FROM actor 
                                WHERE name = 'Julie Andrews'
                                )
                )
AND ord = 1;

# use inner join and self join
SELECT distinct title, a1.name
FROM casting as c1
    JOIN movie ON movie.id = c1.movieid
    JOIN actor as a1 ON c1.actorid = a1.id
    JOIN casting as c2 ON c1.movieid = c2.movieid
    JOIN actor as a2 ON c2.actorid = a2.id
WHERE a2.name = 'Julie Andrews' and c1.ord = 1;

13. Actors with 30 leading roles

Obtain a list, in alphabetical order, of actors who've had at least 30 starring roles.

SELECT name
FROM casting JOIN actor ON actorid = actor.id
WHERE ord = 1
GROUP BY name
HAVING count(movieid) >= 30
ORDER BY name;

14.

List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

SELECT title, COUNT(actorid)
FROM movie 
    JOIN casting ON id = movieid
WHERE yr = 1978
GROUP BY title
ORDER BY COUNT(actorid) DESC, title;

15.

List all the people who have worked with 'Art Garfunkel'.

# use inner join and subquery
SELECT DISTINCT name
FROM casting 
    JOIN actor ON (actorid = id AND name != 'Art Garfunkel')
WHERE movieid IN 
    (
    SELECT movieid 
    FROM casting 
    WHERE actorid = (
                    SELECT id 
                    FROM actor 
                    WHERE name = 'Art Garfunkel'
                    )
    );

# use inner join and self join
SELECT distinct a1.name
FROM casting as c1
JOIN actor as a1 ON c1.actorid = a1.id
JOIN casting as c2 ON c1.movieid = c2.movieid and c1.actorid != c2.actorid
JOIN actor as a2 ON c2.actorid = a2.id
WHERE a2.name = 'Art Garfunkel'
ORDER BY a1.name;
Movie-er.png