Problem
Task
Return each product category only once from the products table.
Schema
Table Schema
products(id, title, category, price, rating)
Input
Sample Data
| id | title | category | price | rating |
|---|---|---|---|---|
| 1 | Keyboard | Accessories | 49.99 | 4.6 |
| 2 | Mouse | Accessories | 24.5 | 4.3 |
| 3 | Monitor | Displays | 219 | 4.8 |
Output
Expected Output
| category |
|---|
| Accessories |
| Displays |
Answer
Check Your Solution
Show Answer and Explanation
Correct Answer
SELECT DISTINCT category
FROM products
ORDER BY category;
Explanation
DISTINCT removes duplicate category values from the result. ORDER BY category makes the output order predictable.
Common Mistakes
- Using GROUP BY only to remove duplicates when DISTINCT is clearer.
- Selecting extra columns such as title, which makes the rows unique again.
- Forgetting ORDER BY when the result order needs to be predictable.
Concepts
Related Concepts
DISTINCT
Projection
Result Sets
SELECT
Next practice