Sorting, limiting and removing duplicates
Sort SQL results with ORDER BY (ascending, descending and by several columns), show only the top rows with LIMIT, and list unique values with DISTINCT.
🎯 By the end of this lesson you will be able to:
- Sort results with ORDER BY, ASC and DESC
- Sort by more than one column
- Find the top N rows with LIMIT and unique values with DISTINCT
Outline
What you'll learn and why it matters
“What are our three most expensive products?” “Who joined most recently?” “Which countries do our customers come from?” Questions like these need results in a particular order, cut to a few rows, or without repeats. Three short clauses handle all of them.
Sorting with ORDER BY
Without ORDER BY, a database returns rows in whatever order is convenient for it, which can change. If the order matters, always say so.
SELECT name, price
FROM products
ORDER BY price;Result: 8 rows
| name | price |
|---|---|
| Gel Pens (pack of 5) | 90 |
| Notebook A5 | 120 |
| Coffee Mug | 249 |
| USB-C Cable | 299 |
| SQL Pocket Guide | 350 |
| Water Bottle | 399 |
| Python Basics Book | 450 |
| Desk Lamp | 1200 |
The default is ascending (ASC): smallest first for numbers, A to Z for text, earliest first for dates. Add DESC for descending:
SELECT name, price
FROM products
ORDER BY price DESC;Result: 8 rows
| name | price |
|---|---|
| Desk Lamp | 1200 |
| Python Basics Book | 450 |
| Water Bottle | 399 |
| SQL Pocket Guide | 350 |
| USB-C Cable | 299 |
| Coffee Mug | 249 |
| Notebook A5 | 120 |
| Gel Pens (pack of 5) | 90 |
Sorting by several columns
List several columns, separated by commas. The second column only decides the order within rows that tie on the first.
SELECT name, category, price
FROM products
ORDER BY category, price DESC;Result: 8 rows
| name | category | price |
|---|---|---|
| Python Basics Book | Books | 450 |
| SQL Pocket Guide | Books | 350 |
| Desk Lamp | Electronics | 1200 |
| USB-C Cable | Electronics | 299 |
| Water Bottle | Home | 399 |
| Coffee Mug | Home | 249 |
| Notebook A5 | Stationery | 120 |
| Gel Pens (pack of 5) | Stationery | 90 |
Categories go A to Z; inside each category, the most expensive product comes first. Each column has its own direction: DESC here applies only to price.
Top N with LIMIT
LIMIT keeps only the first few rows of the result. Combined with ORDER BY, it answers every “top N” question:
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;Result: 3 rows
| name | price |
|---|---|
| Desk Lamp | 1200 |
| Python Basics Book | 450 |
| Water Bottle | 399 |
Unique values with DISTINCT
DISTINCT removes duplicate rows from the result. With one column, it lists each different value once:
SELECT DISTINCT country
FROM customers
ORDER BY country;Result: 7 rows
| country |
|---|
| Australia |
| Canada |
| Ghana |
| India |
| Singapore |
| UAE |
| UK |
There are 10 customers but only 7 different countries, because 4 customers are in India.
Where each clause goes
| Order | Clause | Job |
|---|---|---|
| 1 | SELECT (with DISTINCT if needed) | Which columns |
| 2 | FROM | Which table |
| 3 | WHERE | Which rows |
| 4 | ORDER BY | In what order |
| 5 | LIMIT | How many |
You must write the clauses in this order. Putting ORDER BY before WHERE causes a syntax error.
Practice
Guided exercise
Newest customers
Show the names and join dates of the three customers who joined most recently.
- Select
name, joined_onfromcustomers. - Sort by
joined_on DESCso the latest is first. - Add
LIMIT 3.
Show the solution
SELECT name, joined_on
FROM customers
ORDER BY joined_on DESC
LIMIT 3;Result: 3 rows
| name | joined_on |
|---|---|
| Kofi Mensah | 2025-09-14 |
| Emma Brown | 2025-08-25 |
| Arjun Singh | 2025-07-02 |
How to get there: Most recent means the latest date, which is the largest value, so sort descending.
Your turn
List the categories
List each different product category once, in alphabetical order.
Show the solution
SELECT DISTINCT category
FROM products
ORDER BY category;Result: 4 rows
| category |
|---|
| Books |
| Electronics |
| Home |
| Stationery |
How to get there: DISTINCT removes the repeats (each category has two products) and ORDER BY sorts them A to Z.
Common mistakes
Clauses in the wrong order: ORDER BY before WHERE
You see: near "WHERE": syntax error
Fix: Use the order SELECT, FROM, WHERE, ORDER BY, LIMIT.
Expecting DESC to apply to every column
You see: Only the last column is reversed
Fix: Write DESC after each column that should be descending.
Using LIMIT without ORDER BY for a top N
You see: A different 'top 3' on another day or database
Fix: Always add ORDER BY before LIMIT.
Ties at the cut-off
You see: Two items with the same value, but only one shown
Fix: Add a second sort column (such as the name) so the order is always the same, and check the next row.
Recap
ORDER BY columnsorts ascending; addDESCfor descending.- Sort by several columns; later columns break ties in earlier ones.
ORDER BY ... LIMIT ngives the top n rows.SELECT DISTINCTlists each different value once.
Key terms
- Ascending / descending
- Smallest-to-largest (ASC, the default) or largest-to-smallest (DESC).
- DISTINCT
- A keyword that removes duplicate rows from a result.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which query lists the 5 cheapest products?
2. In `ORDER BY country, name`, what does `name` do?
3. What does `SELECT DISTINCT status FROM orders;` return for our shop?
4. Which clause order is correct?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.