Nishaglobal Education logo

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.

Lesson 4 of 1220 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Sorting with ORDER BY
  3. Sorting by several columns
  4. Top N with LIMIT
  5. Unique values with DISTINCT
  6. Where each clause goes
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

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.

SQL
SELECT name, price
FROM products
ORDER BY price;

Result: 8 rows

Query result
nameprice
Gel Pens (pack of 5)90
Notebook A5120
Coffee Mug249
USB-C Cable299
SQL Pocket Guide350
Water Bottle399
Python Basics Book450
Desk Lamp1200

The default is ascending (ASC): smallest first for numbers, A to Z for text, earliest first for dates. Add DESC for descending:

SQL
SELECT name, price
FROM products
ORDER BY price DESC;

Result: 8 rows

Query result
nameprice
Desk Lamp1200
Python Basics Book450
Water Bottle399
SQL Pocket Guide350
USB-C Cable299
Coffee Mug249
Notebook A5120
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.

SQL
SELECT name, category, price
FROM products
ORDER BY category, price DESC;

Result: 8 rows

Query result
namecategoryprice
Python Basics BookBooks450
SQL Pocket GuideBooks350
Desk LampElectronics1200
USB-C CableElectronics299
Water BottleHome399
Coffee MugHome249
Notebook A5Stationery120
Gel Pens (pack of 5)Stationery90

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:

SQL
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;

Result: 3 rows

Query result
nameprice
Desk Lamp1200
Python Basics Book450
Water Bottle399

Unique values with DISTINCT

DISTINCT removes duplicate rows from the result. With one column, it lists each different value once:

SQL
SELECT DISTINCT country
FROM customers
ORDER BY country;

Result: 7 rows

Query result
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

OrderClauseJob
1SELECT (with DISTINCT if needed)Which columns
2FROMWhich table
3WHEREWhich rows
4ORDER BYIn what order
5LIMITHow 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.

  1. Select name, joined_on from customers.
  2. Sort by joined_on DESC so the latest is first.
  3. Add LIMIT 3.
Show the solution
Solution
SELECT name, joined_on
FROM customers
ORDER BY joined_on DESC
LIMIT 3;

Result: 3 rows

Query result
namejoined_on
Kofi Mensah2025-09-14
Emma Brown2025-08-25
Arjun Singh2025-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
Solution
SELECT DISTINCT category
FROM products
ORDER BY category;

Result: 4 rows

Query result
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 column sorts ascending; add DESC for descending.
  • Sort by several columns; later columns break ties in earlier ones.
  • ORDER BY ... LIMIT n gives the top n rows.
  • SELECT DISTINCT lists 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. 1. Which query lists the 5 cheapest products?

  2. 2. In `ORDER BY country, name`, what does `name` do?

  3. 3. What does `SELECT DISTINCT status FROM orders;` return for our shop?

  4. 4. Which clause order is correct?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.