Unique Voices: Using DISTINCT
The Duplicate Hunt
The inventory team came to me with a simple question: *"How many different categories of products do we actually have?"*
I ran a simple `SELECT category FROM products`. The result was a list of 10,000 rows. It said "Electronics" 500 times, "Home" 300 times, and so on. It was a sea of repetition. I didn't need the "Count" yet; I just needed the list of **unique** names.
The Quest: Clearing the Redundancy
In databases, data is redundant by design. Every order has a customer ID, but many orders might have the *same* customer ID. To see the "Unique" entities in a column, we use the `DISTINCT` keyword.
The Implementation: The Unique Filter
By adding `DISTINCT` immediately after `SELECT`, you tell SQL: "Only show me a value one time. Filter out the repeats."
-- Get the unique list of categories
SELECT DISTINCT category
FROM products;
Multi-Column Uniqueness
You can even use it on multiple columns. This will find every unique **combination** of values.
-- Find every unique combination of Country and City
SELECT DISTINCT country, city
FROM customers;
The "Oops" Moment
I once put `DISTINCT` in the middle of my `SELECT` list: `SELECT name, DISTINCT category`. SQL failed.
**Pro Tip**: `DISTINCT` must always come **immediately** after `SELECT`. It applies to everything you list after it.
The Victory
A 10,000-row mess became a clean list of 12 unique categories. The inventory team could now plan their warehouse layout because they knew exactly what types of items they were dealing with. I had turned "Noise" into "Structure."
Your Task for Today
Run a query on a column that you know has duplicates (like 'category' or 'status'). Compare the result count when you use `DISTINCT` vs when you don't.
*Day 9: Fuzzy Matching—LIKE vs. Equals.*