Ravindra BagaleCourses & study guides Track your progress

5. ORDER BY, DISTINCT and Top-N Results

5.3 How to Remove Duplicate Values with DISTINCT

Why. Duplicate city names clutter a “list of cities” answer.

What. DISTINCT removes duplicate result rows for the selected columns.

How. SELECT DISTINCT city FROM orders ORDER BY city;

Steps

  1. List cities without DISTINCT — see repeats.
  2. Add DISTINCT.
  3. Count how many unique cities.

Examples

Example 1 · dataset: raja_coffee

SELECT DISTINCT city FROM orders ORDER BY city;

Output

city
Goa
Kolhapur
Mumbai
Nashik
Pune
Solapur

Meaning. Each city once.

Ravindra Bagale's Tip

DISTINCT applies to the whole selected row, not one column in isolation when you select many columns.