10 Beginner SQL Practice Exercises With Solutions
Table of Contents
- The Dataset
-
Exercises
- 1. Select all data about distribution companies
- 2. Select the title, IMDb rating, and release year for each movie
- 3. Select the title and earnings for movies with earnings above $300 million
- 4. Select titles and IMDb ratings for movies with ‘Godfather’ in the title
- 5. Select the title, IMDb rating, and release year for movies released before 2001 with a rating above 9
- 6. Select the title and release year for movies released after 1991, sorted by release year
- 7. Show the count of movies per each language category
- 8. Show the count of movies by release year and language, sorted by release date
- 9. Show languages and their average movie budget, including only languages with an average above $50 million
- 10. Show movie titles and the name of its distribution company
- That Was Fun! Now, Time to Do SQL Practice on Your Own!
Solve these ten SQL practice problems and test where you stand with your SQL knowledge!
Practice is the best way to learn SQL. In this article we show you ten SQL practice exercises for beginners, where you can test your basic SQL knowledge.
Use them as a practice or a way to learn new SQL concepts. For more theoretical background and (even more!) exercises, there’s our interactive SQL Basics course. It teaches you how to select data from one or more tables, aggregate and group data, write subqueries, and use set operations. The course comprises 129 interactive exercises so there is no lack of opportunities for SQL practice, especially if you add some of the 12 ways of learning SQL online to it.
Let’s start with our exercises!
The Dataset
The question is always where to find data for practicing SQL. We’ll use our dataset for all exercises. No need to limit yourself to this, though – you can find other free online datasets for practicing SQL.
Our dataset consists of two tables.
The table distribution_companies
lists movie distribution companies with the following columns:
id
– The ID of the distribution company. This is the primary key of the table.company_name
– The name of the distribution company.
The table is shown below.
id | company_name |
---|---|
1 | Columbia Pictures |
2 | Paramount Pictures |
3 | Warner Bros. Pictures |
4 | United Artists |
5 | Universal Pictures |
6 | New Line Cinema |
7 | Miramax Films |
8 | Produzioni Europee Associate |
9 | Buena Vista |
10 | StudioCanal |
The second table is movies
. These are the columns:
id
– The ID of the movie. This is the primary key of the table.movie_title
– The movie title.imdb_rating
– The movie rating on IMDb.year_released
– The year the movie was released.budget
– The budget for the movie in millions of dollars.box_office
– The earnings of the movie in millions of dollars.distribution_company_id
– The ID of the distribution company, referencing the table distribution_companies (foreign key).language
– The language(s) spoken in the movie.
The table is shown below.
id | movie_title | imdb_rating | year_released | budget | box_office | distribution_company_id | language |
---|---|---|---|---|---|---|---|
1 | The Shawshank Redemption | 9.2 | 1994 | 25.00 | 73.30 | 1 | English |
2 | The Godfather | 9.2 | 1972 | 7.20 | 291.00 | 2 | English |
3 | The Dark Knight | 9.0 | 2008 | 185.00 | 1,006.00 | 3 | English |
4 | The Godfather Part II | 9.0 | 1974 | 13.00 | 93.00 | 2 | English, Sicilian |
5 | 12 Angry Men | 9.0 | 1957 | 0.34 | 2.00 | 4 | English |
6 | Schindler's List | 8.9 | 1993 | 22.00 | 322.20 | 5 | English, German, Yiddish |
7 | The Lord of the Rings: The Return of the King | 8.9 | 2003 | 94.00 | 1,146.00 | 6 | English |
8 | Pulp Fiction | 8.8 | 1994 | 8.50 | 213.90 | 7 | English |
9 | The Lord of the Rings: The Fellowship of the Ring | 8.8 | 2001 | 93.00 | 898.20 | 6 | English |
10 | The Good, the Bad and the Ugly | 8.8 | 1966 | 1.20 | 38.90 | 8 | English, Italian, Spanish |
Exercises
1. Select all data about distribution companies
Solution:
SELECT * FROM distribution_companies;
Solution explanation: Select the data using the SELECT
statement. To select all the columns, use an asterisk (*
). The table from which the data is selected is specified in the FROM
clause.
Solution output:
id | company_name |
---|---|
1 | Columbia Pictures |
2 | Paramount Pictures |
3 | Warner Bros. Pictures |
4 | United Artists |
5 | Universal Pictures |
6 | New Line Cinema |
7 | Miramax Films |
8 | Produzioni Europee Associate |
9 | Buena Vista |
10 | StudioCanal |
2. Select the title, IMDb rating, and release year for each movie
Solution:
SELECT movie_title, imdb_rating, year_released FROM movies;
Solution explanation: List all the columns needed (movie_title
, imdb_rating
, and year_released
) in the SELECT
statement, separated by the comma. Reference the table movies
in the FROM
clause.
Solution output:
movie_title | imdb_rating | year_released |
---|---|---|
The Shawshank Redemption | 9.2 | 1994 |
The Godfather | 9.2 | 1972 |
The Dark Knight | 9.0 | 2008 |
The Godfather Part II | 9.0 | 1974 |
12 Angry Men | 9.0 | 1957 |
Schindler's List | 8.9 | 1993 |
The Lord of the Rings: The Return of the King | 8.9 | 2003 |
Pulp Fiction | 8.8 | 1994 |
The Lord of the Rings: The Fellowship of the Ring | 8.8 | 2001 |
The Good, the Bad and the Ugly | 8.8 | 1966 |
3. Select the title and earnings for movies with earnings above $300 million
Solution:
SELECT movie_title, box_office FROM movies WHERE box_office > 300;
Solution explanation: List the columns in SELECT
and reference the table in FROM
. Use a WHERE
clause to filter the data – write the column box_office
and use the ‘greater than’ operator (>
) to show only values above $300 million.
Solution output:
movie_title | box_office |
---|---|
The Dark Knight | 1,006.00 |
Schindler's List | 322.20 |
The Lord of the Rings: The Return of the King | 1,146.00 |
The Lord of the Rings: The Fellowship of the Ring | 898.20 |
4. Select titles and IMDb ratings for movies with ‘Godfather’ in the title
Solution:
SELECT movie_title, imdb_rating FROM movies WHERE movie_title LIKE '%Godfather%';
Solution explanation: List the columns in SELECT
and reference the table in the FROM
clause. Use a WHERE
clause to filter the data. After writing the column name, use the LIKE
logical operator to look for ‘Godfather’ in the movie title, written in single quotes. To find the word anywhere in the movie title, place the wildcard character (%
) before and after the word.
Solution output:
movie_title | imdb_rating |
---|---|
The Godfather | 9.2 |
The Godfather Part II | 9.0 |
5. Select the title, IMDb rating, and release year for movies released before 2001 with a rating above 9
Solution:
SELECT movie_title, imdb_rating, year_released FROM movies WHERE year_released < 2001 AND imdb_rating > 9;
Solution explanation: List the columns in SELECT
and reference the table in FROM
. Set the first condition that the year released is before 2001 using the ‘less than’ (<
) operator. To add another condition, use the AND
logical operator. Use the same logic as the first condition, this time using the ‘greater than’ operator with the column imdb_rating
.
Solution output:
movie_title | imdb_rating | year_released |
---|---|---|
The Shawshank Redemption | 9.2 | 1994 |
The Godfather | 9.2 | 1972 |
6. Select the title and release year for movies released after 1991, sorted by release year
Solution:
SELECT movie_title, year_released FROM movies WHERE year_released > 1991 ORDER BY year_released ASC;
Solution explanation: List the columns in SELECT
and reference the table in FROM
. Filter the data with WHERE
by applying the ‘greater than’ operator to the column year_released
. To sort the data, use an ORDER BY
clause and write the column name by which you wish to sort. The type of sorting is specified by writing ASC
(ascending) or DESC
(descending). If the type is omitted, the output is sorted in ascending order by default.
Solution output:
movie_title | year_released |
---|---|
Schindler's List | 1993 |
The Shawshank Redemption | 1994 |
Pulp Fiction | 1994 |
The Lord of the Rings: The Fellowship of the Ring | 2001 |
The Lord of the Rings: The Return of the King | 2003 |
The Dark Knight | 2008 |
7. Show the count of movies per each language category
Solution:
SELECT language, COUNT(*) AS number_of_movies FROM movies GROUP BY language;
Solution explanation: Select the column language
from the table movies
. To count the number of movies, use the aggregate function COUNT()
. Use the asterisk (*
) to count the rows, which equals the count of movies. To give this column a name, use the AS
keyword followed by the desired name. To show the count by language, you need to group the data by it, so write the column language
in the GROUP BY
clause.
Solution output:
language | number_of_movies |
---|---|
English | 7 |
English, German, Yiddish | 1 |
English, Sicilian | 1 |
English, Italian, Spanish | 1 |
8. Show the count of movies by release year and language, sorted by release date
Solution:
SELECT year_released, language, COUNT(*) AS number_of_movies FROM movies GROUP BY year_released, language ORDER BY year_released ASC;
Solution explanation: List the columns year_released
and language
from the table movies
in SELECT
. Use COUNT(*)
to count the number of movies and give this column a name using the AS
keyword. Specify the columns by which you want to group in the GROUP BY
clause. Separate each column name with a comma. Sort the output using ORDER BY
with the column year_released
and the ASC
keyword.
Solution output:
year_released | language | number_of_movies |
---|---|---|
1957 | English | 1 |
1966 | English, Italian, Spanish | 1 |
1972 | English | 1 |
1974 | English, Sicilian | 1 |
1993 | English, German, Yiddish | 1 |
1994 | English | 2 |
2001 | English | 1 |
2003 | English | 1 |
2008 | English | 1 |
9. Show languages and their average movie budget, including only languages with an average above $50 million
Solution:
SELECT language, AVG(budget) AS movie_budget FROM movies GROUP BY language HAVING AVG(budget) > 50;
Solution explanation: Select the column language
from the table movies
. To compute the average budget, use the aggregate function AVG()
with the column budget in parentheses. Name the column in the output by using the AS
keyword. Group the data by rating using GROUP BY
. To filter the data after grouping, use a HAVING
clause. In it, use the same AVG()
construct as in SELECT
and set the values to be above 50 using the ‘greater than’ operator.
Solution output:
language | movie_budget |
---|---|
English | 59.01 |
10. Show movie titles and the name of its distribution company
Solution:
SELECT movie_title, company_name FROM distribution_companies dc JOIN movies m ON dc.id = m.distribution_company_id;
Solution explanation: List the columns movie_title
and company_name
in SELECT
. In the FROM
clause, reference the table distribution_companies
. Give it an alias dc to shorten its name for use later. The AS
keyword is omitted here; you may use it if you wish. To access the data from the other table, use JOIN
(it may also be written as INNER JOIN
) and write the table name after it. Give this table an alias also. The join used here is an inner type of join; it returns only the rows that match the joining condition specified in the ON
clause. The tables are joined where the column id
from the table distribution_companies
is equal to the column distribution_company_id
from the table movies
. To specify which column is from which table, use the corresponding alias of each table.
Solution output:
movie_title | company_name |
---|---|
The Shawshank Redemption | Columbia Pictures |
The Godfather Part II | Paramount Pictures |
The Godfather | Paramount Pictures |
The Dark Knight | Warner Bros. Pictures |
12 Angry Men | United Artists |
Schindler's List | Universal Pictures |
The Lord of the Rings: The Fellowship of the Ring | New Line Cinema |
The Lord of the Rings: The Return of the King | New Line Cinema |
Pulp Fiction | Miramax Films |
The Good, the Bad and the Ugly | Produzioni Europee Associate |
That Was Fun! Now, Time to Do SQL Practice on Your Own!
These ten SQL practice exercises give you a taste of what practicing SQL looks like. Whether you are at the beginner, intermediate, or advanced level, it’s the same. What changes is the complexity of the problems you solve and of the code you write. Check out our SQL practice guide to learn how you can practice SQL with our platform.
Look for more challenges in the SQL Practice track that contains over 1000 SQL exercises for beginners in 10 courses. This track is particularly useful if you do not have an opportunity to use SQL on a daily basis in your job.
So, don’t try to test how long it takes to forget what you once knew in SQL! Use every opportunity to solve as many SQL practice problems as possible.