This guide is specifically for SQL Server syntax.
Beginner-level SQL SELECT Statement practice exercises with solutions.
Exercise 1
Question: Write a query to select all columns from the 'employees' table.
SELECT * FROM employees;
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.
SELECT name, salary FROM employees;
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).
SELECT name, salary, salary * 0.10 AS annual_bonus FROM employees;
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.
SELECT DISTINCT department FROM employees;
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.
SELECT * FROM employees ORDER BY salary DESC TOP 5;
ORDER BY sorts results (DESC for descending). TOP restricts the number of rows returned. This pattern is common for 'top N' queries.
SQL Server-Specific Notes
This page covers SQL Server syntax. Other databases may have different syntax for similar operations.