SELECT
The most fundamental SQL statement for retrieving rows and columns from a table. It serves as the starting point for querying a database.
Syntax
-- Retrieves the specified columns. SELECT column1, column2 FROM table_name; -- Retrieves all columns. SELECT * FROM table_name; -- Retrieves a column with an alias. SELECT column_name AS alias FROM table_name; -- Retrieves unique values, excluding duplicate rows. SELECT DISTINCT column_name FROM table_name;
Syntax List
| Syntax | Description |
|---|---|
| SELECT column_name | Retrieves data from the specified column. Separate multiple columns with commas. |
| SELECT * | Retrieves all columns in the table. In production code, it is recommended to specify only the columns you need. |
| AS alias | Assigns an arbitrary alias to a retrieved column or expression. The alias appears as the column header in the result set. |
| DISTINCT | Excludes duplicate rows and returns only unique values. |
Sample Code
-- Retrieves the name and department from the employees table. SELECT name, department FROM employees; -- Retrieves all columns from the employees table. SELECT * FROM employees; -- Retrieves columns with aliases. SELECT name AS full_name, salary AS monthly_salary FROM employees; -- Retrieves a list of departments with duplicates removed. SELECT DISTINCT department FROM employees; -- You can also assign an alias to a calculated expression. SELECT name, salary * 12 AS annual_salary FROM employees;
Output
-- Example result of: SELECT name AS full_name, salary AS monthly_salary FROM employees; -- +---------------+----------------+ -- | full_name | monthly_salary | -- +---------------+----------------+ -- | Taro Tanaka | 300000 | -- | Hanako Suzuki | 280000 | -- | Ichiro Sato | 320000 | -- +---------------+----------------+
Database-Specific Syntax
The basic syntax for SELECT, AS, and DISTINCT is supported across all major databases.
SELECT name AS full_name, salary AS monthly_salary FROM employees; SELECT DISTINCT department FROM employees;
In SQL Server, you can use TOP to limit the number of rows returned (equivalent to LIMIT in MySQL).
SELECT TOP 10 name, department FROM employees;
In PostgreSQL, DISTINCT ON (column) returns only the first row for each distinct value of the specified column.
-- Retrieves the highest-paid employee from each department. SELECT DISTINCT ON (department) name, department, salary FROM employees ORDER BY department, salary DESC;
Notes
SELECT is the most frequently used statement in SQL and reads data from a table. Specifying only the columns you need reduces the amount of data transferred over the network and improves performance.
Aliases defined with AS can include non-ASCII characters, but be careful when referencing column names from application code — names that contain spaces must be wrapped in backticks (MySQL) or double quotes (standard SQL).
Use SELECT together with WHERE for filtering, ORDER BY for sorting, and LIMIT for limiting the number of rows returned.
If you find any errors or copyright issues, please contact us.