Commands
Commons (Data Manipulation)
SELECT
Selects data from a table or multiple tables.
Command is executed in the following logical order:
Data is taken from a table value expressions that are specified in the
FROM
clause, joins are executed. IfFROM
clause is not specified a single row is constructed.WHERE
filters rows. Aggregate or window functions are not allowed in this clause.GROUP BY
groups the result by the given expression(s). ifGROUP BY
clause is not specified, but non-window aggregate function are used orHAVING
is specified all rows are grouped together.Aggregate functions are evaluated.
Having filters rows after grouping and evaluation of aggregate functions. Non-window aggregate functions are allowed in this clause.
Window functions are evaluated.
QUALIFY
filters rows after evaluation of window functions. Aggregate and window functions are allowed in this clause. (Non-standard syntax)DISTINCT
removes duplicates. IfDISTINCT
is used only the specified expressions are checked for duplicated;ORDER BY
caluse, if any, is used to datetime preserved rows. First row is eachDISTINCT ON
group is preserved. In absence ofORDER BY
preserved rows are not datermined, database may choose any row from eachDISTINCT ON
group.UNION
,EXCEPT
andINTERSECT
combine the result of this query with the results of another query.INTERSECT
has higher precedence thanUNION
andEXCEPT
. Operators with equal precedence are evaluated from left to right.ORDER BY
sorts the result by the given column(s) or expression(s).Number of rows in output can be limited with
OFFSET
andFETCH
clauses.OFFSET
specifies how many rows to skip. Please note that queries with high offset values can be slow.FETCH FIRST/NEXT
limits the number of rows returned by the query. IfPERCENT
is specified number of rows is specified as a percent of the total number of rows and should be an integer value between 0 and 100 inclusive.WITH TIES
can be used only together withORDER BY
and means that all additional rows that have the same sorting position as the last row will be also returned.
Example:
Last updated