29 28.1 SQL Practice: Movies Table
29.1 Download the practice data
Use either format below (same data in both files):
- SQLite database file: moviesPractice-v001.sqlite
- CSV file: moviesPractice-v001.csv
The SQLite file contains one table named movies.
29.2 Table structure
movies(movie_id, title, genre, director, release_year, runtime_minutes, rating, domestic_box_office_millions, oscar_wins, franchise)
29.3 Sample rows
| movie_id | title | genre | director | release_year | runtime_minutes | rating | domestic_box_office_millions | oscar_wins | franchise |
|---|---|---|---|---|---|---|---|---|---|
| 1 | The Last Algorithm | Sci-Fi | Ava Chen | 2018 | 124 | PG-13 | 210.4 | 2 | DataVerse |
| 6 | Skyline Detectives II | Action | Noah Price | 2022 | 146 | PG-13 | 455.1 | 2 | Skyline |
| 11 | Map of Small Worlds | Animation | Kira Stone | 2018 | 93 | G | 132.4 | 1 | Small Worlds |
| 16 | The Null Value | Mystery | Priya Raman | 2024 | 110 | NULL | NULL | 0 | NULL |
29.4 Practice questions (easy to challenging)
Write SQL queries that answer the following.
29.4.1 Level 1: Core SELECT/FROM
- Show all columns for all movies.
- Show only
title,director, andrelease_year. - Show the first 5 rows in alphabetical order by
title. - Show all distinct
genrevalues in alphabetical order.
29.4.2 Level 2: WHERE, ORDER BY, LIMIT, IN, BETWEEN
- Show all movies released between 2018 and 2022 (inclusive), ordered by
release_yearthentitle. - Show all movies directed by
Ava Chen,Noah Price, orLena Brooks. - Show the 3 longest movies (display
titleandruntime_minutes). - Show movies with
rating = 'PG'orrating = 'G', ordered byrelease_yeardescending.
29.4.3 Level 3: LIKE, calculated columns, aliases
- Show movies whose
titlestarts withMap. - Show movies whose
titlecontains the wordSkyline. - Show
title,runtime_minutes, and a calculated column namedruntime_hours(runtime_minutes / 60.0). - Show
titleand a calculated column namedbox_office_per_minute(domestic_box_office_millions / runtime_minutes), ordered from highest to lowest.
29.4.4 Level 4: NULL handling and logic
- Show movies where
ratingis NULL. - Show movies where
domestic_box_office_millionsis NOT NULL and is at least 100. - Show movies where
franchiseis NULL andrelease_year >= 2018. - Show movies where
genre = 'Drama'and (release_year < 2018oroscar_wins >= 1).
29.4.5 Level 5: GROUP BY and HAVING
- For each
director, show the number of movies they directed (movie_count), ordered from largest to smallest. - For each
genre, show average runtime (avg_runtime) rounded to 1 decimal place. - Show directors with at least 3 movies in the table.
- For each non-NULL
franchise, show total domestic box office and keep only franchises above 300 million.
29.5 Optional stretch questions
- Show the second page of results when listing movies by
title, 5 rows per page. - Show each movie with a label column named
erathat is:
'classic'ifrelease_year < 2015'modern'ifrelease_yearis 2015 to 2020'new'ifrelease_year > 2020
- Show the top movie (or movies) by
domestic_box_office_millionswithin eachgenre.