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 exercises range from level 1 to level 1; 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

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