Syntax

Updated at:
Copy as MD

Use the SELECT statement to query data from one or more tables. The statement supports filtering, grouping, sorting, and set operations.

[ WITH with_query [, ...] ]
SELECT
[ ALL | DISTINCT ] select_expr [, ...]
[ FROM table_reference [, ...] ]
[ WHERE condition ]
[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
[ HAVING condition]
[ WINDOW window_name AS (window_spec) [, window_name AS (window_spec)] ...]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY {column_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]

The clauses below are listed in the order they are evaluated:

Parameters

table_reference

The data source to query. Accepted values: a table, a view, an associated table, or a subquery.

ALL | DISTINCT

Controls whether duplicate rows appear in the result set.

  • ALL — returns all rows, including duplicates. Default.

  • DISTINCT — removes duplicate rows from the result set.

Usage notes

  • Table names and column names are case-insensitive.

  • If a table name or column name contains keywords or spaces, wrap it in backticks (`).

WHERE

Pass a BOOLEAN expression to WHERE to filter rows that match the condition.

SELECT * FROM CUSTOMER WHERE customer_id = 2368;

ALL and DISTINCT

The following two queries show the difference between ALL and DISTINCT. The first returns all rows; the second removes duplicates.

SELECT id FROM orders;
SELECT DISTINCT id FROM orders;