SQL Exercises

SQL SELECT Statement Exercises (With Answers)

20 Exercises
~100 min
20 Beginner

Exercise 1

beginner

Question

Write a query to select all columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50),
  hire_date DATE
);
Show Solution

Solution

SQL
1SELECT * FROM employees;

Expected Output

| id | name | salary | department | hire_date |
|---|---|---|---|---|
| 1 | John | 50000 | Sales | 2024-01-15 |

Explanation

The asterisk (*) is a wildcard that selects all columns from the specified table. While convenient for exploration, in production code it's better to explicitly list column names.

Exercise 1

beginner

Question

Write a query to select all columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50),
  hire_date DATE
);
Show Solution

Solution

SQL
1SELECT * FROM employees;

Expected Output

| id | name | salary | department | hire_date |
|---|---|---|---|---|
| 1 | John | 50000 | Sales | 2024-01-15 |

Explanation

The asterisk (*) is a wildcard that selects all columns from the specified table. While convenient for exploration, in production code it's better to explicitly list column names.

Exercise 1

beginner

Question

Write a query to select all columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50),
  hire_date DATE
);
Show Solution

Solution

SQL
1SELECT * FROM employees;

Expected Output

| id | name | salary | department | hire_date |
|---|---|---|---|---|
| 1 | John | 50000 | Sales | 2024-01-15 |

Explanation

The asterisk (*) is a wildcard that selects all columns from the specified table. While convenient for exploration, in production code it's better to explicitly list column names.

Exercise 1

beginner

Question

Write a query to select all columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50),
  hire_date DATE
);
Show Solution

Solution

SQL
1SELECT * FROM employees;

Expected Output

| id | name | salary | department | hire_date |
|---|---|---|---|---|
| 1 | John | 50000 | Sales | 2024-01-15 |

Explanation

The asterisk (*) is a wildcard that selects all columns from the specified table. While convenient for exploration, in production code it's better to explicitly list column names.

Exercise 2

beginner

Question

Select only the 'name' and 'salary' columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT name, salary FROM employees;

Expected Output

| name | salary |
|---|---|
| John | 50000 |
| Jane | 60000 |

Explanation

Explicitly listing column names is a best practice. It improves query performance and makes your code more maintainable.

Exercise 2

beginner

Question

Select only the 'name' and 'salary' columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT name, salary FROM employees;

Expected Output

| name | salary |
|---|---|
| John | 50000 |
| Jane | 60000 |

Explanation

Explicitly listing column names is a best practice. It improves query performance and makes your code more maintainable.

Exercise 2

beginner

Question

Select only the 'name' and 'salary' columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT name, salary FROM employees;

Expected Output

| name | salary |
|---|---|
| John | 50000 |
| Jane | 60000 |

Explanation

Explicitly listing column names is a best practice. It improves query performance and makes your code more maintainable.

Exercise 2

beginner

Question

Select only the 'name' and 'salary' columns from the 'employees' table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT name, salary FROM employees;

Expected Output

| name | salary |
|---|---|
| John | 50000 |
| Jane | 60000 |

Explanation

Explicitly listing column names is a best practice. It improves query performance and makes your code more maintainable.

Exercise 3

beginner

Question

Select all employees and add a calculated column showing annual bonus (10% of salary).

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT name, salary, salary * 0.10 AS annual_bonus FROM employees;

Expected Output

| name | salary | annual_bonus |
|---|---|---|
| John | 50000 | 5000 |

Explanation

You can perform calculations in the SELECT clause and use AS to create an alias for the calculated column.

Exercise 3

beginner

Question

Select all employees and add a calculated column showing annual bonus (10% of salary).

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT name, salary, salary * 0.10 AS annual_bonus FROM employees;

Expected Output

| name | salary | annual_bonus |
|---|---|---|
| John | 50000 | 5000 |

Explanation

You can perform calculations in the SELECT clause and use AS to create an alias for the calculated column.

Exercise 3

beginner

Question

Select all employees and add a calculated column showing annual bonus (10% of salary).

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT name, salary, salary * 0.10 AS annual_bonus FROM employees;

Expected Output

| name | salary | annual_bonus |
|---|---|---|
| John | 50000 | 5000 |

Explanation

You can perform calculations in the SELECT clause and use AS to create an alias for the calculated column.

Exercise 3

beginner

Question

Select all employees and add a calculated column showing annual bonus (10% of salary).

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT name, salary, salary * 0.10 AS annual_bonus FROM employees;

Expected Output

| name | salary | annual_bonus |
|---|---|---|
| John | 50000 | 5000 |

Explanation

You can perform calculations in the SELECT clause and use AS to create an alias for the calculated column.

Exercise 4

beginner

Question

Select unique department names from the employees table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT DISTINCT department FROM employees;

Expected Output

| department |
|---|
| Sales |
| Engineering |
| Marketing |

Explanation

DISTINCT removes duplicate values from the result set. It's useful for finding unique values in a column.

Exercise 4

beginner

Question

Select unique department names from the employees table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT DISTINCT department FROM employees;

Expected Output

| department |
|---|
| Sales |
| Engineering |
| Marketing |

Explanation

DISTINCT removes duplicate values from the result set. It's useful for finding unique values in a column.

Exercise 4

beginner

Question

Select unique department names from the employees table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT DISTINCT department FROM employees;

Expected Output

| department |
|---|
| Sales |
| Engineering |
| Marketing |

Explanation

DISTINCT removes duplicate values from the result set. It's useful for finding unique values in a column.

Exercise 4

beginner

Question

Select unique department names from the employees table.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT DISTINCT department FROM employees;

Expected Output

| department |
|---|
| Sales |
| Engineering |
| Marketing |

Explanation

DISTINCT removes duplicate values from the result set. It's useful for finding unique values in a column.

Exercise 5

beginner

Question

Select the first 5 employees ordered by salary descending.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

Expected Output

| id | name | salary |
|---|---|---|
| 3 | Alice | 75000 |
| 2 | Bob | 65000 |

Explanation

ORDER BY sorts results (DESC for descending). LIMIT restricts the number of rows returned. This pattern is common for 'top N' queries.

Exercise 5

beginner

Question

Select the first 5 employees ordered by salary descending.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

Expected Output

| id | name | salary |
|---|---|---|
| 3 | Alice | 75000 |
| 2 | Bob | 65000 |

Explanation

ORDER BY sorts results (DESC for descending). LIMIT restricts the number of rows returned. This pattern is common for 'top N' queries.

Exercise 5

beginner

Question

Select the first 5 employees ordered by salary descending.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

Expected Output

| id | name | salary |
|---|---|---|
| 3 | Alice | 75000 |
| 2 | Bob | 65000 |

Explanation

ORDER BY sorts results (DESC for descending). LIMIT restricts the number of rows returned. This pattern is common for 'top N' queries.

Exercise 5

beginner

Question

Select the first 5 employees ordered by salary descending.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  salary DECIMAL(10,2)
);
Show Solution

Solution

SQL
1SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

Expected Output

| id | name | salary |
|---|---|---|
| 3 | Alice | 75000 |
| 2 | Bob | 65000 |

Explanation

ORDER BY sorts results (DESC for descending). LIMIT restricts the number of rows returned. This pattern is common for 'top N' queries.

Related Content

From Our Blog

Ready for more practice?

Join SQL Mastery and get access to interactive exercises, quizzes, and more.