A user can only select one table or query to perform a simple select query on

The

WHERE condition
9 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
2,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
3,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
4,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
5 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
6 statement. When writing a data-modifying statement (
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
4,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
5 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
6) in
WHERE condition
9, it is usual to include a
HAVING condition
1 clause. It is the output of
HAVING condition
1, not the underlying table that the statement modifies, that forms the temporary table that is read by the primary query. If
HAVING condition
1 is omitted, the statement is still executed, but it produces no output so it cannot be referenced as a table by the primary query.

A name (without schema qualification) must be specified for each

WHERE condition
9 query. Optionally, a list of column names can be specified; if this is omitted, the column names are inferred from the subquery.

If

HAVING condition
5 is specified, it allows a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 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

HAVING condition
7. 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 query in a data-modifying statement. See Section 7.8 for an example.

Another effect of

HAVING condition
5 is that
WHERE condition
9 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
HAVING condition
5,
WHERE condition
9 queries can only reference sibling
WHERE condition
9 queries that are earlier in the
WHERE condition
9 list.

When there are multiple queries in the

WHERE condition
9 clause,
HAVING condition
5 should be written only once, immediately after
WHERE condition
9. It applies to all queries in the
WHERE condition
9 clause, though it has no effect on queries that do not use recursion or forward references.

The optional

WINDOW window_name AS ( window_definition ) [, ...]
9 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
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
0 will be added to the result column list of the
WHERE condition
9 query. This column can be ordered by in the outer query to achieve the respective ordering. See for examples.

The optional

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
2 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
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
3 will be added to the result column list of the
WHERE condition
9 query. This column will be set to
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
5 when a cycle has been detected, else to
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
6. Furthermore, processing of the recursive union will stop when a cycle has been detected.
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
5 and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
6 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,
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
9 and
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
0 (of type
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
1) are used. Furthermore, a column named
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
2 will be added to the result column list of the
WHERE condition
9 query. This column is used internally for tracking visited rows. See for examples.

Both the

WINDOW window_name AS ( window_definition ) [, ...]
9 and the
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
2 clause are only valid for recursive
WHERE condition
9 queries. The
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
7 must be a
HAVING condition
7 (or
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
9) of two
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 (or equivalent) commands (no nested
HAVING condition
7s). If both clauses are used, the column added by the
WINDOW window_name AS ( window_definition ) [, ...]
9 clause appears before the columns added by the
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
2 clause.

The primary query and the

WHERE condition
9 queries are all (notionally) executed at the same time. This implies that the effects of a data-modifying statement in
WHERE condition
9 cannot be seen from other parts of the query, other than by reading its
HAVING condition
1 output. If two such data-modifying statements attempt to modify the same row, the results are unspecified.

A key property of

WHERE condition
9 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.

However, a

WHERE condition
9 query can be marked
UNBOUNDED PRECEDING
offset PRECEDING
CURRENT ROW
offset FOLLOWING
UNBOUNDED FOLLOWING
9 to remove this guarantee. In that case, the
WHERE condition
9 query can be folded into the primary query much as though it were a simple sub-
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 in the primary query's
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause. This results in duplicate computations if the primary query refers to that
WHERE condition
9 query more than once; but if each such use requires only a few rows of the
WHERE condition
9 query's total output,
UNBOUNDED PRECEDING
offset PRECEDING
CURRENT ROW
offset FOLLOWING
UNBOUNDED FOLLOWING
9 can provide a net savings by allowing the queries to be optimized jointly.
UNBOUNDED PRECEDING
offset PRECEDING
CURRENT ROW
offset FOLLOWING
UNBOUNDED FOLLOWING
9 is ignored if it is attached to a
WHERE condition
9 query that is recursive or is not side-effect-free (i.e., is not a plain
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 containing no volatile functions).

By default, a side-effect-free

WHERE condition
9 query is folded into the primary query if it is used exactly once in the primary query's
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause. This allows joint optimization of the two query levels in situations where that should be semantically invisible. However, such folding can be prevented by marking the
WHERE condition
9 query as
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
2. That might be useful, for example, if the
WHERE condition
9 query is being used as an optimization fence to prevent the planner from choosing a bad plan. PostgreSQL versions before v12 never did such folding, so queries written for older versions might rely on
WHERE condition
9 to act as an optimization fence.

See Section 7.8 for additional information.

EXCLUDE CURRENT ROW EXCLUDE GROUP EXCLUDE TIES EXCLUDE NO OTHERS 2 Clause

The

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause specifies one or more source tables for the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1. If multiple sources are specified, the result is the Cartesian product (cross join) of all the sources. But usually qualification conditions are added (via
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8) to restrict the returned rows to a small subset of the Cartesian product.

The

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause can contain the following elements:

WHERE condition
00

The name (optionally schema-qualified) of an existing table or view. If

WHERE condition
01 is specified before the table name, only that table is scanned. If
WHERE condition
01 is not specified, the table and all its descendant tables (if any) are scanned. Optionally,
WHERE condition
03 can be specified after the table name to explicitly indicate that descendant tables are included.

WHERE condition
04

A substitute name for the

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 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
WHERE condition
06, the remainder of the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 must refer to this
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item as
WHERE condition
09 not
WHERE condition
10. 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.

WHERE condition
11 (
WHERE condition
12 [, ...] ) [ REPEATABLE (
WHERE condition
13 ) ]

A

WHERE condition
14 clause after a
WHERE condition
00 indicates that the specified
WHERE condition
16 should be used to retrieve a subset of the rows in that table. This sampling precedes the application of any other filters such as
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 clauses. The standard PostgreSQL distribution includes two sampling methods,
WHERE condition
18 and
WHERE condition
19, and other sampling methods can be installed in the database via extensions.

The

WHERE condition
18 and
WHERE condition
19 sampling methods each accept a single
WHERE condition
12 which is the fraction of the table to sample, expressed as a percentage between 0 and 100. This argument can be any
WHERE condition
23-valued expression. (Other sampling methods might accept more or different arguments.) These two methods each return a randomly-chosen sample of the table that will contain approximately the specified percentage of the table's rows. The
WHERE condition
18 method scans the whole table and selects or ignores individual rows independently with the specified probability. The
WHERE condition
19 method does block-level sampling with each block having the specified chance of being selected; all rows in each selected block are returned. The
WHERE condition
19 method is significantly faster than the
WHERE condition
18 method when small sampling percentages are specified, but it may return a less-random sample of the table as a result of clustering effects.

The optional

WHERE condition
28 clause specifies a
WHERE condition
13 number or expression to use for generating random numbers within the sampling method. The seed value can be any non-null floating-point value. Two queries that specify the same seed and
WHERE condition
12 values will select the same sample of the table, if the table has not been changed meanwhile. But different seed values will usually produce different samples. If
WHERE condition
28 is not given then a new random sample is selected for each query, based upon a system-generated seed. Note that some add-on sampling methods do not accept
WHERE condition
28, and will always produce new samples on each use.

WHERE condition
33

A sub-

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 can appear in the
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause. This acts as though its output were created as a temporary table for the duration of this single
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 command. Note that the sub-
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 must be surrounded by parentheses, and an alias must be provided for it. A
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
3 command can also be used here.

WHERE condition
39

A

WHERE condition
9 query is referenced by writing its name, just as though the query's name were a table name. (In fact, the
WHERE condition
9 query hides any real table of the same name for the purposes of the primary query. If necessary, you can refer to a real table of the same name by schema-qualifying the table's name.) An alias can be provided in the same way as for a table.

WHERE condition
42

Function calls can appear in the

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 command. If the function's result type is composite (including the case of a function with multiple
WHERE condition
45 parameters), each attribute becomes a separate column in the implicit table.

When the optional

WHERE condition
46 clause is added to the function call, an additional column of type
WHERE condition
47 will be 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
WHERE condition
48.

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 ordinality column if present.

Multiple function calls can be combined into a single

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2-clause item by surrounding them with
WHERE condition
50. 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 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.

If the function has been defined as returning the

WHERE condition
51 data type, then an alias or the key word
WHERE condition
52 must be present, followed by a column definition list in the form
WHERE condition
53
WHERE condition
54 [, ... ]). The column definition list must match the actual number and types of columns returned by the function.

When using the

WHERE condition
50 syntax, if one of the functions requires a column definition list, it's preferred to put the column definition list after the function call inside
WHERE condition
50. A column definition list can be placed after the
WHERE condition
50 construct only if there's just a single function and no
WHERE condition
46 clause.

To use

WHERE condition
59 together with a column definition list, you must use the
WHERE condition
50 syntax and put the column definition list inside
WHERE condition
50.

WHERE condition
62

One of

  • WHERE condition
    
    63

  • WHERE condition
    
    64

  • WHERE condition
    
    65

  • WHERE condition
    
    66

For the

WHERE condition
67 and
WHERE condition
68 join types, a join condition must be specified, namely exactly one of
WHERE condition
69,
WHERE condition
70 [, ...]), or
WHERE condition
71. See below for the meaning.

A

WHERE condition
72 clause combines two
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 items, which for convenience we will refer to as “tables”, though in reality they can be any type of
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses,
WHERE condition
72s nest left-to-right. In any case
WHERE condition
72 binds more tightly than the commas separating
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2-list items. All the
WHERE condition
72 options are just a notational convenience, since they do nothing you couldn't do with plain
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 and
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8.

WHERE condition
81 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. 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
WHERE condition
72 clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards.

Conversely,

WHERE condition
83 returns all the joined rows, plus one row for each unmatched right-hand row (extended with nulls on the left). This is just a notational convenience, since you could convert it to a
WHERE condition
81 by switching the left and right tables.

WHERE condition
85 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).

WHERE condition
69

WHERE condition
87 is an expression resulting in a value of type
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
1 (similar to a
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 clause) that specifies which rows in a join are considered to match.

WHERE condition
90 [, ...] ) [ AS
WHERE condition
91 ]

A clause of the form

WHERE condition
92 is shorthand for
WHERE condition
93. Also,
WHERE condition
94 implies that only one of each pair of equivalent columns will be included in the join output, not both.

If a

WHERE condition
91 name is specified, it provides a table alias for the join columns. Only the join columns listed in the
WHERE condition
94 clause are addressable by this name. Unlike a regular
WHERE condition
04, this does not hide the names of the joined tables from the rest of the query. Also unlike a regular
WHERE condition
04, you cannot write a column alias list — the output names of the join columns are the same as they appear in the
WHERE condition
94 list.

WHERE condition
71

WHERE condition
71 is shorthand for a
WHERE condition
94 list that mentions all columns in the two tables that have matching names. If there are no common column names,
WHERE condition
71 is equivalent to
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
04.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
05

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
05 is equivalent to
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
07, that is, no rows are removed by qualification. They produce a simple Cartesian product, the same result as you get from listing the two tables at the top level of
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2, but restricted by the join condition (if any).

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09

The

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 key word can precede a sub-
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item. This allows the sub-
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 to refer to columns of
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 items that appear before it in the
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 list. (Without
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09, each sub-
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 is evaluated independently and so cannot cross-reference any other
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item.)

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 can also precede a function-call
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item, but in this case it is a noise word, because the function expression can refer to earlier
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 items in any case.

A

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 item can appear at top level in the
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 list, or within a
WHERE condition
72 tree. In the latter case it can also refer to any items that are on the left-hand side of a
WHERE condition
72 that it is on the right-hand side of.

When a

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item contains
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 cross-references, evaluation proceeds as follows: for each row of the
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 item providing the cross-referenced column(s), or set of rows of multiple
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 items providing the columns, the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 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

WHERE condition
67 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
32 joined to the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 item, else there would not be a well-defined set of rows from which to compute each set of rows for the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
09 item. Thus, although a construct such as
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
35 RIGHT JOIN LATERAL
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
36 is syntactically valid, it is not actually allowed for
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
36 to reference
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
38.

SELECT DISTINCT ON (location) location, time, report FROM weather_reports ORDER BY location, time DESC; 8 Clause

The optional

SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 clause has the general form

WHERE condition

where

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
41 is any expression that evaluates to a result of type
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
1. 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 [ ALL | DISTINCT ] grouping_element [, ...] 43 Clause

The optional

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clause has the general form

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 will condense into a single row all selected rows that share the same values for the grouped expressions. An
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
46 used inside a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
47 can be an input column name, or the name or ordinal number of an output column (
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 name will be interpreted as an input-column name rather than an output column name.

If any of

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
50,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
51 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
52 are present as grouping elements, then the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clause as a whole defines some number of independent
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
54. The effect of this is equivalent to constructing a
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
9 between subqueries with the individual grouping sets as their
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clauses. The optional
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57 clause removes duplicate sets before processing; it does not transform the
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
9 into a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
59. For further details on the handling of grouping sets see .

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 [ ALL | DISTINCT ] grouping_element [, ...]
43 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
61 clause to the aggregate function call; see for more information. When a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
61 clause is present, only those rows matching it are included in the input to that aggregate function.

When

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 is present, or any aggregate functions are present, it is not valid for the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 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

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 clause or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 list. This means that, for example, a
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
67 expression cannot be used to skip evaluation of an aggregate function; see .

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified with
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] 65 Clause

The optional

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 clause has the general form

HAVING condition

where

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
41 is the same as specified for the
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 clause.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 eliminates group rows that do not satisfy the condition.
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 is different from
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8:
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 filters individual rows before the application of
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43, while
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 filters group rows created by
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43. Each column referenced in
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
41 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

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 turns a query into a grouped query even if there is no
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clause. This is the same as what happens when the query contains aggregate functions but no
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clause. All the selected rows are considered to form a single group, and the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 list and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 clause can only reference table columns from within aggregate functions. Such a query will emit a single row if the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 condition is true, zero rows if it is not true.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified with
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] 96 Clause

The optional

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause has the general form

WINDOW window_name AS ( window_definition ) [, ...]

where

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
98 is a name that can be referenced from
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
99 clauses or subsequent window definitions, and
HAVING condition
00 is

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]

If an

HAVING condition
01 is specified it must refer to an earlier entry in the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 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
HAVING condition
03 clause, and it can specify
HAVING condition
04 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.

The elements of the

HAVING condition
03 list are interpreted in much the same fashion as elements of a 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 [ ALL | DISTINCT ] grouping_element [, ...]
43 clause. They are allowed here because windowing occurs after grouping and aggregation.

Similarly, the elements of the

HAVING condition
04 list are interpreted in much the same fashion as elements of a statement-level clause, except that the expressions are always taken as simple expressions and never the name or number of an output column.

The optional

HAVING condition
10 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
HAVING condition
10 can be one of

{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]

where

HAVING condition
12 and
HAVING condition
13 can be one of

UNBOUNDED PRECEDING
offset PRECEDING
CURRENT ROW
offset FOLLOWING
UNBOUNDED FOLLOWING

and

HAVING condition
14 can be one of

EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS

If

HAVING condition
13 is omitted it defaults to
HAVING condition
16. Restrictions are that
HAVING condition
12 cannot be
HAVING condition
18,
HAVING condition
13 cannot be
HAVING condition
20, and the
HAVING condition
13 choice cannot appear earlier in the above list of
HAVING condition
12 and
HAVING condition
13 options than the
HAVING condition
12 choice does — for example
HAVING condition
25 PRECEDING is not allowed.

The default framing option is

HAVING condition
26, which is the same as
HAVING condition
27; 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
HAVING condition
04 clause considers equivalent to the current row; all rows are peers if there is no
HAVING condition
04). In general,
HAVING condition
20 means that the frame starts with the first row of the partition, and similarly
HAVING condition
18 means that the frame ends with the last row of the partition, regardless of
HAVING condition
32,
HAVING condition
33 or
HAVING condition
34 mode. In
HAVING condition
33 mode,
HAVING condition
16 means that the frame starts or ends with the current row; but in
HAVING condition
32 or
HAVING condition
34 mode it means that the frame starts or ends with the current row's first or last peer in the
HAVING condition
04 ordering. The
HAVING condition
40
HAVING condition
41 and
HAVING condition
40
HAVING condition
43 options vary in meaning depending on the frame mode. In
HAVING condition
33 mode, the
HAVING condition
40 is an integer indicating that the frame starts or ends that many rows before or after the current row. In
HAVING condition
34 mode, the
HAVING condition
40 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
HAVING condition
04 clause. In
HAVING condition
32 mode, use of an
HAVING condition
40 option requires that there be exactly one
HAVING condition
04 column in the window definition. Then the frame contains those rows whose ordering column value is no more than
HAVING condition
40 less than (for
HAVING condition
41) or more than (for
HAVING condition
43) the current row's ordering column value. In these cases the data type of the
HAVING condition
40 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
HAVING condition
56. In all these cases, the value of the
HAVING condition
40 must be non-null and non-negative. Also, while the
HAVING condition
40 does not have to be a simple constant, it cannot contain variables, aggregate functions, or window functions.

The

HAVING condition
14 option allows rows around the current row to be excluded from the frame, even if they would be included according to the frame start and frame end options.
HAVING condition
60 excludes the current row from the frame.
HAVING condition
61 excludes the current row and its ordering peers from the frame.
HAVING condition
62 excludes any peers of the current row from the frame, but not the current row itself.
HAVING condition
63 simply specifies explicitly the default behavior of not excluding the current row or its peers.

Beware that the

HAVING condition
33 mode can produce unpredictable results if the
HAVING condition
04 ordering does not order the rows uniquely. The
HAVING condition
32 and
HAVING condition
34 modes are designed to ensure that rows that are peers in the
HAVING condition
04 ordering are treated alike: all rows of a given peer group will be in the frame or excluded from it.

The purpose of a

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause is to specify the behavior of window functions appearing in the query's or clause. These functions can reference the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause entries by name in their
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
99 clauses. A
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause entry does not have to be referenced anywhere, however; if it is not used in the query it is simply ignored. It is possible to use window functions without any
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause at all, since a window function call can specify its window definition directly in its
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
99 clause. However, the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96 clause saves typing when the same window definition is needed for more than one window function.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified with
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
96.

Window functions are described in detail in Section 3.5, , and .

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] 1 List

The

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 list (between the key words
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 and
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2) specifies expressions that form the output rows of the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement. The expressions can (and usually do) refer to columns computed in the
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
2 clause.

Just as in a table, every output column of a

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 has a name. In a simple
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 this name is just used to label the column for display, but when the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 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
WHERE condition
52
HAVING condition
93 after the column's expression. (You can omit
WHERE condition
52, 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
WHERE condition
52 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
HAVING condition
96.

An output column's name can be used to refer to the column's value in

HAVING condition
04 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 clauses, but not in the
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
65 clauses; there you must write out the expression instead.

Instead of an expression,

WHERE condition
03 can be written in the output list as a shorthand for all the columns of the selected rows. Also, you can write
WHERE condition
00.*
as a shorthand for the columns coming from just that table. In these cases it is not possible to specify new names with
WHERE condition
52; 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

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57,
HAVING condition
04, or
WINDOW window_name AS ( window_definition ) [, ...]
06. This is obviously necessary when using
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57, 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
HAVING condition
04 and
WINDOW window_name AS ( window_definition ) [, ...]
06; 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57,
HAVING condition
04 or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43. (As a counterexample,
WINDOW window_name AS ( window_definition ) [, ...]
13 clearly must evaluate
WINDOW window_name AS ( window_definition ) [, ...]
14 before sorting.) Output expressions that contain set-returning functions are effectively evaluated after sorting and before limiting, so that
WINDOW window_name AS ( window_definition ) [, ...]
06 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.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] 57 Clause

If

WINDOW window_name AS ( window_definition ) [, ...]
17 is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates).
WINDOW window_name AS ( window_definition ) [, ...]
18 specifies the opposite: all rows are kept; that is the default.

SELECT DISTINCT ON (

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
46 [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The
WINDOW window_name AS ( window_definition ) [, ...]
20 expressions are interpreted using the same rules as for
HAVING condition
04 (see above). Note that the “first row” of each set is unpredictable unless
HAVING condition
04 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

HAVING condition
04 to force descending order of time values for each location, we'd have gotten a report from an unpredictable time for each location.

The

WINDOW window_name AS ( window_definition ) [, ...]
20 expression(s) must match the leftmost
HAVING condition
04 expression(s). The
HAVING condition
04 clause will normally contain additional expression(s) that determine the desired precedence of rows within each
WINDOW window_name AS ( window_definition ) [, ...]
20 group.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified with
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57.

HAVING condition 7 Clause

The

HAVING condition
7 clause has this general form:

WHERE condition
0

WINDOW window_name AS ( window_definition ) [, ...]
35 is any
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement without an
HAVING condition
04,
WINDOW window_name AS ( window_definition ) [, ...]
06,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70, or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 clause. (
HAVING condition
04 and
WINDOW window_name AS ( window_definition ) [, ...]
06 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
HAVING condition
7, not to its right-hand input expression.)

The

HAVING condition
7 operator computes the set union of the rows returned by the involved
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 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
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statements that represent the direct operands of the
HAVING condition
7 must produce the same number of columns, and corresponding columns must be of compatible data types.

The result of

HAVING condition
7 does not contain any duplicate rows unless the
WINDOW window_name AS ( window_definition ) [, ...]
51 option is specified.
WINDOW window_name AS ( window_definition ) [, ...]
51 prevents elimination of duplicates. (Therefore,
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
9 is usually significantly quicker than
HAVING condition
7; use
WINDOW window_name AS ( window_definition ) [, ...]
51 when you can.)
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57 can be written to explicitly specify the default behavior of eliminating duplicate rows.

Multiple

HAVING condition
7 operators in the same
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement are evaluated left to right, unless otherwise indicated by parentheses.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified either for a
HAVING condition
7 result or for any input of a
HAVING condition
7.

WINDOW window_name AS ( window_definition ) [, ...] 65 Clause

The

WINDOW window_name AS ( window_definition ) [, ...]
65 clause has this general form:

WHERE condition
1

WINDOW window_name AS ( window_definition ) [, ...]
35 is any
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement without an
HAVING condition
04,
WINDOW window_name AS ( window_definition ) [, ...]
06,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70, or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 clause.

The

WINDOW window_name AS ( window_definition ) [, ...]
65 operator computes the set intersection of the rows returned by the involved
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statements. A row is in the intersection of two result sets if it appears in both result sets.

The result of

WINDOW window_name AS ( window_definition ) [, ...]
65 does not contain any duplicate rows unless the
WINDOW window_name AS ( window_definition ) [, ...]
51 option is specified. With
WINDOW window_name AS ( window_definition ) [, ...]
51, a row that has
WINDOW window_name AS ( window_definition ) [, ...]
80 duplicates in the left table and
WINDOW window_name AS ( window_definition ) [, ...]
81 duplicates in the right table will appear min(
WINDOW window_name AS ( window_definition ) [, ...]
80,
WINDOW window_name AS ( window_definition ) [, ...]
81) times in the result set.
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57 can be written to explicitly specify the default behavior of eliminating duplicate rows.

Multiple

WINDOW window_name AS ( window_definition ) [, ...]
65 operators in the same
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement are evaluated left to right, unless parentheses dictate otherwise.
WINDOW window_name AS ( window_definition ) [, ...]
65 binds more tightly than
HAVING condition
7. That is,
WINDOW window_name AS ( window_definition ) [, ...]
89 will be read as
WINDOW window_name AS ( window_definition ) [, ...]
90.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified either for an
WINDOW window_name AS ( window_definition ) [, ...]
65 result or for any input of an
WINDOW window_name AS ( window_definition ) [, ...]
65.

WINDOW window_name AS ( window_definition ) [, ...] 97 Clause

The

WINDOW window_name AS ( window_definition ) [, ...]
97 clause has this general form:

WHERE condition
2

WINDOW window_name AS ( window_definition ) [, ...]
35 is any
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement without an
HAVING condition
04,
WINDOW window_name AS ( window_definition ) [, ...]
06,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70, or
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 clause.

The

WINDOW window_name AS ( window_definition ) [, ...]
97 operator computes the set of rows that are in the result of the left
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement but not in the result of the right one.

The result of

WINDOW window_name AS ( window_definition ) [, ...]
97 does not contain any duplicate rows unless the
WINDOW window_name AS ( window_definition ) [, ...]
51 option is specified. With
WINDOW window_name AS ( window_definition ) [, ...]
51, a row that has
WINDOW window_name AS ( window_definition ) [, ...]
80 duplicates in the left table and
WINDOW window_name AS ( window_definition ) [, ...]
81 duplicates in the right table will appear max(
WINDOW window_name AS ( window_definition ) [, ...]
80-
WINDOW window_name AS ( window_definition ) [, ...]
81,0) times in the result set.
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
57 can be written to explicitly specify the default behavior of eliminating duplicate rows.

Multiple

WINDOW window_name AS ( window_definition ) [, ...]
97 operators in the same
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 statement are evaluated left to right, unless parentheses dictate otherwise.
WINDOW window_name AS ( window_definition ) [, ...]
97 binds at the same level as
HAVING condition
7.

Currently,

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
68,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
69,
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
70 and
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
71 cannot be specified either for an
WINDOW window_name AS ( window_definition ) [, ...]
97 result or for any input of an
WINDOW window_name AS ( window_definition ) [, ...]
97.

HAVING condition 04 Clause

The optional

HAVING condition
04 clause has this general form:

WHERE condition
3

The

HAVING condition
04 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

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
46 can be the name or ordinal number of an output column (
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 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

WHERE condition
52 clause.

It is also possible to use arbitrary expressions in the

HAVING condition
04 clause, including columns that do not appear in the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
1 output list. Thus the following statement is valid:

WHERE condition
4

A limitation of this feature is that an

HAVING condition
04 clause applying to the result of a
HAVING condition
7,
WINDOW window_name AS ( window_definition ) [, ...]
65, or
WINDOW window_name AS ( window_definition ) [, ...]
97 clause can only specify an output column name or number, not an expression.

If an

HAVING condition
04 expression is a simple name that matches both an output column name and an input column name,
HAVING condition
04 will interpret it as the output column name. This is the opposite of the choice that
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
43 will make in the same situation. This inconsistency is made to be compatible with the SQL standard.

Optionally one can add the key word

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
42 (ascending) or
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
43 (descending) after any expression in the
HAVING condition
04 clause. If not specified,
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
42 is assumed by default. Alternatively, a specific ordering operator name can be specified in the
WHERE condition
94 clause. An ordering operator must be a less-than or greater-than member of some B-tree operator family.
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
42 is usually equivalent to
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
48 and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
43 is usually equivalent to
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
50. (But 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

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
51 is specified, null values sort after all non-null values; if
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
52 is specified, null values sort before all non-null values. If neither is specified, the default behavior is
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
51 when
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
42 is specified or implied, and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
52 when
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
43 is specified (thus, the default is to act as though nulls are larger than non-nulls). When
WHERE condition
94 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

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
58 does not mean the same thing as
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
59.

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

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
60 clause in the
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
46, for example
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
62. For more information see and Section 24.2.

WINDOW window_name AS ( window_definition ) [, ...] 06 Clause

The

WINDOW window_name AS ( window_definition ) [, ...]
06 clause consists of two independent sub-clauses:

WHERE condition
5

The parameter

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
65 specifies the maximum number of rows to return, while
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
66 specifies the number of rows to skip before starting to return rows. When both are specified,
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
66 rows are skipped before starting to count the
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
65 rows to be returned.

If the

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
65 expression evaluates to NULL, it is treated as
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
70, i.e., no limit. If
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
66 evaluates to NULL, it is treated the same as
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
72.

SQL:2008 introduced a different syntax to achieve the same result, which PostgreSQL also supports. It is:

WHERE condition
6

In this syntax, the

[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
66 or
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
65 value is required by the standard to be a literal constant, a parameter, or a variable name; as a PostgreSQL extension, other expressions are allowed, but will generally need to be enclosed in parentheses to avoid ambiguity. If
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
65 is omitted in a
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
76 clause, it defaults to 1. The
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
77 option is used to return any additional rows that tie for the last place in the result set according to the
HAVING condition
04 clause;
HAVING condition
04 is mandatory in this case, and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
80 is not allowed.
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
81 and
HAVING condition
33 as well as
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
83 and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
84 are noise words that don't influence the effects of these clauses. According to the standard, the
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
85 clause must come before the
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
76 clause if both are present; but PostgreSQL is laxer and allows either order.

When using

WINDOW window_name AS ( window_definition ) [, ...]
06, it is a good idea to use an
HAVING condition
04 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 don't know what ordering unless you specify
HAVING condition
04.

The query planner takes

WINDOW window_name AS ( window_definition ) [, ...]
06 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
WINDOW window_name AS ( window_definition ) [, ...]
06 and
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
85. Thus, using different
WINDOW window_name AS ( window_definition ) [, ...]
06/
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
85 values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with
HAVING condition
04. 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
HAVING condition
04 is used to constrain the order.

It is even possible for repeated executions of the same

WINDOW window_name AS ( window_definition ) [, ...]
06 query to return different subsets of the rows of a table, if there is not an
HAVING condition
04 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.

GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] 2 Command

The command

WHERE condition
7

is equivalent to

WHERE condition
8

It can be used as a top-level command or as a space-saving syntax variant in parts of complex queries. Only the

WHERE condition
9,
HAVING condition
7,
WINDOW window_name AS ( window_definition ) [, ...]
65,
WINDOW window_name AS ( window_definition ) [, ...]
97,
HAVING condition
04,
WINDOW window_name AS ( window_definition ) [, ...]
06,
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
85,
[ existing_window_name ]
[ PARTITION BY expression [, ...] ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ frame_clause ]
76 and
{ RANGE | ROWS | GROUPS } frame_start [ frame_exclusion ]
{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end [ frame_exclusion ]
08 locking clauses can be used with
GROUP BY [ ALL | DISTINCT ] grouping_element [, ...]
2; the
SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;
8 clause and any form of aggregation cannot be used.

What is a SELECT query in MS Access?

A select query is a database object that shows information in Datasheet view. A query does not store data, it displays data that is stored in tables. A query can show data from one or more tables, from other queries, or from a combination of the two.

What type of query creates a new table using the query results?

A make table query retrieves data from one or more tables, and then loads the result set into a new table. That new table can reside in the database that you have open, or you can create it in another database.

What can queries be used for?

A query can give you an answer to a simple question, perform calculations, combine data from different tables, add, change, or delete data from a database. Since queries are so versatile, there are many types of queries and you would create a type of query based on the task.

What happens when you run a SELECT query in Access?

You use select queries and crosstab queries to retrieve and present data, and to supply forms and reports with data. When you run a select or a crosstab query, Access displays the results in Datasheet view.