---
title: "DISTINCT"
description: "DISTINCT removes duplicate rows from a query's result set, returning only unique combinations of the selected columns."
canonical: "https://motherduck.com/glossary/distinct/"
related:
  - title: "DuckDB SQL | MotherDuck Docs"
    url: "https://motherduck.com/docs/sql-reference/duckdb-sql-reference/"
  - title: "DuckLake Architecture Deep Dive"
    url: "https://motherduck.com/blog/ducklake-architecture-deep-dive/"
  - title: "CREATE INDEX | MotherDuck Docs"
    url: "https://motherduck.com/docs/sql-reference/duckdb-sql-reference/duckdb-statements/create-index/"
---

# DISTINCT

> DISTINCT removes duplicate rows from a query's result set, returning only unique combinations of the selected columns.

## Overview
`DISTINCT` is a `SELECT` modifier that eliminates duplicate rows from the result set. Two rows are considered duplicates when every selected column has the same value in both. It's commonly used to answer questions like "what are the unique countries in this table?" or "how many distinct customers placed an order this month?"

```sql
SELECT DISTINCT country
FROM customers;

SELECT COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;
```

`DISTINCT` applies to the entire row of selected columns, not just the first one -- `SELECT DISTINCT country, city` deduplicates on the combination of both columns together.

<glossary-callout guide="duckdb-cheatsheet-full" />

## DISTINCT ON in DuckDB
DuckDB (like PostgreSQL) supports `DISTINCT ON`, which returns one row per unique value of a specified expression, rather than requiring the whole row to be unique. Combined with `ORDER BY`, it's a concise way to pick "the top row per group" in a single query:

```sql
SELECT DISTINCT ON (country) country, city, population
FROM cities
ORDER BY country, population DESC;
```

This returns the most populous city for each country, without a window function or subquery. Without an `ORDER BY`, the row returned for each group is unspecified.

## Performance considerations
`DISTINCT` requires comparing (and typically sorting or hashing) every row, which can be expensive on large tables. When you only need uniqueness on a subset of columns, `DISTINCT ON` or a `GROUP BY` is often cheaper than deduplicating the full row.

## DISTINCT vs GROUP BY
`SELECT DISTINCT col FROM t` and `SELECT col FROM t GROUP BY col` return equivalent results when no aggregate functions are involved; `GROUP BY` is generally preferred when you also need to compute aggregates alongside the grouping column.