+ class="parameter">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.
+
+
+
UNION Clause
The UNION clause has this general form:
-select_statement UNION [ ALL ] select_statement
+select_statement UNION [ ALL | DISTINCT ] select_statement
select_statement is
any SELECT statement without an ORDER
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.
The INTERSECT clause has this general form:
-select_statement INTERSECT [ ALL ] select_statement
+select_statement INTERSECT [ ALL | DISTINCT ] select_statement
select_statement is
any SELECT statement without an ORDER
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.
The EXCEPT clause has this general form:
-select_statement EXCEPT [ ALL ] select_statement
+select_statement EXCEPT [ ALL | DISTINCT ] select_statement
select_statement is
any SELECT statement without an ORDER
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.
-
-
DISTINCT Clause
-
- If DISTINCT> is specified, all duplicate rows are
- removed from the result set (one row is kept from each group of
- duplicates). ALL> specifies the opposite: all rows are
- kept; that is the default.
-
-
-
DISTINCT ON (
- class="parameter">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.
-
-
-
LIMIT Clause
[ GROUP BY expression [, ...] ]
[ HAVING condition [, ...] ]
[ WINDOW window_name AS ( window_definition ) [, ...] ]
- [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
+ [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start [ ROW | ROWS ] ]