Practice makes perfect, and practice is a necessity when you’re learning to code. Courses and books on coding will teach you the fundamentals you need to get started, but writing code will lock all those concepts in place.
Code challenges are a great way to practice your coding skills. They’re small problems designed to teach you a programming concept that can usually be solved with a few lines of code. Even though these types of problems are bite-sized, they can be fun and truly challenge your skills.
In this article, we have 10 SQL code challenges that are suitable for beginner developers who are learning SQL. If you already know SQL, you can start on them today and try a few each day to practice your database query skills. If you don’t know SQL yet, check out What is SQL to get an idea of what it is, and at the end of this article, we’ll show you how to start learning more about it.
10 top SQL beginner code challenges
These SQL challenges are suitable for beginner programmers, but you’ll need to know the basics of using SQL to complete them. If you know some SQL but want to refresh your knowledge, our Learn SQL course is a great option.
Since this is SQL, each query will need a database and one or more tables to work with. In each challenge, we’ll give you the challenge and the table structures necessary to come up with the solution.
You can use the type of development environment that suits you best. Either use a locally running database or create the tables and run the queries in one of the many online tools that allow you to run SQL online, like SQL Fiddle.
1. Select each number as its opposite
In this challenge, your database table is structured like this:
The challenge is to write a SQL query for this table that’ll return all the opposites of the values in the Value column. If a number is negative, make it positive and vice versa. Here’s what your query should return:
56, -76, 84, -96, 47
2. Select all the divisions that have had revenue this year
In this challenge, your database table is:
Your task is to write a query for this table that’ll return just the division ids for all the divisions that had positive revenue in 2021. Your query should return the following values:
3. Find the century for the year
In this challenge, your database table looks like this:
Write a query for the table above that’ll return the century that the year is in. The results of this query should have the following values:
18, 21, 17, 19, 20
4. Even or odd
Here’s the database table you’ll use in this challenge:
Your job is to return whether or not the number in the Value column is even or odd rather than returning the number itself. Your query should return the following values:
even, odd, odd, even, odd
5. Group by age
This is the table structure that you’ll use for this SQL challenge:
The challenge is to write a query that’ll group all the people by their age, along with a count of the people that are the same age. Here’s what the result set should look like:
6. Return a greeting string
Here’s the table for this challenge:
Your challenge is to return a result set that has a column called Greeting in it. This column will take the value from the Name column and return a string like this, with the name inserted into it:
Hi, Bob! How are you today?
7. Find the best selling products
Use this data in your table for this challenge:
Write a query that returns only the top five selling items in the table.
8. Who needs a passport
Here’s the data for this challenge:
The challenge here is to assume that you’re a Canadian border guard. Citizens from the United States and Canada don’t need a passport to cross the border. Write a query to find all the names that need a passport to cross.
9. Just the unique values
For this challenge, let’s reuse this table:
For this challenge, write a query that’ll return all unique ages from the table. Your result should have these values:
18, 19, 20, 21
10. Add the ages
For this challenge, use the same table again:
Your task for this challenge is to return a sum of all the ages in the table. Your query should return 139.
Improve your SQL skills with courses and other challenges
If you like testing your SQL skills with these challenges, consider checking out more of our coding challenges here. Competing in our weekly and daily code challenges is a great way to keep your SQL skills in top condition while learning even more things you can do with the language.
If you need to brush up on your SQL skills to complete these challenges — or want to explore what else you can do with SQL — consider taking one of our SQL coding courses. If you’re a beginner or you’re a little rusty, you can check out Learn SQL. This course will teach you how to interact with relational databases using SQL and is suitable for beginners. If you want to take your SQL studies a step further, our Analyze Data with SQL course will teach you not only advanced SQL queries but also data science concepts. Why not get started today?