18th Apr 2023 Updated: 25th Oct 2024 8 minutes read 10 Beginner SQL Practice Exercises With Solutions Tihomir Babic online practice sql practice 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. 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 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: idcompany_name 1Columbia Pictures 2Paramount Pictures 3Warner Bros. Pictures 4United Artists 5Universal Pictures 6New Line Cinema 7Miramax Films 8Produzioni Europee Associate 9Buena Vista 10StudioCanal 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_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 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_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 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_titleimdb_rating The Godfather9.2 The Godfather Part II9.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_titleimdb_ratingyear_released The Shawshank Redemption9.21994 The Godfather9.21972 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_titleyear_released Schindler's List1993 The Shawshank Redemption1994 Pulp Fiction1994 The Lord of the Rings: The Fellowship of the Ring2001 The Lord of the Rings: The Return of the King2003 The Dark Knight2008 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: languagenumber_of_movies English7 English, German, Yiddish1 English, Sicilian1 English, Italian, Spanish1 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_releasedlanguagenumber_of_movies 1957English1 1966English, Italian, Spanish1 1972English1 1974English, Sicilian1 1993English, German, Yiddish1 1994English2 2001English1 2003English1 2008English1 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: languagemovie_budget English59.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_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. 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. Tags: online practice sql practice