What is the DISTINCT keyword

· Category: SQL & Databases

Short answer

DISTINCT removes duplicate rows from the result set, returning only unique combinations of the selected columns.

How it works

When DISTINCT is specified, the database sorts or hashes the selected rows and discards duplicates before returning results. It operates across all selected columns, not just one.

Example

SELECT DISTINCT department FROM employees;
SELECT DISTINCT country, city FROM customers;

Why it matters

DISTINCT is essential for reports that require unique lists, such as all countries where you have customers. It prevents inflated counts when joining tables that produce Cartesian products.

Common issues

  • DISTINCT on large datasets can be expensive because it requires sorting or hashing.
  • SELECT DISTINCT * compares every column, which may not produce the uniqueness you expect if timestamps or IDs differ slightly.r