The SELECT statement is used to fetch data from a database. It is the most frequently used SQL command.
Syntax
SELECT column1, column2, ...
FROM table_name;
You can select all columns using *, but explicitly naming columns is faster and more maintainable in production queries.
SELECT with Expressions
You are not limited to column names. You can use expressions, calculations, and string operations directly in SELECT:
SELECT name, salary * 12 AS annual_salary, UPPER(name) AS upper_name
FROM employees;
Aliases
Aliases rename a column or table for the duration of a query. They make output more readable and are required when using expressions.
SELECT salary AS monthly_income FROM employees;
The AS keyword is optional but recommended for clarity.
DISTINCT
DISTINCT removes duplicate rows from the result set:
SELECT DISTINCT department_id FROM employees;
It operates on all selected columns — if you select two columns, only rows where both columns are identical are collapsed.
SELECT TOP / LIMIT / FETCH
Different databases use different syntax to limit results:
| Database | Syntax |
|---|---|
| MySQL / PostgreSQL | SELECT * FROM table LIMIT 5; |
| SQL Server | SELECT TOP 5 * FROM table; |
| Oracle | SELECT * FROM table FETCH FIRST 5 ROWS ONLY; |
Execution Order (Logical)
Knowing the order in which SQL processes your query helps you write correct and efficient statements:
FROM— identify the source tableWHERE— filter rowsGROUP BY— group rowsHAVING— filter groupsSELECT— pick columns and apply aliasesORDER BY— sort the final resultLIMIT / OFFSET— paginate
This is why you cannot use a column alias defined in SELECT inside the WHERE clause — WHERE runs before SELECT.
Q: What is the purpose of the SELECT statement?
A: The SELECT statement is used to retrieve specific columns of data from one or more tables in a database.
Q: What is the difference between SQL statement and query?
A:
- SQL Statement: Any valid SQL command that can be executed (e.g.,
CREATE,INSERT,UPDATE,DROP,SELECT). - Query: Specifically refers to a statement that retrieves data (i.e., the
SELECTstatement).
Q: What is the execution order of a SELECT query?
A: The logical execution order is: FROM -> JOIN -> WHERE -> GROUP BY -> HAVING -> SELECT -> DISTINCT -> ORDER BY -> LIMIT.
Q: Can you use WHERE on an aliased column?
A: No. Since WHERE executes before SELECT, the alias does not yet exist. You must use the original column name or a subquery/CTE.
1. Select all columns from employees.
SELECT * FROM employees;2. Select only name and salary from employees.
SELECT name, salary FROM employees;3. Rename salary as annual_salary (Aliases).
SELECT salary AS annual_salary FROM employees;4. Select employees hired after 2022.
SELECT * FROM employees
WHERE hire_date > '2022-12-31';5. Select all unique department IDs.
SELECT DISTINCT department_id FROM employees;6. Use an expression in SELECT.
SELECT name, salary, salary * 0.1 AS tax FROM employees;7. Select first 3 employees by ID.
SELECT * FROM employees ORDER BY employee_id LIMIT 3;Premium Content
Unlock SELECT Statement and all premium lessons with a subscription.
From ₹199.99/year — See plans