This topic describes the syntax of the SELECT statement for querying data in Interactive Analytics.
Synopsis
SELECT [ALL | DISTINCT [ON (expression [, ...])]]
* | expression [[AS] output_name] [, ...]
[FROM from_item [, ...]]
[WHERE condition]
[GROUP BY grouping_element [, ...]]
[HAVING condition [, ...]]
[{UNION | INTERSECT | EXCEPT} [ALL] select]
[ORDER BY expression [ASC | DESC | USING operator] [, ...]]
[LIMIT {count | ALL}]
- grouping_element contains:
expression
- from_item contains:
table_name [[AS] alias [( column_alias [, ...] )]] (select) [AS] alias [( column_alias [, ...] )] from_item [NATURAL] join_type from_item [ON join_condition | USING ( join_column [, ...] )]
Description
- 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, the union of the results is returned.
- If the WHERE clause is specified, all rows that do not satisfy the condition are eliminated from the output.
- If the
GROUP BY
clause is specified, or if aggregate functions are called, the output is combined to groups of rows that match one or more values, and the results of aggregate functions are computed. If the HAVING clause is specified, it eliminates groups that do not satisfy the given condition. - The actual output rows are computed using the SELECT output expressions for each selected row or row group.
SELECT DISTINCT
eliminates duplicate rows from the result.SELECT DISTINCT ON
eliminates rows that match all the specified expressions.SELECT ALL
(the default) will return all candidate rows, including duplicates.- 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. The noise word DISTINCT can be added to explicitly specify eliminating duplicate rows. Notice that DISTINCT is the default behavior here, even though ALL is the default for SELECT itself.
- If the
ORDER BY
clause is specified, the returned rows are sorted in the specified order. IfORDER BY
is not given, the rows are returned in whatever order the system finds fastest to produce. - If the LIMIT (or FETCH FIRST) or OFFSET clause is specified, the SELECT statement only returns a subset of the result rows.
Parameters
- WITH list
The WITH list exists before the SELECT statement or serves as a subquery of the SELECT clause. It is usually located before the SELECT statement to define subqueries and declare a name and the names of returned columns for each subquery. Each WITH clause is defined as a common table expression (CTE). The syntax is as follows:
with_query_name [ ( column_name [, ...] ) ] AS ( select )
The
with_query_name
parameter specifies the name of the current CTE, which can be any valid identifier. Thecolumn_name
list specifies the names of the columns returned by a subquery. Similar to the semantics of AS in the SELECT clause, a subquery can be a regular SELECT query.Separate CTEs with commas (,). Subsequent CTEs can reference the previously defined CTEs. However, CTEs cannot be called recursively. In subsequent queries,
with_query_name
can be used as a view. If thecolumn_name
list is not specified, thecolumn_name
list of the view contains the names of the columns returned by the corresponding subquery. If thecolumn_name
list is specified, you must use the specifiedcolumn_name
list. In addition, make sure that the number of columns in thecolumn_name
list is the same as the number of columns returned by the SELECT statement of the subquery. - SELECT list
The SELECT list (between the keywords SELECT and FROM) specifies expressions that form the output rows of the SELECT statement. The expressions can (and usually do) indicate columns computed in the FROM clause.
Just as in a table, every output column of a SELECT has a name. In a simple SELECT, this name is only used to label the column for display. However, when the SELECT is a subquery of a larger query, the name is seen by the larger query as the column name of the virtual table produced by the subquery. To specify the name to use for an output column, write
AS output_name
after the column's expression. You can omit AS, but only if the desired output name does not match any PostgreSQL keyword. For protection against possible future keyword additions, we recommend that you always either write AS or double-quote the output name. If you do not specify a column name, a name is chosen automatically by PostgreSQL. If the column's expression is a simple column reference, the chosen name is the same as that column's name. In more complex cases, a function or type name may be used, or the system may fall back on a generated name such ascolumn
.An output column's name can be used to indicate the column's value in
ORDER BY
andGROUP BY
clauses, but not in the WHERE or HAVING clauses, where you must write out the expression instead.Instead of an expression, an asterisk (*) 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 only that table. In these cases, you cannot specify new names with AS. The output column names will be the same as the table columns' names. - 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. However, usually qualification conditions are added (through WHERE) to restrict the returned rows to a small subset of the Cartesian product.
The FROM clause can contain the following elements:
table_name: the name (optionally schema-qualified) of an existing table or view.
alias: 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.select: A sub-SELECT can appear in the FROM clause. This acts as though its output were created as a temporary table for the duration of this single SELECT statement. Note that the sub-SELECT must be surrounded by parentheses, and an alias must be provided for it. A VALUES statement can also be used here.
function_name: Function calls can appear in the FROM clause. This is especially useful for functions that return result sets, but any function can be used. This acts as though the function's output were created as a temporary table for the duration of this single SELECT statement.
An alias can be provided in the same way as for a table. If an alias is written, a column alias list can also be written to provide substitute names for one or more attributes of the function's composite return type, including the column added by ORDINALITY if present.
Multiple function calls can be combined into a single FROM-clause item by surrounding them with
ROWS FROM( ... )
. The output of such an item is the concatenation of the first row from each function, then the second row from each function, and so on. If some of the functions produce fewer rows than others, null values are substituted for the missing data, so that the total number of rows returned is always the same as for the function that produced the most rows.join_type [ INNER ] JOIN LEFT [ OUTER ] JOIN RIGHT [ OUTER ] JOIN FULL [ OUTER ] JOIN CROSS JOIN
For the INNER and OUTER join types, a join condition must be specified, namely exactly one of
NATURAL, ON join_condition, or USING (join_column [, ...])
. See below for the meaning. ForCROSS JOIN
, none of these clauses can appear. A JOIN clause combines two FROM items, which for convenience we will refer to as "tables", though in reality they can be any type of FROM item. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case, JOIN binds more tightly than the commas separating FROM-list items.CROSS JOIN
andINNER JOIN
produce a simple Cartesian product, the same result as you get from listing the two tables at the top level of FROM, but restricted by the join condition (if any).CROSS JOIN
is equivalent toINNER JOIN ON (TRUE)
, that is, no rows are removed by qualification. These join types are only for a notational convenience, since they do nothing you cannot do with plain FROM and WHERE.LEFT OUTER JOIN
returns all rows in the qualified Cartesian product (that is, all combined rows that pass its join condition), plus one copy of each row in the left-hand table for which there was no right-hand row that passed the join condition. This left-hand row is extended to the full width of the joined table by inserting null values for the right-hand columns. Note that only the JOIN clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards.Conversely,
RIGHT OUTER JOIN
returns all the joined rows, plus one row for each unmatched right-hand row (extended with nulls on the left). This is only a notational convenience, since you could convert it to aLEFT OUTER JOIN
by switching the left and right tables.FULL OUTER JOIN
returns all the joined rows, plus one row for each unmatched left-hand row (extended with nulls on the right), plus one row for each unmatched right-hand row (extended with nulls on the left).join_condition
is an expression resulting in a value of type boolean (similar to a WHERE clause) that specifies which rows in a join are considered to match.A clause of the form
USING ( a, b, ... )
is shorthand forON left_table.a = right_table.a AND left_table.b = right_table.b ...
. Also, USING implies that only one of each pair of equivalent columns will be included in the join output, not both.NATURAL is shorthand for a USING list that mentions all columns in the two tables that have matching names.
- WHERE clause
The optional WHERE clause has this general form:
WHERE condition
In this clause, condition is any expression that evaluates to a result of type boolean. Any row that does not satisfy this condition will be eliminated from the output. A row satisfies the condition if it returns true when the actual row values are substituted for any variable references.
- GROUP BY clause
The optional
GROUP BY
clause has this general form:GROUP BY grouping_element [, ...]
GROUP BY
will condense into a single row all selected rows that share the same values for the grouped expressions. An expression used inside agrouping_element
can be an input column name, the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, aGROUP BY
name will be interpreted as an input-column name rather than an output column name.If any of
GROUPING SETS, ROLLUP
or CUBE are present as grouping elements, theGROUP BY
clause as a whole defines some number of independent grouping sets. The effect of this is equivalent to constructing a UNION ALL between subqueries with the individual grouping sets as theirGROUP BY
clauses.Aggregate functions, if any are used, are computed across all rows making up each group, producing a separate value for each group. If there are aggregate functions but no
GROUP BY
clause, the query is treated as having a single group comprising all the selected rows. The set of rows fed to each aggregate function can be further filtered by attaching a FILTER clause to the aggregate function call. When a FILTER clause is present, only those rows matching it are included in the input to that aggregate function.When
GROUP BY
is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions or when the ungrouped column is functionally dependent on the grouped columns, since there would otherwise be more than one possible value to return for an ungrouped column. A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column.Keep in mind that all aggregate functions are evaluated before evaluating any "scalar" expressions in the HAVING clause or SELECT list. This means that, for example, a CASE expression cannot be used to skip evaluation of an aggregate function.
- DISTINCT clause
If
SELECT DISTINCT
is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates).SELECT ALL
specifies the opposite: all rows are kept; that is the default.SELECT DISTINCT ON (expression [, ...])
keeps only the first row of each set of rows where the given expressions evaluate to equal. TheDISTINCT ON
expressions are interpreted using the same rules as for ORDER BY (see above). Note that the "first row" of each set is unpredictable unlessORDER BY
is used to make sure that the desired row appears first. Example:SELECT DISTINCT ON (location) location, time, report FROM weather_reports ORDER BY location, time DESC;
This retrieves the most recent weather report for each location. However, if you do not use
ORDER BY
to force descending order of time values for each location, you will get a report from an unpredictable time for each location.The
DISTINCT ON
expression must match the leftmostORDER BY
expression. TheORDER BY
clause will normally contain additional expressions that determine the desired precedence of rows within eachDISTINCT ON
group. - COUNT DISTINCT clause
COUNT DISTINCT
counts the number of unique values in a column. A value that appears multiple times in the column is counted only once. Null values are not counted. This clause is similar to the COUNT function.The sample syntax for accurate counting is as follows:SELECT c1, COUNT(DISTINCT c2) FROM table GROUP BY c1
AccurateCOUNT DISTINCT
consumes many resources. Interactive Analytics also supports non-accurateCOUNT DISTINCT
. The sample syntax is as follows:SELECT c1, approx_count_distinct(c2) FROM table GROUP BY c1
- UNION clause
select_statement UNION [ ALL | DISTINCT ] select_statement
select_statement
is any SELECT statement without anORDER BY, LIMIT, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE
, orFOR KEY SHARE
clause. (ORDER BY
and LIMIT can be attached to a subexpression if it is enclosed in parentheses. Without parentheses, these clauses will be taken to apply to the result of the UNION, not to its right-hand input expression.)The UNION operator computes the set union of the rows returned by the involved SELECT statements. A row is in the set union of two result sets if it appears in at least one of the result sets. The two SELECT statements that represent the direct operands of the UNION must produce the same number of columns, and corresponding columns must be of compatible data types.
The result of UNION does not contain any duplicate rows unless the ALL option is specified. ALL prevents elimination of duplicates. Therefore, UNION ALL is usually significantly quicker than UNION. Use ALL whenever possible. DISTINCT can be written to explicitly specify the default behavior of eliminating duplicate rows.
Multiple UNION operators in the same SELECT statement are evaluated left to right, unless otherwise indicated by parentheses.
- INTERSECT clause
The INTERSECT clause has this general form:
select_statement INTERSECT [ ALL | DISTINCT ] select_statement
select_statement
is any SELECT statement without anORDER BY or LIMIT
clause.The INTERSECT operator computes the set intersection of the rows returned by the involved SELECT statements. A row is in the intersection of two result sets if it appears in both result sets.
The result of INTERSECT does not contain any duplicate rows unless the ALL option is specified. With ALL, a row that has m duplicates in the left table and n duplicates in the right table will appear min(m,n) times in the result set. DISTINCT can be written to explicitly specify the default behavior of eliminating duplicate rows.
Multiple INTERSECT operators in the same SELECT statement are evaluated left to right, unless parentheses dictate otherwise. INTERSECT binds more tightly than UNION. That is,
A UNION B INTERSECT C
will be read asA UNION (B INTERSECT C)
. - EXCEPT clause
The EXCEPT clause has this general form:
select_statement EXCEPT [ ALL | DISTINCT ] select_statement
select_statement
is any SELECT statement without anORDER BY or LIMIT
clause.The EXCEPT operator computes the set of rows that are in the result of the left SELECT statement but not in the result of the right one.
The result of EXCEPT does not contain any duplicate rows unless the ALL option is specified. With ALL, a row that has m duplicates in the left table and n duplicates in the right table will appear max(m-n,0) times in the result set. DISTINCT can be written to explicitly specify the default behavior of eliminating duplicate rows.
Multiple EXCEPT operators in the same SELECT statement are evaluated left to right, unless parentheses dictate otherwise. EXCEPT binds at the same level as UNION.
Currently,
FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE
andFOR KEY SHARE
cannot be specified either for an EXCEPT result or for any input of an EXCEPT. - ORDER BY clause
The optional
ORDER BY
clause has this general form:ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...]
The
ORDER BY
clause causes the result rows to be sorted according to the specified expressions. If two rows are equal according to the leftmost expression, they are compared according to the next expression and so on. If they are equal according to all specified expressions, they are returned in an implementation-dependent order.Each expression can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.
The ordinal number refers to the ordinal (left-to-right) position of the output column. This feature makes it possible to define an ordering on the basis of a column that does not have a unique name. This is never absolutely necessary because it is always possible to assign a name to an output column using the AS clause.
It is also possible to use arbitrary expressions in the ORDER BY clause, including columns that do not appear in the SELECT output list. Therefore, the following statement is valid:
SELECT name FROM distributors ORDER BY code;
A limitation of this feature is that an
ORDER BY
clause applying to the result of a UNION, INTERSECT, or EXCEPT clause can only specify an output column name or number, not an expression.If an ORDER BY expression is a simple name that matches both an output column name and an input column name, ORDER BY will interpret it as the output column name. This is the opposite of the choice that GROUP BY will make in the same situation. This inconsistency is made to be compatible with the SQL standard.
Optionally, you can add the keyword ASC (ascending) or DESC (descending) after any expression in the ORDER BY clause. If not specified, ASC is assumed by default. Alternatively, a specific ordering operator name can be specified in the USING clause. An ordering operator must be a less-than or greater-than member of some B-tree operator family. ASC is usually equivalent to USING < and DESC is usually equivalent to USING >. However, the creator of a user-defined data type can define exactly what the default sort ordering is, and it might correspond to operators with other names.
If
NULLS LAST
is specified, null values sort after all non-null values. IfNULLS FIRST
is specified, null values sort before all non-null values. If neither is specified, the default behavior isNULLS LAST
when ASC is specified or implied, andNULLS FIRST
when DESC is specified (thus, the default is to act as though nulls are larger than non-nulls). When USING is specified, the default nulls ordering depends on whether the operator is a less-than or greater-than operator.Note that ordering options apply only to the expression they follow. For example,
ORDER BY x, y DESC
does not mean the same thing asORDER BY x DESC, y DESC
.Character-string data is sorted according to the collation that applies to the column being sorted. That can be overridden at need by including a COLLATE clause in the expression, for example,
ORDER BY mycolumn COLLATE "en_US"
. - LIMIT clause
The LIMIT clause consists of two independent sub-clauses:
LIMIT { count | ALL } OFFSET start
In the LIMIT clause, count specifies the maximum number of rows to return, whereas start specifies the number of rows to skip before rows are returned. When both are specified, start rows are skipped before starting to count the count rows to be returned.
If the count expression evaluates to NULL, it is treated as
LIMIT ALL
, which means no limit. If start evaluates to NULL, it is treated the same as OFFSET 0.When using LIMIT, it is a good idea to use an
ORDER BY
clause that constrains the result rows into a unique order. Otherwise, you will get an unpredictable subset of the query's rows. You might be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? You do not know what ordering unless you specifyORDER BY
.The query planner takes LIMIT into account when generating a query plan, so you are very likely to get different plans (yielding different row orders) depending on what you use for LIMIT and OFFSET. Thus, using different LIMIT/OFFSET values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with ORDER BY. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless
ORDER BY
is used to constrain the order.It is even possible for repeated executions of the same LIMIT query to return different subsets of the rows of a table, if there is not an
ORDER BY
to enforce selection of a deterministic subset. Again, this is not a bug; determinism of the results is simply not guaranteed in such a case.
Examples
- This example shows how to join the table films with the table distributors:
SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM distributors d, films f WHERE f.did = d.did
- This example shows how to sum the column length of all films and group the results
by kind:
SELECT kind, sum(length) AS total FROM films GROUP BY kind;
- This example shows how to sum the column length of all films, group the results by
kind, and show those group totals that are less than 5 hours:
SELECT kind, sum(length) AS total FROM films GROUP BY kind HAVING sum(length) < interval '5 hours';
- This example shows how to sort the returned results by name:
SELECT * FROM distributors ORDER BY name;