Removing duplicates with DISTINCT
Which cities do our customers come from? If you select the city column from customers, a city with 500 customers shows up 500 times. DISTINCT fixes that.
Without DISTINCT:
SELECT city FROM customers;
You get one row per customer, repeats included.
With DISTINCT:
SELECT DISTINCT city FROM customers;
Now each city appears once. The database removes the repeats from your result only. The table itself doesn’t change.
With more than one column, DISTINCT looks at the combination:
SELECT DISTINCT city, name FROM customers;
This keeps every unique pairing of city and name, so a city can still appear more than once.
DISTINCT is useful for exploring a table you don’t know. It quickly shows which values a column really contains, including typos such as Hyderabad and Hyderbad appearing as separate entries.
Go deeper: SQLite SELECT reference
What column would you check for messy values first?
Join the conversation on LinkedIn