DISTINCT and ALL - Amazon Athena
Services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the AWS European Sovereign Cloud Region, see the AWS European Sovereign Cloud User Guide.

DISTINCT and ALL

SELECT DISTINCT returns each distinct row of the result once. SELECT ALL, the default, keeps duplicate rows.

Syntax

SELECT DISTINCT select_list

Examples

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

The following SQL query returns each region once.

SELECT DISTINCT region FROM sales_db.sales_parquet ORDER BY region

The query returns the following results.

region

east

north

west

Considerations

To keep one row for each distinct value of some columns, filter on row_number() in a QUALIFY clause. For example, QUALIFY row_number() OVER (PARTITION BY region ORDER BY amount DESC NULLS LAST) = 1 keeps the largest order in each region. For more information about QUALIFY, see QUALIFY clause.