SELECT DISTINCT tells the database engine to scan the result produced by the SELECT list and discard rows that would be identical across all columns named in that list. It operates after the FROM, WHERE, JOIN, and GROUP BY clauses have filtered and shaped the data but before ORDER BY is applied. Two rows are considered duplicates when every value in the projected columns matches (NULLs are treated as equal to NULLs). Because the database must compare each row with previously seen rows, DISTINCT can increase memory use and execution time, especially on large, unsorted datasets. Indexes that cover the DISTINCT columns or a preceding GROUP BY on the same columns can mitigate this cost. DISTINCT does not accept arguments beyond the column list, and it affects only the columns explicitly selected, not hidden columns such as those in ORDER BY unless they also appear in the SELECT list. In PostgreSQL, the DISTINCT ON variant allows picking the first row of each distinct group, but standard SQL supports only the plain DISTINCT keyword.
column_list
(list) - One or more columns or expressions to compare for uniqueness.table_name
(identifier) - Name of the source table or view.condition
(boolean) - Optional predicate that filters rows before DISTINCT is applied.SELECT, GROUP BY, DISTINCT ON, UNION, HAVING, COUNT(DISTINCT), ORDER BY
SQL-92 standard
It removes duplicate rows from the query result, returning each unique value or combination only once.
Case sensitivity depends on the database collation. In most default collations for MySQL and PostgreSQL it is case-insensitive, while binary collations treat case differently.
Yes. You can write expressions like COUNT(DISTINCT column) to count unique values. However, you cannot place DISTINCT after an aggregate in the SELECT list.
Add an ORDER BY clause after the SELECT DISTINCT statement: `SELECT DISTINCT city FROM customers ORDER BY city;`