DISTINCT Clause

SELECT DISTINCT removes all duplicate rows from the result set.

SELECT DISTINCT ON ( expression [, ...] ) keeps the first row of each set of rows that match the expression. The DISTINCT ON expressions are interpreted using the same rules as ORDER BY. The first row of each set is random unless you use ORDER BY.

Match the DISTINCT ON expressions to the leftmost ORDER BY expressions. The ORDER BY clause normally contains additional expressions that determine the precedence of rows within each DISTINCT ON group.

This example shows a select statement with DISTINCT ON and ORDER BY defined. The example retrieves the most recent weather report for each location. Without ORDER BY the report returns a random time for each location.