Skip to main content

DISTINCT

DISTINCT returns unique result rows for selected columns.

Use it when you want unique values, not every raw record.

Core Concepts

flowchart LR
A[Raw query results] --> B[DISTINCT applied]
B --> C[Duplicate rows removed]
QueryMeaning
SELECT city FROM customers;Includes duplicates
SELECT DISTINCT city FROM customers;Unique city values

Code Examples

-- Setup sample data with repeated values.
CREATE TABLE visits (
visit_id INTEGER PRIMARY KEY,
city TEXT NOT NULL
);

INSERT INTO visits (city)
VALUES ('Tokyo'), ('Paris'), ('Tokyo');
Expected output
Table created and three rows inserted.
-- Unique list of cities.
SELECT DISTINCT city
FROM visits;
city
Tokyo
Paris

SQLite-Specific Nuances

SQLite Nuance

DISTINCT works on the full selected column set. If you select multiple columns, uniqueness is based on the combination.

Common Pitfalls / Best Practices

Pitfall

Using DISTINCT to hide schema or join mistakes that create accidental duplicates.

Best Practice

Use DISTINCT intentionally for reporting/lookup lists, and investigate unexpected duplicates at the source.

Quick Challenge

Return unique city values from visits in alphabetical order.

View Solution
SELECT DISTINCT city
FROM visits
ORDER BY city;
city
Paris
Tokyo