The document contains SQL queries for various tasks related to customer rentals and film categories. It includes retrieving customer rental history, counting rentals per customer, finding the most rented movies, and identifying customers who have never rented. Additionally, it lists overdue rentals and aims to find popular actors based on rental data.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
11 views
SQL Joins Questions
The document contains SQL queries for various tasks related to customer rentals and film categories. It includes retrieving customer rental history, counting rentals per customer, finding the most rented movies, and identifying customers who have never rented. Additionally, it lists overdue rentals and aims to find popular actors based on rental data.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
--1.
Get Customer Rental History
--Retrieve customer names along with the titles of the films they have rented. SELECT c.first_name, c.last_name, f.title, r.rental_date FROM customer c JOIN rental r ON c.customer_id = r.customer_id JOIN inventory i ON i.inventory_id = r.inventory_id JOIN film f ON f.film_id = i.film_id
--2. Find Films and Their Categories
--List all movies along with their categories. SELECT f.title, c.name AS film_category FROM film f JOIN film_category fc ON f.film_id = fc.film_id JOIN category c ON fc.category_id = c.category_id ORDER BY film_category
--3. Count Rentals per Customer
--Find how many rentals each customer has made. SELECT c.customer_id, c.first_name, c.last_name, COUNT(r.rental_id) AS total_rental FROM customer c JOIN rental r ON c.customer_id = r.customer_id GROUP BY c.customer_id --ORDER BY total_rental --4. Find the Most Rented Movies --Get the top 5 most rented movies. SELECT f.title, COUNT(r.rental_id) AS rental_count FROM film f JOIN inventory i ON f.film_id = i.film_id JOIN rental r ON i.inventory_id = r.inventory_id GROUP BY f.title ORDER BY rental_count DESC LIMIT 5
--5. Find the Most Popular Actors
--List actors who have appeared in the most rented movies.
--6. Find Customers Who Have Never Rented
--List all customers who have never rented a movie. SELECT c.customer_id, c.first_name, c.last_name FROM customer c LEFT JOIN rental r ON c.customer_id = r.customer_id WHERE r.rental_id IS NULL
--7. List All Overdue Rentals
--Find all rentals that were not returned on time. SELECT c.first_name, c.last_name, f.title, r.rental_date, r.return_date, f.rental_duration FROM customer c JOIN rental r ON c.customer_id = r.customer_id JOIN inventory i ON i.inventory_id = r.inventory_id JOIN film f ON f.film_id = i.film_id WHERE r.return_date > (r.rental_date+INTERVAL '1' DAY*f.rental_duration) ORDER BY r.return_date