All SQL topics
SQL TOPIC

SQL DISTINCT: list unique values

DISTINCT removes duplicate rows from the result, so each combination appears once.

SELECT DISTINCT country FROM customers lists each country once. With several columns, DISTINCT applies to the whole combination.

COUNT(DISTINCT customer_id) counts unique values, which is how you count customers rather than orders.

Worked example

Which sales channels have been used?

SELECT DISTINCT channel
FROM orders
ORDER BY channel;
Result on the sample data
channel
app
marketplace
web

Common mistakes

  • Using DISTINCT to hide duplicates created by a wrong join instead of fixing the join.
  • Expecting DISTINCT to apply to only one of several selected columns.