SELECT DISTINCT Basics
Return only unique rows.
Why Duplicates Happen
Tables often contain repeated values. A customers table might list the same city across thousands of rows. When you only care about the set of distinct values, plain SELECT returns every row including repeats.
SELECT DISTINCT collapses identical rows into one. In this lesson you will learn the basics of returning only unique results.
SELECT city FROM customers;
-- Returns one row per customer, with many repeated citiesThe DISTINCT Keyword
DISTINCT goes right after SELECT. It removes duplicate rows from the result set, keeping just one copy of each unique combination of the selected columns.
SELECT DISTINCT city
FROM customers;
-- One row per unique cityAll lessons in this course
- SELECT DISTINCT Basics
- DISTINCT on Multiple Columns
- PostgreSQL DISTINCT ON
- Counting Unique Values