Quick Syntax
SELECT DISTINCT column_name
FROM table_name;
This tells SQL:
“Hey, just show me each value once — skip the duplicates.”
Here’s the Table You’re Working With
Let’s say you’ve got a table called orders
:
order_id | customer_name | product | city |
---|---|---|---|
1 | Brill Venn | mug | Fogtown |
2 | Larka Tove | t-shirt | Fogtown |
3 | Brill Venn | mug | Dustvale |
4 | Milo Kresh | hoodie | Fogtown |
5 | Vesh Luro | mug | Windmere |
6 | Tola Jinn | t-shirt | Fogtown |
You can already see some repeats — like “Fogtown” shows up more than once, and so does “mug.” Let’s clean that up using DISTINCT
.
Suppose You Want to See All the Cities
SELECT DISTINCT city
FROM orders;
Output:
Fogtown
Dustvale
Windmere
This gave you a unique list of cities — no duplicates.
Suppose You Want to See All the Products
SELECT DISTINCT product
FROM orders;
Output:
mug
t-shirt
hoodie
Even though “mug” shows up multiple times, it’s only listed once here. That’s the magic of DISTINCT
.
Suppose You Want Unique City + Product Pairs
Now you’re curious about which products were sold in which cities — again, just once per pair.
SELECT DISTINCT city, product
FROM orders;
Output:
Fogtown mug
Fogtown t-shirt
Fogtown hoodie
Dustvale mug
Windmere mug
Here, SQL is looking at both city
and product
together. It’s only showing a row if that exact combo hasn’t already appeared. So even if “mug” was sold in three cities, it shows up once per location.
Ready for the next move? Check out ORDER BY
— it helps you sort your results (like A-Z, Z-A, lowest to highest, etc).
See how it works here.