New month, new goals. Your help keeps the project moving forward. 🖥️ Support sqltest →
SQL code copied to buffer

Sakila database: schema, tables and SQL exercises

Sakila is the MySQL sample database for a chain of DVD rental stores. On SQLtest.online you can use it right in your browser: solve exercises with automatic checking and run your own queries in the playground, with nothing to install.

  • 16 tables and 7 views
  • 1,000 films
  • 16,044 rentals
  • 179 SQL exercises

What is Sakila

Sakila was created by Mike Hillyer of the MySQL documentation team, so that examples in the documentation and in books could share one realistic schema. It is named after Sakila, the dolphin in the MySQL logo, and is distributed under the BSD license.

The database models an everyday business: a film catalog with actors and genres, customers and staff of two stores, disc rentals and payments. That makes it good for learning: the relationships are clear without explanation, and there is enough data for grouping, window functions and analytics.

ER diagram

The diagram shows the Sakila tables and the foreign keys between them. Click it to open the full-size version.

ER diagram of the Sakila database

What's inside

The Sakila tables fall into three groups.

Film catalog

film, actor, category, language and the link tables film_actor, film_category.

Stores and people

store, staff, customer and addresses: address → city → country.

Rentals and payments

inventory holds the physical discs in each store, rental the rentals, payment the payments.

The key thing to remember: a customer rents a disc, not a film. So rental is linked to film through inventory, not directly. The film_text table is a helper copy of titles and descriptions for full-text search.

How much data the main tables hold:

TableRowsContents
rental16,044disc rentals
payment16,049customer payments
film_actor5,462actors' roles in films
inventory4,581discs in stores
film1,000films
customer599customers
city600cities
actor200actors
country109countries
category16genres
store2stores

Table structure

Click a table to see its columns, a sample row and its keys.

The list of tables

actor - actor table.
  • actor_idunique record identifier (PK)
  • first_nameactor's first name
  • last_nameactor's last name
  • last_updatedate and time of last update
actor_id first_name last_name last_update
1 John Doe 2023-01-01 12:00:00
  • PRIMARY KEY, btree (actor_id)
address - customer and staff addresses.
  • address_idunique record identifier (PK)
  • addresspostal address
  • address2additional address
  • districtdistrict or region
  • city_idcity identifier (FK)
  • postal_codepostal code
  • phonephone number
  • last_updatedate and time of last update
address_id address address2 district city_id postal_code phone last_update
1 123 Main St [null] Downtown 1 12345 +1234567890 2023-01-01 12:00:00
  • PRIMARY KEY, btree (address_id)
  • FOREIGN KEY (city_id) REFERENCES city(city_id)
category - film categories.
  • category_idunique record identifier (PK)
  • namecategory name
  • last_updatedate and time of last update
category_id name last_update
1 Action 2023-01-01 12:00:00
  • PRIMARY KEY, btree (category_id)
city - city table.
  • city_idunique record identifier (PK)
  • citycity name
  • country_idcountry identifier (FK)
  • last_updatedate and time of last update
city_id city country_id last_update
1 Metropolis 1 2023-01-01 12:00:00
  • PRIMARY KEY, btree (city_id)
  • FOREIGN KEY (country_id) REFERENCES country(country_id)
country - country table.
  • country_idunique record identifier (PK)
  • countrycountry name
  • last_updatedate and time of last update
country_id country last_update
1 United States 2023-01-01 12:00:00
  • PRIMARY KEY, btree (country_id)
customer - customer table.
  • customer_idunique record identifier (PK)
  • store_idstore identifier (FK)
  • first_namecustomer's first name
  • last_namecustomer's last name
  • emailcustomer's email address
  • address_idaddress identifier (FK)
  • activecustomer activity indicator (0/1)
  • create_datedate and time the customer was added to the database
  • last_updatedate and time of last update
customer_id store_id first_name last_name email address_id active create_date last_update
1 1 John Doe john.doe@example.com 1 1 2023-01-01 12:00:00 2023-01-01 12:00:00
  • PRIMARY KEY, btree (customer_id)
  • FOREIGN KEY (store_id) REFERENCES store(store_id)
  • FOREIGN KEY (address_id) REFERENCES address(address_id)
film - table of films.
  • film_idunique record identifier (PK)
  • titlefilm title
  • descriptionbrief description or plot of the film
  • release_yearyear the film was released
  • language_ididentifier of the film's language (FK)
  • original_language_ididentifier of the original language of the film in case it is dubbed into a new language
  • rental_durationduration of rental period in days
  • rental_ratecost of renting the film for the duration specified in the rental_duration column
  • lengthlength of the film in minutes
  • replacement_costamount of penalty for loss or damage of the disc
  • ratingrating assigned to the film. Can be one of: G, PG, PG-13, R, or NC-17
  • special_featureslist of special features included on the DVD. Can be zero or more of: Trailers, Commentaries, Deleted Scenes, Behind the Scenes
  • last_updatedate and time of last update
film_id title description release_year language_id original_language_id rental_duration rental_rate length replacement_cost rating special_features last_update
1 Film Title A brief description of the film. 2000 1 2 5 4.99 120 19.99 PG-13 Trailers, Commentaries 2023-01-01 12:00:00
  • PRIMARY KEY, btree (film_id)
  • FOREIGN KEY (language_id) REFERENCES language(language_id)
  • FOREIGN KEY (original_language_id) REFERENCES language(language_id)
film_actor - actors to films relation.
  • actor_ididentifier for actor (FK)
  • film_ididentifier for film (FK)
  • last_updatedate and time of last update
actor_id film_id last_update
1 1 2023-01-01 12:00:00
  • PRIMARY KEY, btree (actor_id, film_id)
  • FOREIGN KEY (actor_id) REFERENCES actor(actor_id)
  • FOREIGN KEY (film_id) REFERENCES film(film_id)
film_category - films to categories relation.
  • film_ididentifier for each film (FK)
  • category_ididentifier for each category (FK)
  • last_updatedate and time of last update
film_id category_id last_update
1 1 2023-01-01 12:00:00
  • PRIMARY KEY, btree (film_id, category_id)
  • FOREIGN KEY (film_id) REFERENCES film(film_id)
  • FOREIGN KEY (category_id) REFERENCES category(category_id)
inventory - table of items.
  • inventory_idunique record identifier (PK)
  • film_ididentifier of the film (FK)
  • store_ididentifier of the store where the inventory is located (FK)
  • last_updatedate and time of last update
inventory_id film_id store_id last_update
1 23 2 2023-01-01 12:00:00
  • PRIMARY KEY, btree (inventory_id)
  • FOREIGN KEY (film_id) REFERENCES film(film_id)
  • FOREIGN KEY (store_id) REFERENCES store(store_id)
language - films languages.
  • language_idunique record identifier (PK)
  • namelanguage name
  • last_updatedate and time of last update
language_id name last_update
1 English 2023-01-01 12:00:00
  • PRIMARY KEY, btree (language_id)
payment - customers payments.
  • payment_idunique identifier of the record (PK)
  • customer_ididentifier of the customer (FK)
  • staff_ididentifier of the staff member who received the payment (FK)
  • rental_ididentifier of the rental record (FK)
  • amountpayment amount
  • payment_datedate and time of the payment
  • last_updatedate and time of the last update
payment_id customer_id staff_id rental_id amount payment_date last_update
1 1 1 1 4.99 2023-01-01 12:13:14 2023-01-01 12:14:15
  • PRIMARY KEY, btree (payment_id)
  • FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
  • FOREIGN KEY (staff_id) REFERENCES staff(staff_id)
  • FOREIGN KEY (rental_id) REFERENCES rental(rental_id)
rental - customers rentals.
  • rental_idunique identifier of the record (PK)
  • rental_daterental start date
  • inventory_ididentifier of the disk (FK)
  • customer_ididentifier of the customer (FK)
  • return_datedate of returning the film
  • staff_idid of the staff member issued the disk (FK)
  • last_updatedate and time of the last update
rental_id rental_date inventory_id customer_id return_date staff_id last_update
1 2023-01-01 16:15:21 1 1 2023-01-10 09:12:36 1 2023-01-01 12:00:00
  • PRIMARY KEY, btree (rental_id)
  • FOREIGN KEY (inventory_id) REFERENCES inventory(inventory_id)
  • FOREIGN KEY (customer_id) REFERENCES customer(customer_id)
  • FOREIGN KEY (staff_id) REFERENCES staff(staff_id)
staff - company staff.
  • staff_idunique identifier of the record (PK)
  • first_namefirst name of the staff member
  • last_namelast name of the staff member
  • address_ididentifier of the address (FK)
  • picturephoto of the staff member
  • emailemail address of the staff member
  • store_idforeign key referencing the store table (FK)
  • activeindicator of staff member's activity (0/1)
  • usernameusername for system login
  • passwordpassword for login
  • last_updatedate and time of the last update
staff_id first_name last_name address_id picture email store_id active username password last_update
1 John Doe 1 [null] john.doe@example.com 1 1 johndoe ******** 2023-01-01 12:00:00
  • PRIMARY KEY, btree (staff_id)
  • FOREIGN KEY (address_id) REFERENCES address(address_id)
  • FOREIGN KEY (store_id) REFERENCES store(store_id)
store - company stories.
  • store_idunique identifier of the record (PK)
  • manager_staff_ididentifier of the store manager (FK)
  • address_ididentifier of the address (FK)
  • last_updatedate and time of the last update
store_id manager_staff_id address_id last_update
1 1 1 2023-01-01 12:00:00
  • PRIMARY KEY, btree (store_id)
  • FOREIGN KEY (manager_staff_id) REFERENCES staff(staff_id)
  • FOREIGN KEY (address_id) REFERENCES address(address_id)

Sample queries

These queries show how the tables are connected. Copy any of them and run it in the playground.

A film and its language: a simple many-to-one relationship.

SELECT f.title, l.name AS language, f.rental_rate, f.length
FROM film f
JOIN language l ON l.language_id = f.language_id
ORDER BY f.film_id
LIMIT 3;
titlelanguagerental_ratelength
ACADEMY DINOSAUREnglish0.9986
ACE GOLDFINGEREnglish4.9948
ADAPTATION HOLESEnglish2.9950

Where a customer lives: a chain of four tables.

SELECT c.first_name, c.last_name, ci.city, co.country
FROM customer c
JOIN address a ON a.address_id = c.address_id
JOIN city ci ON ci.city_id = a.city_id
JOIN country co ON co.country_id = ci.country_id
ORDER BY c.customer_id
LIMIT 3;
first_namelast_namecitycountry
MARYSMITHSaseboJapan
PATRICIAJOHNSONSan BernardinoUnited States
LINDAWILLIAMSAthenaiGreece

Which film was rented and how much was paid: from a rental to its film through inventory.

SELECT r.rental_date, f.title, p.amount
FROM rental r
JOIN inventory i ON i.inventory_id = r.inventory_id
JOIN film f ON f.film_id = i.film_id
JOIN payment p ON p.rental_id = r.rental_id
ORDER BY r.rental_id
LIMIT 3;
rental_datetitleamount
2005-05-24 22:53:30BLANKET BEVERLY2.99
2005-05-24 22:54:33FREAKY POCUS2.99
2005-05-24 23:03:39GRADUATE LORD3.99

SQL exercises by topic

There are 179 exercises on the Sakila database, from simple SELECT queries to analytics with window functions. Solutions are checked automatically on a real MySQL server. The number on the right is how many exercises a topic has; the colored dots show its difficulty range.

Where to start

The first exercises of the "Sakila database" section:

  1. Get the actors
  2. Retrieve Actor Names
  3. Ordered Movie Titles
  4. Top 10 Movies by Title
  5. Films List - Third Page
  6. Sort Movies by Multiple Fields
  7. The Longest Movie
  8. Identify Long Movies
  9. Find Long Comedies
  10. Classic Movies

All Sakila exercises →

FAQ

Do I need to install MySQL to use Sakila?

No. The exercises and the playground on SQLtest.online run your queries on our servers, so a browser is all you need. In the playground, Sakila is available on MySQL 8.0, MySQL 9.7 and MariaDB 10.

Where can I download the Sakila database?

The official sakila-schema.sql and sakila-data.sql files are on the MySQL example databases page, and the Sakila documentation describes them.

Is there a Sakila database for PostgreSQL?

Yes, there is a port called Pagila. The structure is the same, but some types and functions are replaced with their PostgreSQL equivalents.

Can I change the data in Sakila?

In the playground the database is read-only, so everyone sees the same data. Exercises on INSERT, UPDATE and DELETE run on a temporary copy of the table they need, and then the copy's contents are checked.

Is Sakila good for SQL interview preparation?

Yes. It is a convenient way to practice JOINs, grouping, subqueries and window functions, the topics most often asked in technical interviews.