SQL JOIN Practice: Linking Customers, Rentals, and Movies
Stitch three tables together so a rental reads as a name and a title, not a pair of ids.
The
rentals
table records a
customer_id
and a
movie_id, but not the customer's name or the film's title. Those live in the
customers
and
movies
tables, reached by their keys.
A single query can join more than two tables. Start from the table in the
middle:
rentals, and add one
JOIN
for each related table you need.
Build the joins before you filter
Get the relationships working first, then add the
WHERE
clause. Because the join brings customer columns into the same result, you
can filter on
c.city
even though the city never appears in the rentals table. Table aliases
like
r,
c, and
m
keep the column references short and unambiguous.
select a.detail, b.detail, c.detail
from fact_table as a
join dim_one as b on a.b_id = b.b_id
join dim_two as c on a.c_id = c.c_id
where b.filter_column = 'value' Schema · Movie Rentals Tables · rentals, customers, movies 3 tables
One row per rental. Movies still out have a null returned_date.
One row per customer.
One row per movie in the catalog.
For rentals by Portland customers, return the customer's first name, last name, the movie title, the rental date, and the amount. Order by rental date, oldest first.
Run your query to inspect the result.
Expected answer
- Columns: first_name, last_name, title, rental_date, amount.
- Rows: 8 rentals by Portland customers.
- The first row is Ava Mitchell renting Neon Harbor on 2023-06-01.
Related
INNER, LEFT, RIGHT, FULL, and CROSS joins.
Join types and common join patterns.
Practice join keys, left joins, duplicate rows, and debugging.
One-to-many joins can inflate totals.
Qualify shared columns with a table alias.
Nested SELECTs, IN, EXISTS, and correlation.