Back to articles list Articles Cookbook
9 minutes read

10 Beginner SQL Practice Exercises With Solutions

Solve these ten SQL practice problems and test where you stand with your SQL knowledge!

This article is all about SQL practice. It’s the best way to learn SQL. We show you ten SQL practice exercises where you need to apply essential SQL concepts. If you’re an SQL rookie, no need to worry – these examples are for beginners.

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.

Speaking of practice, 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.

idcompany_name
1Columbia Pictures
2Paramount Pictures
3Warner Bros. Pictures
4United Artists
5Universal Pictures
6New Line Cinema
7Miramax Films
8Produzioni Europee Associate
9Buena Vista
10StudioCanal

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.

idmovie_titleimdb_ratingyear_releasedbudgetbox_officedistribution_company_idlanguage
1The Shawshank Redemption9.2199425.0073.301English
2The Godfather9.219727.20291.002English
3The Dark Knight9.02008185.001,006.003English
4The Godfather Part II9.0197413.0093.002English, Sicilian
512 Angry Men9.019570.342.004English
6Schindler's List8.9199322.00322.205English, German, Yiddish
7The Lord of the Rings: The Return of the King8.9200394.001,146.006English
8Pulp Fiction8.819948.50213.907English
9The Lord of the Rings: The Fellowship of the Ring8.8200193.00898.206English
10The Good, the Bad and the Ugly8.819661.2038.908English, Italian, Spanish

Exercise 1: Selecting All Columns From a Table

Exercise: Select all data from the table 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:

idcompany_name
1Columbia Pictures
2Paramount Pictures
3Warner Bros. Pictures
4United Artists
5Universal Pictures
6New Line Cinema
7Miramax Films
8Produzioni Europee Associate
9Buena Vista
10StudioCanal

Exercise 2: Selecting a Few Columns From a Table

Exercise: For each movie, select the movie title, the IMDb rating, and the year the movie was released.

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_titleimdb_ratingyear_released
The Shawshank Redemption9.21994
The Godfather9.21972
The Dark Knight9.02008
The Godfather Part II9.01974
12 Angry Men9.01957
Schindler's List8.91993
The Lord of the Rings: The Return of the King8.92003
Pulp Fiction8.81994
The Lord of the Rings: The Fellowship of the Ring8.82001
The Good, the Bad and the Ugly8.81966

Exercise 3: Selecting a Few Columns and Filtering Numeric Data in WHERE

Exercise: Select the columns movie_title and box_office from the table movies. Show only 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_titlebox_office
The Dark Knight1,006.00
Schindler's List322.20
The Lord of the Rings: The Return of the King1,146.00
The Lord of the Rings: The Fellowship of the Ring898.20

Exercise 4: Selecting a Few Columns and Filtering Text Data in WHERE

Exercise: Select the columns movie_title, imdb_rating, and year_released from the table movies. Show movies that have the word ‘Godfather’ in the title.

Solution:

SELECT
  movie_title,
  imdb_rating,
  year_released
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_titleimdb_ratingyear_released
The Godfather9.21972
The Godfather Part II9.01974

Exercise 5: Selecting a Few Columns and Filtering Data Using Two Conditions in WHERE

Exercise: Select the columns movie_title, imdb_rating, and year_released from the table movies. Show movies that were released before 2001 and had 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_titleimdb_ratingyear_released
The Shawshank Redemption9.21994
The Godfather9.21972

Exercise 6: Filtering Data Using WHERE and Sorting the Output

Exercise: Select the columns movie_title, imdb_rating, and year_released from the table movies. Show movies released after 1991. Sort the output by the year released in ascending order.

Solution:

SELECT
  movie_title,
  imdb_rating,
  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_titleimdb_ratingyear_released
Schindler's List8.91993
The Shawshank Redemption9.21994
Pulp Fiction8.81994
The Lord of the Rings: The Fellowship of the Ring8.82001
The Lord of the Rings: The Return of the King8.92003
The Dark Knight9.02008

Exercise 7: Grouping Data by One Column

Exercise: 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:

languagenumber_of_movies
English7
English, German, Yiddish1
English, Sicilian1
English, Italian, Spanish1

Exercise 8: Grouping Data by Multiple Columns

Exercise: Show the count of movies by year released and language. Sort results by the release date in ascending order.

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_releasedlanguagenumber_of_movies
1957English1
1966English, Italian, Spanish1
1972English1
1974English, Sicilian1
1993English, German, Yiddish1
1994English2
2001English1
2003English1
2008English1

Exercise 9: Filtering Data After Grouping

Exercise: Show the languages spoken and the average movie budget by language category. Show only the languages with an average budget 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:

languagemovie_budget
English59.01

Exercise 10: Selecting Columns From Two Tables

Exercise: Show movie titles from the table movies, each with 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_titlecompany_name
The Shawshank RedemptionColumbia Pictures
The Godfather Part IIParamount Pictures
The GodfatherParamount Pictures
The Dark KnightWarner Bros. Pictures
12 Angry MenUnited Artists
Schindler's ListUniversal Pictures
The Lord of the Rings: The Fellowship of the RingNew Line Cinema
The Lord of the Rings: The Return of the KingNew Line Cinema
Pulp FictionMiramax Films
The Good, the Bad and the UglyProduzioni 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.

Look for more challenges in the SQL Basics course and the Monthly SQL Practice track. Both are excellent for your SQL practice online. This is true, especially 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.