SQL exercises with solutions: SELECT, aliases and DISTINCT

SQL exercises with solutions: SELECT, aliases and DISTINCT

Updated on

SELECT picks the columns to show; AS renames a column, DISTINCT removes duplicates. These 4 game exercises are level 1, followed by 5 exercises from the course; they use the syntax of SQLite, SpeedQL’s SQL engine.

Tip: List the columns you need rather than SELECT *: the result is easier to read and faster.

Read the “SELECT, aliases, DISTINCT” card in the cheat sheet

Exercise 1 · level 1

Show every column and every row of the employees table.

Table employees (4 rows)
idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000
Show the hint

Topics to use: SELECT, aliases, DISTINCT.

Query structure:

SELECT *
FROM …
Show the solution
SELECT *
FROM employees;

Expected result (4 rows):

idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000

Exercise 2 · level 1

Show only the name and the salary of every employee.

Table employees (4 rows)
idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000
Show the hint

Topics to use: SELECT, aliases, DISTINCT.

Query structure:

SELECT …, …
FROM …
Show the solution
SELECT name, salary
FROM employees;

Expected result (4 rows):

namesalary
Alice32000
Bob41000
Claire38000
David29000

Exercise 3 · level 1

Show the employees' names in a column called employee, and their salaries in a column called annual_salary.

Table employees (4 rows)
idnamedepartmentsalary
1AliceFinance32000
2BobIT41000
3ClaireFinance38000
4DavidHR29000
Show the hint

Topics to use: SELECT, aliases, DISTINCT.

Query structure:

SELECT … AS …, … AS …
FROM …
Show the solution
SELECT name AS employee, salary AS annual_salary
FROM employees;

Expected result (4 rows):

employeeannual_salary
Alice32000
Bob41000
Claire38000
David29000

Exercise 4 · level 1

Show the list of product categories, without duplicates.

Table products (6 rows)
idnamecategorypricestock
1KeyboardOffice2540
2MouseOffice150
3ScreenDisplay18012
4HeadsetAudio608
5WebcamOffice450
6SpeakerAudio3525
Show the hint

Topics to use: SELECT, aliases, DISTINCT.

Query structure:

SELECT DISTINCT …
FROM …
Show the solution
SELECT DISTINCT category
FROM products;

Expected result (3 rows):

category
Office
Display
Audio

More exercises: level 1 of the SQL course, “Reading a table with SELECT”

These 5 exercises come from level 1 of the course, on other tables. Each one has written hints and its solution, folded by default.

Exercise 5 · course, level 1 · Guided

Show every column of the movies table.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint: The star * replaces the list of all the columns.

Show the solution
SELECT *
FROM movies;

Expected result (10 rows):

idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL

SELECT * is the quickest way to discover what a table contains.

Exercise 6 · course, level 1 · Practice

Show the title and genre of each movie.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint 1 · lead: List the columns you want right after SELECT.

Hint 2 · method: Separate the two columns with a comma: SELECT title, genre …

Show the solution
SELECT title, genre
FROM movies;

Expected result (10 rows):

titlegenre
Night TrainThriller
Blue HarborDrama
Paper Moon CityComedy
Silent PeakDrama
Last SignalSci-Fi
Summer KeysComedy
Iron GardenSci-Fi
Dust and GoldWestern
The Quiet HourDrama
Deep CurrentThriller

Columns appear in the order you write them.

Exercise 7 · course, level 1 · Practice

Show the title of each movie in a column named film, and its duration in a column named minutes.

The requested column names matter.

Table movies (10 rows)
idtitlegenreyeardurationratingdirector_id
1Night TrainThriller20151187.81
2Blue HarborDrama20181027.12
3Paper Moon CityComedy2012956.45
4Silent PeakDrama20201318.23
5Last SignalSci-Fi20191427.51
6Summer KeysComedy2016885.94
7Iron GardenSci-Fi202112583
8Dust and GoldWestern20141106.82
9The Quiet HourDrama2022977.45
10Deep CurrentThriller20171056.9NULL
Show the hints

Hint 1 · lead: An alias goes right after the column it renames.

Hint 2 · method: Add AS film after title, and AS minutes after duration.

Show the solution
SELECT title AS film, duration AS minutes
FROM movies;

Expected result (10 rows):

filmminutes
Night Train118
Blue Harbor102
Paper Moon City95
Silent Peak131
Last Signal142
Summer Keys88
Iron Garden125
Dust and Gold110
The Quiet Hour97
Deep Current105

An alias only changes the displayed name: the table itself is not modified.

Exercise 8 · course, level 1 · Practice

Show the name and price of each dish, plus the price for two people in a column named price_for_two.

The requested column names matter.

Table dishes (8 rows)
idnamecategorypricecaloriesvegetarianchef_id
1Ratatouillemain14.5320yes1
2Beef burgermain16780no2
3Caesar saladstarter9.5410no1
4Tomato soupstarter7180yes3
5Chocolate cakedessert6.5520yes2
6Lemon tartdessert6430yes3
7Grilled salmonmain19560no1
8Onion soupstarter8290yes2
Show the hints

Hint 1 · lead: A calculation can be written directly in the SELECT list, like a column.

Hint 2 · method: A computed column: price * 2 AS price_for_two.

Show the solution
SELECT name, price, price * 2 AS price_for_two
FROM dishes;

Expected result (8 rows):

namepriceprice_for_two
Ratatouille14.529
Beef burger1632
Caesar salad9.519
Tomato soup714
Chocolate cake6.513
Lemon tart612
Grilled salmon1938
Onion soup816

The calculation is done again for every row of the table.

Exercise 9 · course, level 1 · Challenge

For each flight, show the airline, the flight's revenue (revenue: price × seats sold) and the revenue lost to empty seats (lost_revenue: price × empty seats).

The requested column names matter.

Table flights (12 rows)
idairlineorigindestdepartsduration_minpriceseats_soldcapacity
1SkyJetCDGMAD2025-06-01 08:1012589150180
2AirNovaCDGLIS2025-06-01 11:40155120160170
3BlueWingLYSFCO2025-06-02 07:309575110150
4SkyJetMADCDG2025-06-02 18:2012095170180
5AirNovaBERCDG2025-06-03 09:05110105140160
6BlueWingNCEBER2025-06-03 13:5013014090150
7SkyJetCDGFCO2025-06-04 06:4513599175180
8AirNovaLISMAD2025-06-04 16:15756560120
9BlueWingFCOLYS2025-06-05 20:3010082130150
10SkyJetCDGBER2025-06-05 10:00105110120180
11AirNovaMADLIS2025-06-06 12:25807095120
12BlueWingLYSMAD2025-06-06 15:4011579100150
Show the hints

Hint 1 · lead: First work out the number of empty seats, then multiply it by the price. Mind the order of operations.

Hint 2 · method: Empty seats are capacity - seats_sold: put this calculation in parentheses before multiplying it by price.

Show the solution
SELECT airline,
       price * seats_sold AS revenue,
       price * (capacity - seats_sold) AS lost_revenue
FROM flights;

Expected result (12 rows):

airlinerevenuelost_revenue
SkyJet133502670
AirNova192001200
BlueWing82503000
SkyJet16150950
AirNova147002100
BlueWing126008400
SkyJet17325495
AirNova39003900
BlueWing106601640
SkyJet132006600
AirNova66501750
BlueWing79003950

Without parentheses, price * capacity - seats_sold computes (price × capacity) − seats_sold: multiplication comes before subtraction.

Practice with automatic checking

In SpeedQL, you write your query and it is checked straight away, on these tables and then on a hidden dataset.

Practice on the 4 “SELECT, aliases, DISTINCT” questions