SQL Select Distinct
Sometimes you need the different values in a column, not every row that contains them. DISTINCT removes duplicate result rows after the selected expressions have been evaluated.
Understand the idea
Two products belong to Stationery. Selecting category normally repeats that label. DISTINCT returns it once. If you select two columns, uniqueness applies to the pair, not just the first column. Use this when the question is about distinct values; do not use it to hide a join that unexpectedly multiplies rows.
Where to try it
Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.
SQLite sample data · MySQL / MariaDB sample data · See the sample tables
A worked example
SELECT DISTINCT category
FROM products
ORDER BY category;
Read the result
The result has Accessories, Home, and Stationery. There are three categories even though there are four products. Sorting makes the order predictable for a dropdown or comparison.
Try one change
Select DISTINCT city from customers. Notice that the missing city is represented by NULL and is separate from an empty text value.