0Pricing
SQL Academy · Lesson

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 cities

The 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 city

All lessons in this course

  1. SELECT DISTINCT Basics
  2. DISTINCT on Multiple Columns
  3. PostgreSQL DISTINCT ON
  4. Counting Unique Values
← Back to SQL Academy