Read-only
WITH nyc_rentals AS (
SELECT
r.rental_id,
r.rental_date,
r.return_date,
r.customer_id,
c.first_name,
c.last_name,
a.latitude,
a.longitude,
i.store_id,
f.film_id,
f.title
FROM rental AS r
JOIN customer AS c
ON c.customer_id = r.customer_id
JOIN address AS a
ON a.address_id = c.address_id
JOIN inventory AS i
ON i.inventory_id = r.inventory_id
JOIN film AS f
ON f.film_id = i.film_id
WHERE r.rental_date >= CURRENT_TIMESTAMP - INTERVAL '6 months'
AND r.rental_date < CURRENT_TIMESTAMP
-- Approximate bounding box for New York City's five boroughs.
AND a.latitude BETWEEN 40.4774 AND 40.9176
AND a.longitude BETWEEN -74.2591 AND -73.7004
),
top_films AS (
SELECT
film_id,
COUNT(*) AS rental_count_past_6_months
FROM nyc_rentals
GROUP BY film_id
ORDER BY rental_count_past_6_months DESC, film_id
LIMIT 10
)
SELECT
nr.film_id,
nr.title,
tf.rental_count_past_6_months,
nr.rental_id,
nr.rental_date,
nr.return_date,
nr.customer_id,
nr.first_name,
nr.last_name,
nr.latitude,
nr.longitude,
nr.store_id
FROM nyc_rentals AS nr
JOIN top_films AS tf
ON tf.film_id = nr.film_id
ORDER BY
tf.rental_count_past_6_months DESC,
nr.film_id,
nr.rental_date DESC,
nr.rental_id;
●Reviewing your schemadone132ms
●Reviewing your schemadone0ms
●Reviewing your schemadone0ms
●Reviewing your schemadone0ms
●Reviewing your schemadone0ms
📝 Summary
Finds the 10 most-rented films during the past six months among customers whose addresses fall within an approximate New York City bounding box, then returns every qualifying rental for those films.
The output contains one row per rental, not one row per film, with customer, film, location, and store details.
🔎 Step by step
- Build
nyc_rentals: Join rentals to customers, customer addresses, inventory, and films. Keep rentals within the time window and geographic bounds. - Build
top_films: Count qualifying rentals per film and select up to 10 films. Break ties using the smallerfilm_id. - Return rental details: Join the selected films back to
nyc_rentals. - Sort: Show the most popular films first, then group by film ID, with each film’s newest rentals first.
WITH nyc_rentals AS (
SELECT
r.rental_id,
r.rental_date,
r.return_date,
r.customer_id,
c.first_name,
c.last_name,
a.latitude,
a.longitude,
i.store_id,
f.film_id,
f.title
FROM rental AS r
JOIN customer AS c
ON c.customer_id = r.customer_id
JOIN address AS a
ON a.address_id = c.address_id -- Customer's address, not store location.
JOIN inventory AS i
ON i.inventory_id = r.inventory_id
JOIN film AS f
ON f.film_id = i.film_id
-- Include the lower time boundary, but exclude the current instant.
WHERE r.rental_date >= CURRENT_TIMESTAMP - INTERVAL '6 months'
AND r.rental_date < CURRENT_TIMESTAMP
-- Inclusive rectangular bounds approximating NYC.
AND a.latitude BETWEEN 40.4774 AND 40.9176
AND a.longitude BETWEEN -74.2591 AND -73.7004
),
top_films AS (
SELECT
film_id,
COUNT(*) AS rental_count_past_6_months -- Count rental events per film.
FROM nyc_rentals
GROUP BY film_id
ORDER BY rental_count_past_6_months DESC, film_id
LIMIT 10 -- At most 10 films; ties do not expand this limit.
)
SELECT
nr.film_id,
nr.title,
tf.rental_count_past_6_months,
nr.rental_id,
nr.rental_date,
nr.return_date,
nr.customer_id,
nr.first_name,
nr.last_name,
nr.latitude,
nr.longitude,
nr.store_id
FROM nyc_rentals AS nr
JOIN top_films AS tf
ON tf.film_id = nr.film_id -- Keep every qualifying rental of a top film.
ORDER BY
tf.rental_count_past_6_months DESC,
nr.film_id,
nr.rental_date DESC,
nr.rental_id; -- Final tie-breaker for rentals with the same timestamp.📋 Returned rows
Each row represents a qualifying rental of one of the selected films. The film’s rental count repeats on every row for that film.
| Column | Type | Description |
|---|---|---|
film_id | integer | Film identifier. |
title | text | Film title. |
rental_count_past_6_months | bigint | Total qualifying rentals for this film within the time window and geographic bounds. |
rental_id | integer | Rental identifier. |
rental_date | timestamp with time zone | When the rental began. |
return_date | timestamp with time zone | Recorded return time; may be NULL. |
customer_id | integer | Customer identifier. |
first_name | text | Customer’s first name. |
last_name | text | Customer’s last name. |
latitude | double precision | Latitude of the customer’s address. |
longitude | double precision | Longitude of the customer’s address. |
store_id | integer | Store associated with the rented inventory item. |
⚠️ Things to watch
- Approximate geography: A rectangle is not NYC’s exact boundary and may include locations outside the city. Customers with missing coordinates are excluded.
- Current address: Location comes from the customer’s stored address, not necessarily their address when the rental occurred.
- Time semantics: In PostgreSQL,
CURRENT_TIMESTAMPis fixed at the start of the transaction.INTERVAL '6 months'means six calendar months, not a fixed 180 days. - No 10-row limit: Up to 10 films are selected, but all their qualifying rental rows are returned. Counts measure rentals—not unique customers—and include rentals regardless of whether they have been returned.
15s
Shared threads are read-only. Start your own chat to keep working.
Start a new chat