Remove duplicates with DISTINCT
Question
List each product category once, in alphabetical order.
customersid, name, country, joined_on
productsid, name, category, price
ordersid, customer_id, ordered_on, status
order_itemsorder_id, product_id, quantity
Your query
Ctrl+Enter to run
Result
Expected result
| category |
|---|
| Accessories |
| Displays |
| Video |
Hint
DISTINCT removes repeated rows from the result.
Show the solution
solution.sql
Run this code
SELECT DISTINCT category
FROM products
ORDER BY category;
Why it works
The products table has six rows but only three different categories. DISTINCT keeps one row for each different value, and ORDER BY sorts them A to Z.
Common mistake
DISTINCT applies to the whole row, not to one column. SELECT DISTINCT category, name would bring every product back, because each name is different.