Easy SELECT basics Shop database

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

Press Run to see your query's result, or Check answer to compare it with the expected result.

Expected result

category
Accessories
Displays
Video

The row order matters for this question.

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.

Want to try the query on MySQL or PostgreSQL? Open it in the SQL Online Compiler or SQLite Online.