SELECT(7)
π SELECT, TABLE, WITH β PostgreSQL 14.24 Documentation π
okay so bestie, this is SELECT(7), and it is literally the main character of SQL. We're talking SELECT, TABLE, and WITH β the whole βretrieve rows from a table or viewβ situation. Buckle up, because this page is long and we are not skipping a single detail. π
β¨ NAME β¨
SELECT, TABLE, WITH β retrieve rows from a table or view. Slay. π
π¦ SYNOPSIS π¦
Here's the whole shape of it, bestie β every bracket exactly where it belongs:
[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
[ { * | expression [ [ AS ] output_name ] } [, ...] ]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
[ HAVING condition ]
[ WINDOW window_name AS ( window_definition ) [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start [ ROW | ROWS ] ]
[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]
[ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ]
where from_item can be one of:
[ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ]
[ LATERAL ] ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ LATERAL ] function_name ( [ argument [, ...] ] )
[ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
[ LATERAL ] function_name ( [ argument [, ...] ] ) [ AS ] alias ( column_definition [, ...] )
[ LATERAL ] function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
[ LATERAL ] ROWS FROM( function_name ( [ argument [, ...] ] ) [ AS ( column_definition [, ...] ) ] [, ...] )
[ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
from_item join_type from_item { ON join_condition | USING ( join_column [, ...] ) [ AS join_using_alias ] }
from_item NATURAL join_type from_item
from_item CROSS JOIN from_item
and grouping_element can be one of:
( )
expression
( expression [, ...] )
ROLLUP ( { expression | ( expression [, ...] ) } [, ...] )
CUBE ( { expression | ( expression [, ...] ) } [, ...] )
GROUPING SETS ( grouping_element [, ...] )
and with_query is:
with_query_name [ ( column_name [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( select | values | insert | update | delete )
[ SEARCH { BREADTH | DEPTH } FIRST BY column_name [, ...] SET search_seq_col_name ]
[ CYCLE column_name [, ...] SET cycle_mark_col_name [ TO cycle_mark_value DEFAULT cycle_mark_default ] USING cycle_path_col_name ]
TABLE [ ONLY ] table_name [ * ]
π DESCRIPTION π
SELECT retrieves rows from zero or more tables. The general processing of SELECT is like a little glow-up routine, and it goes a lil something like this:
- All queries in the
WITHlist are computed. These effectively serve as temporary tables that can be referenced in theFROMlist. AWITHquery that is referenced more than once inFROMis computed only once, unless specified otherwise withNOT MATERIALIZED. (See WITH Clause below.) π - All elements in the
FROMlist are computed. (Each element in theFROMlist is a real or virtual table.) If more than one element is specified in theFROMlist, they are cross-joined together. (See FROM Clause below.) - If the
WHEREclause is specified, all rows that do not satisfy the condition are eliminated from the output. (See WHERE Clause below.) Bye-bye, irrelevant rows. π - If the
GROUP BYclause is specified, or if there are aggregate function calls, the output is combined into groups of rows that match on one or more values, and the results of aggregate functions are computed. If theHAVINGclause is present, it eliminates groups that do not satisfy the given condition. (See GROUP BY Clause and HAVING Clause below.) Although query output columns are nominally computed in the next step, they can also be referenced (by name or ordinal number) in theGROUP BYclause. - The actual output rows are computed using the
SELECToutput expressions for each selected row or row group. (See SELECT List below.) SELECT DISTINCTeliminates duplicate rows from the result.SELECT DISTINCT ONeliminates rows that match on all the specified expressions.SELECT ALL(the default) will return all candidate rows, including duplicates. (See DISTINCT Clause below.)- Using the operators
UNION,INTERSECT, andEXCEPT, the output of more than oneSELECTstatement can be combined to form a single result set. TheUNIONoperator returns all rows that are in one or both of the result sets. TheINTERSECToperator returns all rows that are strictly in both result sets. TheEXCEPToperator returns the rows that are in the first result set but not in the second. In all three cases, duplicate rows are eliminated unlessALLis specified. The noise wordDISTINCTcan be added to explicitly specify eliminating duplicate rows. Notice thatDISTINCTis the default behavior here, even thoughALLis the default forSELECTitself. (See UNION Clause, INTERSECT Clause, and EXCEPT Clause below.) - If the
ORDER BYclause is specified, the returned rows are sorted in the specified order. IfORDER BYis not given, the rows are returned in whatever order the system finds fastest to produce. (See ORDER BY Clause below.) - If the
LIMIT(orFETCH FIRST) orOFFSETclause is specified, theSELECTstatement only returns a subset of the result rows. (See LIMIT Clause below.) - If
FOR UPDATE,FOR NO KEY UPDATE,FOR SHAREorFOR KEY SHAREis specified, theSELECTstatement locks the selected rows against concurrent updates. (See The Locking Clause below.)
You must have SELECT privilege on each column used in a SELECT command. The use of FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE or FOR KEY SHARE requires UPDATE privilege as well (for at least one column of each table so selected). No sneaky reads without the right permissions, bestie. π
πΈ PARAMETERS πΈ
π WITH Clause π
The WITH clause allows you to specify one or more subqueries that can be referenced by name in the primary query. The subqueries effectively act as temporary tables or views for the duration of the primary query. Each subquery can be a SELECT, TABLE, VALUES, INSERT, UPDATE or DELETE statement. When writing a data-modifying statement (INSERT, UPDATE or DELETE) in WITH, it is usual to include a RETURNING clause. It is the output of RETURNING, not the underlying table that the statement modifies, that forms the temporary table that is read by the primary query. If RETURNING is omitted, the statement is still executed, but it produces no output so it cannot be referenced as a table by the primary query. Lowkey important, highkey iconic. π
A name (without schema qualification) must be specified for each WITH query. Optionally, a list of column names can be specified; if this is omitted, the column names are inferred from the subquery.
If RECURSIVE is specified, it allows a SELECT subquery to reference itself by name. Such a subquery must have the form
non_recursive_term UNION [ ALL | DISTINCT ] recursive_term
where the recursive self-reference must appear on the right-hand side of the UNION. Only one recursive self-reference is permitted per query. Recursive data-modifying statements are not supported, but you can use the results of a recursive SELECT query in a data-modifying statement. See Section 7.8 for an example. π
Another effect of RECURSIVE is that WITH queries need not be ordered: a query can reference another one that is later in the list. (However, circular references, or mutual recursion, are not implemented.) Without RECURSIVE, WITH queries can only reference sibling WITH queries that are earlier in the WITH list.
When there are multiple queries in the WITH clause, RECURSIVE should be written only once, immediately after WITH. It applies to all queries in the WITH clause, though it has no effect on queries that do not use recursion or forward references.
The optional SEARCH clause computes a search sequence column that can be used for ordering the results of a recursive query in either breadth-first or depth-first order. The supplied column name list specifies the row key that is to be used for keeping track of visited rows. A column named search_seq_col_name will be added to the result column list of the WITH query. This column can be ordered by in the outer query to achieve the respective ordering. See Section 7.8.2.1 for examples. π
The optional CYCLE clause is used to detect cycles in recursive queries. The supplied column name list specifies the row key that is to be used for keeping track of visited rows. A column named cycle_mark_col_name will be added to the result column list of the WITH query. This column will be set to cycle_mark_value when a cycle has been detected, else to cycle_mark_default. Furthermore, processing of the recursive union will stop when a cycle has been detected. cycle_mark_value and cycle_mark_default must be constants and they must be coercible to a common data type, and the data type must have an inequality operator. (The SQL standard requires that they be Boolean constants or character strings, but PostgreSQL does not require that.) By default, TRUE and FALSE (of type boolean) are used. Furthermore, a column named cycle_path_col_name will be added to the result column list of the WITH query. This column is used internally for tracking visited rows. See Section 7.8.2.2 for examples. π
Both the SEARCH and the CYCLE clause are only valid for recursive WITH queries. The with_query must be a UNION (or UNION ALL) of two SELECT (or equivalent) commands (no nested UNIONs). If both clauses are used, the column added by the SEARCH clause appears before the columns added by the CYCLE clause.
The primary query and the WITH queries are all (notionally) executed at the same time. This implies that the effects of a data-modifying statement in WITH cannot be seen from other parts of the query, other than by reading its RETURNING output. If two such data-modifying statements attempt to modify the same row, the results are unspecified. β οΈ
A key property of WITH queries is that they are normally evaluated only once per execution of the primary query, even if the primary query refers to them more than once. In particular, data-modifying statements are guaranteed to be executed once and only once, regardless of whether the primary query reads all or any of their output.
...but bestie, you can slap NOT MATERIALIZED on a WITH query to β¨remove that guaranteeβ¨. And then what happens? The WITH query can get folded into the primary query, kinda like it was just a lil baby sub-SELECT chilling in the primary query's FROM clause π«Ά. Downside? If the primary query references that WITH query more than once, you get duplicate computations π. But β and this is the slay part β if each of those uses only needs a few rows out of the WITH query's whole output, NOT MATERIALIZED can give you a net savings by letting the queries be optimized jointly. π
Heads up though:NOT MATERIALIZEDgets ignored if you attach it to a WITH query that's recursive, or one that isn't side-effect-free (meaning it's not a plainSELECTwith no volatile functions in it). π
By default, a side-effect-free WITH query gets folded into the primary query if it's used exactly once in the primary query's FROM clause. That lets the two query levels get optimized together in situations where doing so should be semantically invisible π». But you can block that folding by marking the WITH query as MATERIALIZED. When's that useful? Say, when the WITH query is acting as an optimization fence so the planner doesn't go pick a trash plan π§. Also β and this is a lil history moment β PostgreSQL versions before v12 never did this folding, so queries written for older versions might be lowkey relying on WITH to act as an optimization fence. iykyk.
See Section 7.8 for additional info, bestie π.
π FROM Clause π
The FROM clause is where you tell the SELECT which source table(s) you're pulling from. Specify more than one source? Congrats, you just got the Cartesian product (cross join) of all of them π₯. But fr fr, usually you add qualification conditions (via WHERE) so the returned rows are just a small subset of that Cartesian product instead of an absolute chaos explosion π.
The FROM clause can contain these elements:
π table_name
The name (optionally schema-qualified) of an existing table or view. If ONLY comes before the table name, only that table gets scanned. If ONLY is absent, the table and all its descendant tables (if any) get scanned. Optionally you can put * after the table name to explicitly say "yep, include the descendant tables" β¨.
π alias
A substitute name for the FROM item that contains the alias. Aliases are for brevity, or for killing ambiguity in self-joins (where you scan the same table multiple times). Important: when you provide an alias, it completely hides the actual name of the table or function π. Like, given FROM foo AS f, the rest of the SELECT has to refer to this FROM item as f, not foo. And if you write an alias, you can also write a column alias list to give substitute names to one or more columns of the table π
.
π TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ]
A TABLESAMPLE clause after a table_name says "use this sampling_method to grab a subset of the rows in that table". This sampling happens before any other filters (like WHERE clauses) get applied β‘. The standard PostgreSQL distribution ships with two sampling methods, BERNOULLI and SYSTEM, and you can install other sampling methods into the database via extensions π§.
The BERNOULLI and SYSTEM sampling methods each accept a single argument: the fraction of the table to sample, expressed as a percentage between 0 and 100. That argument can be any real-valued expression. (Other sampling methods might accept more or different arguments, no cap.) Both of these methods return a randomly-chosen sample of the table containing approximately the specified percentage of the table's rows π². Here's the difference:
- The
BERNOULLImethod scans the whole table and selects or ignores individual rows independently, with the specified probability. - The
SYSTEMmethod does block-level sampling, where each block has the specified chance of being selected, and all rows in each selected block get returned.
The SYSTEM method is significantly faster than BERNOULLI when you're specifying small sampling percentages, but because of clustering effects it may return a less-random sample of the table π¬.
The optional REPEATABLE clause specifies a seed number or expression for generating random numbers inside the sampling method. The seed value can be any non-null floating-point value. Two queries with the same seed and argument values will select the same sample of the table, assuming the table hasn't been changed in the meantime π«Ά. Different seed values will usually produce different samples. If REPEATABLE isn't given, a new random sample is selected for each query, based on a system-generated seed. Note that some add-on sampling methods don't accept REPEATABLE at all and will always produce new samples on each use π.
π select
A sub-SELECT can appear in the FROM clause, and it acts like its output was created as a temporary table for the duration of this single SELECT command β¨. Note: the sub-SELECT must be surrounded by parentheses, and you must provide an alias for it. A VALUES command can also be used here π.
π with_query_name
You reference a WITH query by writing its name, just like the query's name were a table name. (Fun fact: the WITH query actually hides any real table with the same name for the purposes of the primary query. If you need to, you can refer to a real table of the same name by schema-qualifying the table's name π .) An alias can be provided the same way as for a table.
π function_name
Function calls can appear in the FROM clause! (Especially useful for functions that return result sets, but literally any function can be used.) This acts like the function's output was created as a temporary table for the duration of this single SELECT command. If the function's result type is composite (including the case of a function with multiple OUT parameters), each attribute becomes a separate column in the implicit table πΈ.
When you add the optional WITH ORDINALITY clause to the function call, an additional column of type bigint gets appended to the function's result column(s). This column numbers the rows of the function's result set, starting from 1 π’. By default, this column is named ordinality.
An alias can be provided 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 ordinality column, if it's there π.
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, etc. If some functions produce fewer rows than others, null values are substituted for the missing data, so the total number of rows returned always matches the function that produced the most rows π.
If the function is defined as returning the record data type, then an alias or the key word AS must be present, followed by a column definition list in the form ( column_name data_type [, ... ]). That column definition list must match the actual number and types of columns returned by the function β οΈ.
When you're using the ROWS FROM( ... ) syntax, if one of the functions requires a column definition list, the preferred move is to put the column definition list after the function call inside ROWS FROM( ... ). You can only place a column definition list after the ROWS FROM( ... ) construct if there's just a single function and no WITH ORDINALITY clause π.
To use ORDINALITY together with a column definition list, you must use the ROWS FROM( ... ) syntax and put the column definition list inside ROWS FROM( ... ). No shortcuts, bestie π
.
π join_type
One of:
[ INNER ] JOINLEFT [ OUTER ] JOINRIGHT [ OUTER ] JOINFULL [ OUTER ] JOIN
For the INNER and OUTER join types, a join condition must be specified β namely exactly one of ON join_condition, USING (join_column [, ...]), or NATURAL. See below for what those mean π.
A JOIN clause combines two FROM items, which for convenience we'll call tables, even though in reality they can be any type of FROM item β¨. Use parentheses if you need to determine the order of nesting. With no parentheses, JOINs nest left-to-right. Either way, JOIN binds more tightly than the commas separating FROM-list items π₯. All the JOIN options are honestly just notational convenience, since they don't do anything you couldn't do with plain FROM and WHERE.
LEFT OUTER JOIN returns all rows in the qualified Cartesian product (i.e., 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. That left-hand row gets extended to the full width of the joined table by inserting null values for the right-hand columns π«Ά. Note: only the JOIN clause's own condition is considered when 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 one's just notational convenience too, since you could convert it to a LEFT 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). It's giving "everybody gets a row" energy π.
π ON join_condition
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 β
.
π USING ( join_column [, ...] ) [ AS join_using_alias ]
A clause of the form USING ( a, b, ... ) is shorthand for ON 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 gets included in the join output, not both.
If a join_using_alias name is specified, it provides a table alias for the join columns. Only the join columns listed in the USING clause are addressable by this name π. Unlike a regular alias, this does not hide the names of the joined tables from the rest of the query. And also unlike a regular alias, you cannot write a column alias list β the output names of the join columns are the same as they appear in the USING list π.
NATURAL
okay so NATURAL is literally shorthand for a USING list that name-drops all the columns in the two tables that have matching names π
. And if there are no common column names at all, NATURAL is equivalent to ON TRUE β it just says yes to everyone, iconic π«Ά.
CROSS JOIN
CROSS JOIN is equivalent to INNER JOIN ON (TRUE), which means no rows are removed by qualification, periodt. They produce a simple Cartesian product β the exact same result you get from listing the two tables at the top level of FROM, but restricted by the join condition (if any) β¨.
LATERAL
The LATERAL key word can precede a sub-SELECT FROM item. This is the whole glow-up: it lets the sub-SELECT refer to columns of FROM items that appear before it in the FROM list π. (Without LATERAL, each sub-SELECT is evaluated independently and so cannot cross-reference any other FROM item β no sneaky peeking π.)
LATERAL can also precede a function-call FROM item, but in this case it is a noise word, because the function expression can refer to earlier FROM items in any case π.
A LATERAL item can appear at top level in the FROM list, or within a JOIN tree. In the latter case it can also refer to any items that are on the left-hand side of a JOIN that it is on the right-hand side of.
When a FROM item contains LATERAL cross-references, evaluation proceeds as follows: for each row of the FROM item providing the cross-referenced column(s), or set of rows of multiple FROM items providing the columns, the LATERAL item is evaluated using that row or row set's values of the columns π. The resulting row(s) are joined as usual with the rows they were computed from. This is repeated for each row or set of rows from the column source table(s).
The column source table(s) must be INNER or LEFT joined to the LATERAL item, else there would not be a well-defined set of rows from which to compute each set of rows for the LATERAL item β οΈ. Thus, although a construct such as X RIGHT JOIN LATERAL Y is syntactically valid, it is not actually allowed for Y to reference X.
bestie listen. it parses. that's it. it is not allowed. gorgeous syntax, zero permission π
πΈ WHERE Clause πΈ
The optional WHERE clause has the general form
WHERE condition
where condition is any expression that evaluates to a result of type boolean. Any row that does not satisfy this condition gets eliminated from the output β bye π. 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 the general form
GROUP BY [ ALL | DISTINCT ] 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 a grouping_element can be an input column name, or the name or ordinal number of an output column (a SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a GROUP 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, then the GROUP 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 their GROUP BY clauses β¨. The optional DISTINCT clause removes duplicate sets before processing; it does not transform the UNION ALL into a UNION DISTINCT β do not let it lie to you π. For further details on the handling of grouping sets see Section 7.2.4.
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 β one big group chat π¬.) The set of rows fed to each aggregate function can be further filtered by attaching a FILTER clause to the aggregate function call; see Section 4.2.7 for more information. 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 β because otherwise there'd be more than one possible value to return for an ungrouped column, and the universe cannot pick favourites π. 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; see Section 4.2.14 π.
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified with GROUP BY.
π¦ HAVING Clause π¦
The optional HAVING clause has the general form
HAVING condition
where condition is the same as specified for the WHERE clause.
HAVING eliminates group rows that do not satisfy the condition. HAVING is different from WHERE: WHERE filters individual rows before the application of GROUP BY, while HAVING filters group rows created by GROUP BY π
. Each column referenced in condition must unambiguously reference a grouping column, unless the reference appears within an aggregate function or the ungrouped column is functionally dependent on the grouping columns.
The presence of HAVING turns a query into a grouped query even if there is no GROUP BY clause. This is the same as what happens when the query contains aggregate functions but no GROUP BY clause. All the selected rows are considered to form a single group, and the SELECT list and HAVING clause can only reference table columns from within aggregate functions. Such a query will emit a single row if the HAVING condition is true, zero rows if it is not true β all or nothing, main character energy β.
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified with HAVING.
π WINDOW Clause π
The optional WINDOW clause has the general form
WINDOW window_name AS ( window_definition ) [, ...]
where window_name is a name that can be referenced from OVER clauses or subsequent window definitions, and window_definition is
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
If an existing_window_name is specified it must refer to an earlier entry in the WINDOW list; the new window copies its partitioning clause from that entry, as well as its ordering clause if any π«Ά. In this case the new window cannot specify its own PARTITION BY clause, and it can specify ORDER BY only if the copied window does not have one. The new window always uses its own frame clause; the copied window must not specify a frame clause β no double-dipping π
.
The elements of the PARTITION BY list are interpreted in much the same fashion as elements of a GROUP BY clause, except that they are always simple expressions and never the name or number of an output column π. Another difference is that these expressions can contain aggregate function calls, which are not allowed in a regular GROUP BY clause. They are allowed here because windowing occurs after grouping and aggregation β order of operations, bestie β¨.
Similarly, the elements of the ORDER BY list are interpreted in much the same fashion as elements of a statement-level ORDER BY clause, except that the expressions are always taken as simple expressions and never the name or number of an output column.
The optional frame_clause defines the window frame for window functions that depend on the frame (not all do). The window frame is a set of related rows for each row of the query (called the current row). The frame_clause can be one of
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
where frame_start and frame_end can be one of
UNBOUNDED PRECEDING
offset PRECEDING
CURRENT ROW
offset FOLLOWING
UNBOUNDED FOLLOWING
and frame_exclusion can be one of
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
If frame_end is omitted it defaults to CURRENT ROW. Restrictions are that frame_start cannot be UNBOUNDED FOLLOWING, frame_end cannot be UNBOUNDED PRECEDING, and the frame_end choice cannot appear earlier in the above list of frame_start and frame_end options than the frame_start choice does β for example RANGE BETWEEN CURRENT ROW AND offset PRECEDING is not allowed π«. (the timeline cannot go backwards, babe.)
The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW; it sets the frame to be all rows from the partition start up through the current row's last peer (a row that the window's ORDER BY clause considers equivalent to the current row; all rows are peers if there is no ORDER BY) π. In general, UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly UNBOUNDED FOLLOWING means that the frame ends with the last row of the partition, regardless of RANGE, ROWS or GROUPS mode. In ROWS mode, CURRENT ROW means that the frame starts or ends with the current row; but in RANGE or GROUPS mode it means that the frame starts or ends with the current row's first or last peer in the ORDER BY ordering π. The offset PRECEDING and offset FOLLOWING options vary in meaning depending on the frame mode. In ROWS mode, the offset is an integer indicating that the frame starts or ends that many rows before or after the current row. In GROUPS mode, the offset is an integer indicating that the frame starts or ends that many peer groups before or after the current row's peer group, where a peer group is a group of rows that are equivalent according to the window's ORDER BY clause π. In RANGE mode, use of an offset option requires that there be exactly one ORDER BY column in the window definition. Then the frame contains those rows whose ordering column value is no more than offset less than (for PRECEDING) or more than (for FOLLOWING) the current row's ordering column value. In these cases the data type of the offset expression depends on the data type of the ordering column. For numeric ordering columns it is typically of the same type as the ordering column, but for datetime ordering columns it is an interval β°. In all these cases, the value of the offset must be non-null and non-negative. Also, while the offset does not have to be a simple constant, it cannot contain variables, aggregate functions, or window functions π
ββοΈ.
okay so the frame_exclusion option is the one that lets rows around the current row get booted out of the frame, even when theyβd normally be included by the frame start and frame end options. EXCLUDE CURRENT ROW kicks the current row out of the frame. EXCLUDE GROUP kicks out the current row plus its ordering peers. EXCLUDE TIES kicks out any peers of the current row, but not the current row itself. EXCLUDE NO OTHERS just spells out, explicitly, the default behavior of not excluding the current row or its peers. π
β οΈ bestie, beware: theROWSmode can produce unpredictable results if theORDER BYordering does not order the rows uniquely. TheRANGEandGROUPSmodes are designed to make sure that rows which are peers in theORDER BYordering get treated alike: all rows of a given peer group will be in the frame or excluded from it. π₯
The purpose of a WINDOW clause is to specify the behavior of window functions appearing in the queryβs SELECT list or ORDER BY clause. These functions can reference the WINDOW clause entries by name in their OVER clauses. A WINDOW clause entry does not have to be referenced anywhere, though; if it is not used in the query it is simply ignored. It is possible to use window functions without any WINDOW clause at all, since a window function call can specify its window definition directly in its OVER clause. However, the WINDOW clause saves typing when the same window definition is needed for more than one window function. π«Ά
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified with WINDOW.
Window functions are described in detail in Section 3.5, Section 4.2.8, and Section 7.2.5.
πΈ 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.
Just as in a table, every output column of a SELECT has a name. In a simple SELECT this name is just used to label the column for display, but when the SELECT is a sub-query of a larger query, the name is seen by the larger query as the column name of the virtual table produced by the sub-query. 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 (see Appendix C). For protection against possible future keyword additions, it is recommended 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 then 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 as ?column?. β¨
An output columnβs name can 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.
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. In these cases it is not possible to specify new names with AS; the output column names will be the same as the table columnsβ names.
According to the SQL standard, the expressions in the output list should be computed before applying DISTINCT, ORDER BY, or LIMIT. This is obviously necessary when using DISTINCT, since otherwise itβs not clear what values are being made distinct. However, in many cases it is convenient if output expressions are computed after ORDER BY and LIMIT; particularly if the output list contains any volatile or expensive functions. With that behavior, the order of function evaluations is more intuitive and there will not be evaluations corresponding to rows that never appear in the output. PostgreSQL will effectively evaluate output expressions after sorting and limiting, so long as those expressions are not referenced in DISTINCT, ORDER BY or GROUP BY. (As a counterexample, SELECT f(x) FROM tab ORDER BY 1 clearly must evaluate f(x) before sorting.) Output expressions that contain set-returning functions are effectively evaluated after sorting and before limiting, so that LIMIT will act to cut off the output from a set-returning function. π
π Note PostgreSQL versions before 9.6 did not provide any guarantees about the timing of evaluation of output expressions versus sorting and limiting; it depended on the form of the chosen query plan.
π« 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. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). Note that the βfirst rowβ of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first. For example:
SELECT DISTINCT ON (location) location, time, report
FROM weather_reports
ORDER BY location, time DESC;
retrieves the most recent weather report for each location. But if we had not used ORDER BY to force descending order of time values for each location, weβd have gotten a report from an unpredictable time for each location. π
The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). The ORDER BY clause will normally contain additional expression(s) that determine the desired precedence of rows within each DISTINCT ON group.
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified with DISTINCT.
π¦ UNION Clause π¦
The UNION clause has this general form:
select_statement UNION [ ALL | DISTINCT ] select_statement
select_statement is any SELECT statement without an ORDER BY, LIMIT, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE, or FOR 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 when you can.) 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.
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified either for a UNION result or for any input of a UNION.
β¨ INTERSECT Clause β¨
The INTERSECT clause has this general form:
select_statement INTERSECT [ ALL | DISTINCT ] select_statement
select_statement is any SELECT statement without an ORDER BY, LIMIT, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE, or FOR KEY SHARE 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 as A UNION (B INTERSECT C).
Currently, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE and FOR KEY SHARE cannot be specified either for an INTERSECT result or for any input of an INTERSECT.
π EXCEPT Clause π
The EXCEPT clause has this general form:
select_statement EXCEPT [ ALL | DISTINCT ] select_statement
select_statement is any SELECT statement without an ORDER BY, LIMIT, FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE, or FOR KEY SHARE 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 and FOR 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 expression(s). 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. Thus the following statement is valid:
So like, <pink>ORDER BY</pink> on top of a UNION? This:
SELECT name FROM distributors ORDER BY code;
Cute, that works π β but there's a catch, bestie. When the ORDER BY is attached to the result of a UNION, INTERSECT, or EXCEPT, it can only name an output column β by name or by number β and not an expression. That's the limitation, periodt.
And here's a spicy one πΆοΈ: if your ORDER BY expression is a plain old name that happens to match both an output column name and an input column name, ORDER BY will read it as the output column name. Which is literally the opposite of the call GROUP BY makes in the exact same situation π. Yeah, it's inconsistent β and it's inconsistent on purpose, to stay compatible with the SQL standard. iykyk.
If you want, you can drop the keyword ASC (ascending) or DESC (descending) after any expression in the ORDER BY clause. Don't specify either? Then ASC is what you get by default β¨. OR β plot twist β you can name a specific ordering operator in the USING clause. That operator has to 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 >. (But the creator of a user-defined data type gets to define exactly what the default sort ordering is, so it might line up with operators that have totally different names. Their type, their rules π
.)
Now, nulls π:
NULLS LASTβ null values sort after all non-null values.NULLS FIRSTβ null values sort before all non-null values.- Neither one specified? Then the default is
NULLS LASTwhenASCis specified or implied, andNULLS FIRSTwhenDESCis specified β so basically the default behaves like nulls are larger than non-nulls. - When
USINGis specified, the default nulls ordering depends on whether that operator is a less-than or a greater-than operator.
Lowkey important detail: ordering options only apply to the expression they follow. So ORDER BY x, y DESC does not mean the same thing as ORDER BY x DESC, y DESC. Not even close, bestie π«Ά.
Character-string data gets sorted according to the collation that applies to the column being sorted. Need to override it? Just throw a COLLATE clause into the expression, like:
ORDER BY mycolumn COLLATE "en_US"
For more info, see Section 4.2.10 and Section 24.2. π¦
πΈ LIMIT Clause πΈ
The LIMIT clause is made of two independent sub-clauses, bestie:
LIMIT { count | ALL }
OFFSET start
The parameter count is the maximum number of rows to return, while start is how many rows to skip before you even start returning rows. When both are specified, start rows get skipped first, and then the count rows to be returned start getting counted. Order of operations, slay π
.
If the count expression evaluates to NULL, it's treated as LIMIT ALL β i.e., no limit at all, go wild π. If start evaluates to NULL, it's treated the same as OFFSET 0.
SQL:2008 brought in a whole different syntax to get the same result, and PostgreSQL supports that too. It's giving:
OFFSET start { ROW | ROWS }
FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES }
In this syntax, the standard requires the start or count value to be a literal constant, a parameter, or a variable name. But as a PostgreSQL extension, other expressions are allowed β they'll generally just need to be wrapped in parentheses so there's no ambiguity, k? If count is omitted in a FETCH clause, it defaults to 1. The WITH TIES option grabs any extra rows that tie for the last place in the result set according to the ORDER BY clause β and in that case ORDER BY is mandatory, and SKIP LOCKED is not allowed π«. ROW and ROWS, plus FIRST and NEXT, are just noise words; they don't change anything about what these clauses do. Per the standard, OFFSET has to come before FETCH when both are present β but PostgreSQL is laxer and lets you put them in either order π.
When you're usingLIMIT, it's a good idea to include anORDER BYclause that pins the result rows into a unique order. Otherwise you're getting an unpredictable subset of the query's rows β like, you might be asking for rows ten through twenty, but ten through twenty in what ordering, bestie?? You don't know the ordering unless you specifyORDER BY. π
The query planner takes LIMIT into account when it generates a query plan, so you're very likely to get different plans (yielding different row orders) depending on what you use for LIMIT and OFFSET. Which means: using different LIMIT/OFFSET values to select different subsets of a query result will give you inconsistent results unless you enforce a predictable result ordering with ORDER BY. And no, that's not a bug β it's an inherent consequence of the fact that SQL simply does not promise to deliver the results of a query in any particular order unless ORDER BY is used to constrain the order. Facts are facts π₯.
It's even possible for repeated executions of the same LIMIT query to return different subsets of a table's rows, if there's no ORDER BY enforcing selection of a deterministic subset. Again β not a bug. Determinism of the results is just straight up not guaranteed in that case. It's giving "chaotic neutral" π.
π The Locking Clause π
FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, and FOR KEY SHARE are locking clauses β they affect how SELECT locks rows as they're obtained from the table. Think of them as the "mine, don't touch" of SQL π.
The locking clause has the general form:
FOR lock_strength [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ]
where lock_strength can be one of:
UPDATE
NO KEY UPDATE
SHARE
KEY SHARE
For more info on each row-level lock mode, refer to Section 13.3.2. π
To stop the operation from waiting for other transactions to commit, use either the NOWAIT or SKIP LOCKED option. With NOWAIT, the statement reports an error instead of waiting if a selected row can't be locked immediately. With SKIP LOCKED, any selected rows that can't be immediately locked are just skipped. β οΈ Skipping locked rows gives you an inconsistent view of the data, so it's not suitable for general-purpose work β but it can be used to avoid lock contention when multiple consumers are hitting a queue-like table. Note that NOWAIT and SKIP LOCKED apply only to the row-level lock(s); the required ROW SHARE table-level lock still gets taken the ordinary way (see Chapter 13). And if you need to acquire that table-level lock without waiting, you can use LOCK with the NOWAIT option first. π«Ά
If specific tables are named in a locking clause, then only rows coming from those tables get locked; any other tables used in the SELECT are simply read as usual. A locking clause without a table list affects all tables used in the statement. If a locking clause is applied to a view or sub-query, it affects all tables used in that view or sub-query. However β these clauses do not apply to WITH queries referenced by the primary query. If you want row locking to happen inside a WITH query, you gotta specify a locking clause within the WITH query itself. π‘
You can write multiple locking clauses if you need different locking behavior for different tables. If the same table is mentioned (or implicitly affected) by more than one locking clause, it's processed as if it was only specified by the strongest one. Same energy: a table is processed as NOWAIT if that's specified in any of the clauses affecting it; otherwise, it's processed as SKIP LOCKED if that's specified in any of the clauses affecting it. π
Locking clauses cannot be used in contexts where the returned rows can't be clearly identified with individual table rows β for example, they can't be used with aggregation. Nope, not happening π ββοΈ.
When a locking clause shows up at the top level of a SELECT query, the rows that get locked are exactly those returned by the query; in the case of a join query, the rows locked are those that contribute to returned join rows. On top of that, rows that satisfied the query conditions as of the query snapshot will be locked β even though they won't be returned if they were updated after the snapshot and no longer satisfy the query conditions. If a LIMIT is used, locking stops once enough rows have been returned to satisfy the limit (but heads up: rows skipped over by OFFSET will get locked π). Similarly, if a locking clause is used in a cursor's query, only rows actually fetched or stepped past by the cursor will be locked.
When a locking clause appears in a sub-SELECT, the rows locked are the ones returned to the outer query by the sub-query. That might involve fewer rows than just staring at the sub-query alone would suggest, since conditions from the outer query might get used to optimize execution of the sub-query. For example:
SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss WHERE col1 = 5;
will lock only the rows having col1 = 5 β even though that condition isn't textually inside the sub-query. Sneaky, but that's the optimizer for you β¨.
Previous releases failed to preserve a lock that's upgraded by a later savepoint. For example, this code:
BEGIN;
SELECT * FROM mytable WHERE key = 1 FOR UPDATE;
SAVEPOINT s;
UPDATE mytable SET ... WHERE key = 1;
ROLLBACK TO s;
would fail to preserve the FOR UPDATE lock after the ROLLBACK TO. This has been fixed in release 9.3 π (yes, we're old, moving on).
π¨ Caution π¨
It is possible for aSELECTcommand running at theREAD COMMITTEDtransaction isolation level and usingORDER BYand a locking clause to return rows out of order. Why? BecauseORDER BYis applied first. The command sorts the result, but might then block trying to obtain a lock on one or more of the rows. Once theSELECTunblocks, some of the ordering column values might have been modified, leading to those rows appearing to be out of order (though they are in order in terms of the original column values). π
This can be worked around at need by placing theFOR UPDATE/SHAREclause in a sub-query, for example:SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss ORDER BY column1;Note that this will result in locking all rows ofmytable, whereasFOR UPDATEat the top level would lock only the actually returned rows. That can make for a significant performance difference, particularly if theORDER BYis combined withLIMITor other restrictions. So this technique is recommended only if concurrent updates of the ordering columns are expected and a strictly sorted result is required. π
At theREPEATABLE READorSERIALIZABLEtransaction isolation level, this would cause a serialization failure (with aSQLSTATEof'40001'), so there is no possibility of receiving rows out of order under these isolation levels. Slay π.
π TABLE Command π
The command:
TABLE name
is equivalent to:
SELECT * FROM name
It can be used as a top-level command, or as a space-saving syntax variant in parts of complex queries. π¦ Only the WITH, UNION, INTERSECT, EXCEPT, ORDER BY, LIMIT, OFFSET, FETCH, and FOR locking clauses can be used with TABLE; the WHERE clause and any form of aggregation cannot be used. That's the whole vibe check, no notes π
.
π¬ EXAMPLES π¬
Okay bestie, buckle up β these examples are lowkey iconic, and they show SELECT doing its main-character thing π
To join the table films with the table distributors, you do this:
SELECT f.title, f.did, d.name, f.date_prod, f.kind
FROM distributors d JOIN films f USING (did);
title | did | name | date_prod | kind
-------------------+-----+--------------+------------+----------
The Third Man | 101 | British Lion | 1949-12-23 | Drama
The African Queen | 101 | British Lion | 1951-08-11 | Romantic
...
To sum the column len of all films and group the results by kind, it's giving this:
SELECT kind, sum(len) AS total FROM films GROUP BY kind;
kind | total
----------+-------
Action | 07:34
Comedy | 02:58
Drama | 14:28
Musical | 06:42
Romantic | 04:38
To sum the column len of all films, group the results by kind, and show only those group totals that are less than 5 hours, you'd do:
SELECT kind, sum(len) AS total
FROM films
GROUP BY kind
HAVING sum(len) < interval '5 hours';
kind | total
----------+-------
Comedy | 02:58
Romantic | 04:38
The next two examples are literally identical ways of sorting the individual results according to the contents of the second column (name) β no cap:
SELECT * FROM distributors ORDER BY name;
SELECT * FROM distributors ORDER BY 2;
did | name
-----+------------------
109 | 20th Century Fox
110 | Bavaria Atelier
101 | British Lion
107 | Columbia
102 | Jean Luc Godard
113 | Luso films
104 | Mosfilm
103 | Paramount
106 | Toho
105 | United Artists
111 | Walt Disney
112 | Warner Bros.
108 | Westward
The next example shows how to obtain the union of the tables distributors and actors, restricting the results to those that begin with the letter W in each table. Only distinct rows are wanted, so the key word ALL is omitted β periodt π
distributors: actors:
did | name id | name
-----+-------------- ----+----------------
108 | Westward 1 | Woody Allen
111 | Walt Disney 2 | Warren Beatty
112 | Warner Bros. 3 | Walter Matthau
... ...
SELECT distributors.name
FROM distributors
WHERE distributors.name LIKE 'W%'
UNION
SELECT actors.name
FROM actors
WHERE actors.name LIKE 'W%';
name
----------------
Walt Disney
Walter Matthau
Warner Bros.
Warren Beatty
Westward
Woody Allen
This example shows how to use a function in the FROM clause, both with and without a column definition list β bestie, it's giving flexible:
CREATE FUNCTION distributors(int) RETURNS SETOF distributors AS $$
SELECT * FROM distributors WHERE did = $1;
$$ LANGUAGE SQL;
SELECT * FROM distributors(111);
did | name
-----+-------------
111 | Walt Disney
CREATE FUNCTION distributors_2(int) RETURNS SETOF record AS $$
SELECT * FROM distributors WHERE did = $1;
$$ LANGUAGE SQL;
SELECT * FROM distributors_2(111) AS (f1 int, f2 text);
f1 | f2
-----+-------------
111 | Walt Disney
Here's an example of a function with an ordinality column added β look at that little ordinality column doing its thing β¨
SELECT * FROM unnest(ARRAY['a','b','c','d','e','f']) WITH ORDINALITY;
unnest | ordinality
--------+----------
a | 1
b | 2
c | 3
d | 4
e | 5
f | 6
(6 rows)
This example shows how to use a simple WITH clause:
WITH t AS (
SELECT random() as x FROM generate_series(1, 3)
)
SELECT * FROM t
UNION ALL
SELECT * FROM t;
x
--------------------
0.534150459803641
0.520092216785997
0.0735620250925422
0.534150459803641
0.520092216785997
0.0735620250925422
Notice that the WITH query was evaluated only once, so that we got two sets of the same three random values. Lowkey important, bestie π
This example uses WITH RECURSIVE to find all subordinates (direct or indirect) of the employee Mary, and their level of indirectness, from a table that shows only direct subordinates:
WITH RECURSIVE employee_recursive(distance, employee_name, manager_name) AS (
SELECT 1, employee_name, manager_name
FROM employee
WHERE manager_name = 'Mary'
UNION ALL
SELECT er.distance + 1, e.employee_name, e.manager_name
FROM employee_recursive er, employee e
WHERE er.employee_name = e.manager_name
)
SELECT distance, employee_name FROM employee_recursive;
Notice the typical form of recursive queries: an initial condition, followed by UNION, followed by the recursive part of the query. Be sure that the recursive part of the query will eventually return no tuples, or else the query will loop indefinitely. (See Section 7.8 for more examples.)
π¨ Bestie, for real: if that recursive part never stops returning tuples, your query is gonna loop forever. Not cute. Not slay. Fix it.
This example uses LATERAL to apply a set-returning function get_product_names() for each row of the manufacturers table:
SELECT m.name AS mname, pname
FROM manufacturers m, LATERAL get_product_names(m.id) pname;
Manufacturers not currently having any products would not appear in the result, since it is an inner join. If we wished to include the names of such manufacturers in the result, we could do:
SELECT m.name AS mname, pname
FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true;
π COMPATIBILITY π
Of course, the SELECT statement is compatible with the SQL standard. But there are some extensions and some missing features β because PostgreSQL likes to do its own thing sometimes π¦
π«Ά Omitted FROM Clauses π«Ά
PostgreSQL allows one to omit the FROM clause. It has a straightforward use to compute the results of simple expressions:
SELECT 2+2;
?column?
----------
4
Some other SQL databases cannot do this except by introducing a dummy one-row table from which to do the SELECT. Weird, but okay π
π Empty SELECT Lists π
The list of output expressions after SELECT can be empty, producing a zero-column result table. This is not valid syntax according to the SQL standard. PostgreSQL allows it to be consistent with allowing zero-column tables. However, an empty list is not allowed when DISTINCT is used.
π¦ Omitting the AS Key Word π¦
In the SQL standard, the optional key word AS can be omitted before an output column name whenever the new column name is a valid column name (that is, not the same as any reserved keyword). PostgreSQL is slightly more restrictive: AS is required if the new column name matches any keyword at all, reserved or not. Recommended practice is to use AS or double-quote output column names, to prevent any possible conflict against future keyword additions.
In FROM items, both the standard and PostgreSQL allow AS to be omitted before an alias that is an unreserved keyword. But this is impractical for output column names, because of syntactic ambiguities.
πΈ ONLY and Inheritance πΈ
The SQL standard requires parentheses around the table name when writing ONLY, for example SELECT * FROM ONLY (tab1), ONLY (tab2) WHERE .... PostgreSQL considers these parentheses to be optional.
PostgreSQL allows a trailing * to be written to explicitly specify the non-ONLY behavior of including child tables. The standard does not allow this.
(These points apply equally to all SQL commands supporting the ONLY option.)
π TABLESAMPLE Clause Restrictions π
The TABLESAMPLE clause is currently accepted only on regular tables and materialized views. According to the SQL standard it should be possible to apply it to any FROM item.
π§ Function Calls in FROM π§
PostgreSQL allows a function call to be written directly as a member of the FROM list. In the SQL standard it would be necessary to wrap such a function call in a sub-SELECT; that is, the syntax FROM func(...) alias is approximately equivalent to FROM LATERAL (SELECT func(...)) alias. Note that LATERAL is considered to be implicit; this is because the standard requires LATERAL semantics for an UNNEST() item in FROM. PostgreSQL treats UNNEST() the same as other set-returning functions.
β Namespace Available to GROUP BY and ORDER BY β
In the SQL-92 standard, an ORDER BY clause can only use output column names or numbers, while a GROUP BY clause can only use expressions based on input column names. PostgreSQL extends each of these clauses to allow the other choice as well (but it uses the standard's interpretation if there is ambiguity). PostgreSQL also allows both clauses to specify arbitrary expressions. Note that names appearing in an expression will always be taken as input-column names, not as output-column names.
SQL:1999 and later use a slightly different definition which is not entirely upward compatible with SQL-92. In most cases, however, PostgreSQL will interpret an ORDER BY or GROUP BY expression the same way SQL:1999 does.
π Functional Dependencies π
PostgreSQL recognizes functional dependency (allowing columns to be omitted from GROUP BY) only when a table's primary key is included in the GROUP BY list. The SQL standard specifies additional conditions that should be recognized.
π₯ LIMIT and OFFSET π₯
The clauses LIMIT and OFFSET are PostgreSQL-specific syntax, also used by MySQL. The SQL:2008 standard has introduced the clauses OFFSET ... FETCH {FIRST|NEXT} ... for the same functionality, as shown above in LIMIT Clause. This syntax is also used by IBM DB2. (Applications written for Oracle frequently use a workaround involving the automatically generated rownum column, which is not available in PostgreSQL, to implement the effects of these clauses.)
π FOR NO KEY UPDATE, FOR UPDATE, FOR SHARE, FOR KEY SHARE π
Although FOR UPDATE appears in the SQL standard, the standard allows it only as an option of DECLARE CURSOR. PostgreSQL allows it in any SELECT query as well as in sub-SELECTs, but this is an extension. The FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHARE variants, as well as the NOWAIT and SKIP LOCKED options, do not appear in the standard.
π Data-Modifying Statements in WITH π
PostgreSQL allows INSERT, UPDATE, and DELETE to be used as WITH queries. This is not found in the SQL standard.
π Nonstandard Clauses π
DISTINCT ON ( ... ) is an extension of the SQL standard.
ROWS FROM( ... ) is an extension of the SQL standard.
The MATERIALIZED and NOT MATERIALIZED options of WITH are extensions of the SQL standard.
And that's the whole vibe check, bestie β source/version is PostgreSQL 14.24, date is 2026, and this iconic little manual page is SELECT(7). Go slay some queries π
β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms