Skip to content

Beginner · Databases and SQL basics · topic 18 of 54 · 20 min

ORDER BY, LIMIT and DISTINCT

Explanation

ORDER BY sorts the rows a query returns, LIMIT keeps only the first few, and OFFSET skips some, which is how apps show page 1, page 2 and so on. DISTINCT removes repeated rows and AS gives a column a nicer name.

Rows have no order until you ask

A database does not promise any order for the rows it returns. If you need "cheapest first" or "newest first", you must say so with ORDER BY. ASC means ascending (small to big, A to Z) and is the default. DESC means descending (big to small, Z to A).

sql
SELECT name, price FROM products ORDER BY price DESC;
The most expensive product comes first.

Sorting by more than one column

You can sort by several columns. The database sorts by the first one, and only when two rows are equal there does it look at the second one.

sql
SELECT name, category, price
FROM products
ORDER BY category ASC, price DESC;
Grouped by category A to Z; inside each category, the costliest first.

LIMIT and OFFSET: pages of results

LIMIT n keeps only the first n rows. OFFSET m skips the first m rows. Together they give pages: with 10 items per page, page 1 is LIMIT 10 OFFSET 0, page 2 is LIMIT 10 OFFSET 10, and page p is LIMIT 10 OFFSET (p - 1) * 10.

Think of it like this: Think of a long merit list on a college notice board, sorted by marks. "Top 10 students" is ORDER BY marks DESC LIMIT 10. "Ranks 11 to 20" is the same list with OFFSET 10: you skip the first ten names and read the next ten.
sql
SELECT name, price FROM products
ORDER BY price ASC
LIMIT 3 OFFSET 3;
Page 2 of a cheapest-first list, 3 products per page.

DISTINCT and AS

DISTINCT removes repeated rows from the result. SELECT DISTINCT category FROM products lists each category once, even if fifty products share it. AS gives a column a new name in the result, called an alias, which makes the output easier to read.

sql
SELECT DISTINCT category FROM products ORDER BY category;

SELECT name AS product, price AS price_in_rupees FROM products;
Each category once; and columns with friendlier names.
Tip: Always use ORDER BY together with LIMIT. Without ORDER BY, "the first 10 rows" can be any 10 rows, and they may change from one run to the next.
Careful: The order of the parts matters: SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ... OFFSET. Writing LIMIT before ORDER BY or WHERE after ORDER BY is a syntax error.

Watch the lesson

ORDER BY, LIMIT and DISTINCT · 1/6

ORDER BY, LIMIT, DISTINCT

Today we sort results, cut them into pages, and remove repeats.

Prefer a video by a teacher? Find free videos on “sql order by limit offset distinct tutorial” on YouTube ↗

In real life: Search results in a shopping app

When you search "pressure cooker" in a shopping app and pick "Price: low to high", the backend runs a query with WHERE name LIKE '%cooker%' ORDER BY price ASC LIMIT 20 OFFSET 0. When you scroll down and the next batch loads, the app asks for OFFSET 20, then OFFSET 40. The phone never downloads all 5,000 cookers at once, only one page at a time.

Practice (0/3 done)

Task 1 · Code · SQL

The products table has id, name, category, price and stock. Show the name and price of every product, the most expensive first.

Task 2 · Code · SQL

Show name, category and price of every product, sorted by category from A to Z, and inside each category by price from highest to lowest.

Task 3 · Code · SQL

The app lists products cheapest first, 3 per page. Show PAGE 2. Return two columns: the name renamed to product (use AS), and price.

Quick quiz

Question 1

1.What does ORDER BY marks DESC do?

Question 2

2.With 20 items per page, which clause gives page 3?

Question 3

3.What does SELECT DISTINCT city FROM customers return?

Question 4

4.Why use ORDER BY together with LIMIT 10?

Question 5

5.In SELECT price AS amount FROM products, what is amount?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: SELECT and WHERE: finding rows