10 SQL Code Challenges for Beginners

0
680
10 SQL Code Challenges for Beginners

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:

id

Value

1

-56

2

76

3

-84

4

96

5

-47

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:

Division id

Year

Revenue

1

2018

60

1

2021

40

1

2020

70

2

2021

-10

3

2018

20

3

2016

40

4

2021

50

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:

1, 4

3. Find the century for the year

In this challenge, your database table looks like this:

id

Year

1

1776

2

2001

3

1643

4

1865

5

1969

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:

id

Name

Age

1

Bob

21

2

Sam

19

3

Jill

18

4

Jim

21

5

Sally

19

6

Jess

20

7

Will

21

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:

id

Name

1

Bob

2

Sam

3

Jill

4

Jim

5

Sally

6

Jess

7

Will

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:

id

Name

Amount Sold

1

Cup

11

2

Saucer

22

3

Plate

46

4

Fork

34

5

Spoon

45

6

Knife

78

7

Mug

23

8

Glass

64

9

Tumbler

24

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:

id

Name

Country

1

Bob Smith

United States

2

Jim Jones

China

3

Sam White

Japan

4

Jess Black

Canada

5

Will Wilson

Germany

6

Wilson Scott

England

7

Scott Daniels

France

8

Daniel Jackson

Canada

9

Jack Johnson

United States

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:

id

Name

Age

1

Bob

21

2

Sam

19

3

Jill

18

4

Jim

21

5

Sally

19

6

Jess

20

7

Will

21

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:

id

Name

Age

1

Bob

21

2

Sam

19

3

Jill

18

4

Jim

21

5

Sally

19

6

Jess

20

7

Will

21

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?


SQL Courses & Tutorials | Codecademy

SQL is the standard relational data management language. We live in a data-driven world, and there are many businesses that store their information inside large, relational databases. This makes SQL a great skill not only for data scientists and engineers, but for anyone wanting to be data-literate.