SELECT

Retrieves rows from a table or view.

Synopsis

  1. SELECT [ALL | DISTINCT [ON (<expression> [, ...])]]
  2. * | <expression> [[AS] <output_name>] [, ...]
  3. [FROM <from_item> [, ...]]
  4. [WHERE <condition>]
  5. [GROUP BY <grouping_element> [, ...]]
  6. [HAVING <condition> [, ...]]
  7. [WINDOW <window_name> AS (<window_specification>)]
  8. [{UNION | INTERSECT | EXCEPT} [ALL] <select>]
  9. [ORDER BY <expression> [ASC | DESC | USING <operator>] [, ...]]
  10. [LIMIT {<count> | ALL}]
  11. [OFFSET <start>]

where can be one of:

  1. ()
  2. <expression>
  3. ROLLUP (<expression> [,...])
  4. CUBE (<expression> [,...])
  5. GROUPING SETS ((<grouping_element> [, ...]))

where can be:

  1. [<window_name>]
  2. [PARTITION BY <expression> [, ...]]
  3. [ORDER BY <expression> [ASC | DESC | USING <operator>] [, ...]
  4. [{RANGE | ROWS}
  5. { UNBOUNDED PRECEDING
  6. | <expression> PRECEDING
  7. | CURRENT ROW
  8. | BETWEEN <window_frame_bound> AND <window_frame_bound> }]]
  9. where <window_frame_bound> can be one of:
  10. UNBOUNDED PRECEDING
  11. <expression> PRECEDING
  12. CURRENT ROW
  13. <expression> FOLLOWING
  14. UNBOUNDED FOLLOWING

where can be one of:

  1. [ONLY] <table_name> [[AS] <alias> [( <column_alias> [, ...] )]]
  2. (select) [AS] <alias> [( <column_alias> [, ...] )]
  3. <function_name> ( [<argument> [, ...]] ) [AS] <alias>
  4. [( <column_alias> [, ...]
  5. | <column_definition> [, ...] )]
  6. <function_name> ( [<argument> [, ...]] ) AS
  7. ( <column_definition> [, ...] )
  8. <from_item> [NATURAL] <join_type>
  9. <from_item>
  10. [ON <join_condition> | USING ( <join_column> [, ...] )]

Description

SELECT retrieves rows from zero or more tables. The general processing of SELECT is as follows:

  1. All elements in the FROM list are computed. (Each element in the FROM list is a real or virtual table.) If more than one element is specified in the FROM list, they are cross-joined together.
  2. If the WHERE clause is specified, all rows that do not satisfy the condition are eliminated from the output.
  3. If the GROUP BY clause is specified, the output is divided into groups of rows that match on one or more of the defined grouping elements. If the HAVING clause is present, it eliminates groups that do not satisfy the given condition.
  4. If a window expression is specified (and optional WINDOW clause), the output is organized according to the positional (row) or value-based (range) window frame.
  5. DISTINCT eliminates duplicate rows from the result. DISTINCT ON eliminates rows that match on all the specified expressions. ALL (the default) will return all candidate rows, including duplicates.
  6. The actual output rows are computed using the SELECT output expressions for each selected row.
  7. Using the operators UNION, INTERSECT, and EXCEPT, the output of more than one SELECT statement can be combined to form a single result set. The UNION operator returns all rows that are in one or both of the result sets. The INTERSECT operator returns all rows that are strictly in both result sets. The EXCEPT operator returns the rows that are in the first result set but not in the second. In all three cases, duplicate rows are eliminated unless ALL is specified.
  8. If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce.
  9. If the LIMIT or OFFSET clause is specified, the SELECT statement only returns a subset of the result rows.

You must have SELECT privilege on a table to read its values.

Parameters

The SELECT List

The SELECT list (between the key words SELECT and FROM) specifies expressions that form the output rows of the SELECT statement. The expressions can (and usually do) refer to columns computed in the FROM clause.

Using the clause [AS] , another name can be specified for an output column. This name is primarily used to label the column for display. It can also be used to refer to the column’s value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead. The AS keyword is optional in most cases (such as when declaring an alias for column names, constants, function calls, and simple unary operator expressions). In cases where the declared alias is a reserved SQL keyword, the must be enclosed in double quotes to avoid ambiguity.

An in the SELECT list can be a constant value, a column reference, an operator invocation, a function call, an aggregate expression, a window expression, a scalar subquery, and so on. There are a number of constructs that can be classified as an expression but do not follow any general syntax rules.

Instead of an expression, * can be written in the output list as a shorthand for all the columns of the selected rows. Also, you can write table_name.* as a shorthand for the columns coming from just that table.

The FROM Clause

The FROM clause specifies one or more source tables for the SELECT. If multiple sources are specified, the result is the Cartesian product (cross join) of all the sources. But usually qualification conditions are added to restrict the returned rows to a small subset of the Cartesian product. The FROM clause can contain the following elements:

The name (optionally schema-qualified) of an existing table or view. If ONLY is specified, only that table is scanned. If ONLY is not specified, the table and all its descendant tables (if any) are scanned.

A substitute name for the FROM item containing the alias. An alias is used for brevity or to eliminate ambiguity for self-joins (where the same table is scanned multiple times). When an alias is provided, it completely hides the actual name of the table or function; for example given FROM foo AS f, the remainder of the SELECT must refer to this FROM item as f not foo. If an alias is written, a column alias list can also be written to provide substitute names for one or more columns of the table.