Categories: PostgreSQL
Tags:

Here are 5 questions you can try on the Pagila Database (a sample database for PostgreSQL inspired by Sakila for MySQL). These questions will help you explore its schema and practice querying:


1. List all movies along with their release year and category.

SELECT 
    f.title AS movie_title, 
    f.release_year, 
    c.name AS category_name
FROM 
    film f
JOIN 
    film_category fc ON f.film_id = fc.film_id
JOIN 
    category c ON fc.category_id = c.category_id;

2. Find the top 5 actors who have acted in the most movies.

SELECT 
    a.actor_id, 
    a.first_name, 
    a.last_name, 
    COUNT(fa.film_id) AS total_movies
FROM 
    actor a
JOIN 
    film_actor fa ON a.actor_id = fa.actor_id
GROUP BY 
    a.actor_id, a.first_name, a.last_name
ORDER BY 
    total_movies DESC
LIMIT 5;

3. Get a list of customers who rented more than 50 movies.

SELECT 
    c.customer_id, 
    c.first_name, 
    c.last_name, 
    COUNT(r.rental_id) AS total_rentals
FROM 
    customer c
JOIN 
    rental r ON c.customer_id = r.customer_id
GROUP BY 
    c.customer_id, c.first_name, c.last_name
HAVING 
    COUNT(r.rental_id) > 50;

4. Find the top 3 most rented movies.

SELECT 
    f.title AS movie_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 3;

5. Find the revenue generated by each store.

SELECT 
    s.store_id, 
    SUM(p.amount) AS total_revenue
FROM 
    store s
JOIN 
    staff st ON s.store_id = st.store_id
JOIN 
    payment p ON st.staff_id = p.staff_id
GROUP BY 
    s.store_id
ORDER BY 
    total_revenue DESC;

These questions cover various concepts such as joins, aggregations, and grouping in PostgreSQL.