SQL Exercises

PostgreSQL: SQL SELECT Statement Exercises (With Answers)

0 Exercises
~0 min

This guide is specifically for PostgreSQL syntax.

Learn SQL SELECT Statement with hands-on practice exercises. Each exercise includes the table schema, solution query, expected output, and detailed explanation.

What You'll Practice

These exercises cover SELECT Statement from basic to advanced concepts. Work through each problem to build your SQL skills.

Exercise 1

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

Table Schema

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

Solution

SELECT * 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

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

Table Schema

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

Solution

SELECT 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

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

Table Schema

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

Solution

SELECT 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

Question: Select unique department names from the employees table.

Table Schema

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

Solution

SELECT 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

Question: Select the first 5 employees ordered by salary descending.

Table Schema

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

Solution

SELECT * 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.

Common Mistakes to Avoid

  • Forgetting to use proper syntax for SELECT Statement
  • Not considering NULL values in comparisons
  • Confusing similar operators or clauses

Related SQL Exercises

Continue practicing with these related topics:


PostgreSQL-Specific Notes

This page covers PostgreSQL syntax. Other databases may have different syntax for similar operations.

Related Content

From Our Blog

Ready for more practice?

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