DISTINCT in SQL

Level 374 keystrokes

DISTINCT removes duplicate values: the query lists each category of the products priced over 10 once, sorted by name (Electronics, Furniture, Tools).

SQL code

-- DISTINCT returns each category only once, even if many products share it
1SELECT DISTINCT
2 category
-- Reads from the products table
3FROM
4 products
-- WHERE runs before DISTINCT: only products priced over 10 are considered
5WHERE
6 price > 10
-- Sorts the categories alphabetically: Electronics, Furniture, Tools
7ORDER BY
8 category;

Output

+-------------+
| category    |
+-------------+
| Electronics |
| Furniture   |
| Tools       |
+-------------+
3 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • ;× 1
  • >× 1

More SQL examples at level 3

← SQL typing practice