Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. SELECT

SELECT

Since: SQL-92(1992)

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

SyntaxDescription
SELECT column_nameRetrieves 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 aliasAssigns an arbitrary alias to a retrieved column or expression. The alias appears as the column header in the result set.
DISTINCTExcludes duplicate rows and returns only unique values.

Sample Code

The following 'employees' table is used in the examples below.

employees id name department salary 1 user_a org_a 300000 2 user_c org_b 280000 3 user_e org_a 320000 3 rows in set

Retrieves the name and department from the employees table.

sample_select.sql
SELECT name, department FROM employees;
+--------+------------+
| name   | department |
+--------+------------+
| user_a | org_a      |
| user_c | org_b      |
| user_e | org_a      |
+--------+------------+
3 rows in set

Retrieves all columns from the employees table.

sample_select.sql
SELECT * FROM employees;
+----+--------+------------+--------+
| id | name   | department | salary |
+----+--------+------------+--------+
|  1 | user_a | org_a      | 300000 |
|  2 | user_c | org_b      | 280000 |
|  3 | user_e | org_a      | 320000 |
+----+--------+------------+--------+
3 rows in set

Retrieves columns with aliases.

sample_select.sql
SELECT name AS full_name, salary AS monthly_salary FROM employees;
+-----------+----------------+
| full_name | monthly_salary |
+-----------+----------------+
| user_a    |         300000 |
| user_c    |         280000 |
| user_e    |         320000 |
+-----------+----------------+
3 rows in set

Retrieves a list of departments with duplicates removed.

sample_select.sql
SELECT DISTINCT department FROM employees;
+------------+
| department |
+------------+
| org_a      |
| org_b      |
+------------+
2 rows in set

You can also assign an alias to a calculated expression.

sample_select.sql
SELECT name, salary * 12 AS annual_salary FROM employees;
+--------+---------------+
| name   | annual_salary |
+--------+---------------+
| user_a |       3600000 |
| user_c |       3360000 |
| user_e |       3840000 |
+--------+---------------+
3 rows in set

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 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).

MySQL (backticks)

SELECT name AS `full name` FROM employees;

Standard SQL / PostgreSQL (double quotes)

SELECT name AS "full name" FROM employees;

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 .