CodeBlanks Logo Icon CodeBlanks

Summary - Ordering and limit

Combine ORDER BY, LIMIT and OFFSET to write a paged query.

Summary - Ordering and limit

In this group you learned ORDER BY, ASC/DESC, LIMIT and OFFSET. Combine them to write a paged sorted query.

SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 5 OFFSET 0;

This returns the 5 most expensive products; first page.

name, price is selected from products. The result is sorted by price in descending order. 0 rows are skipped and the next 5 are returned.

Practice Summary - Ordering and limit

Select the name and price of the 2 most expensive products.

Starter code

query.sql
-- top 2 by price
SELECT name, price
FROM products
ORDER BY

Sample solution

query.sql
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 2

Expected output

name   price
Çanta  25
Defter 12

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which two clauses are used together for pagination?

  • WHERE ve HAVING
  • LIMIT ve OFFSET Correct answer
  • GROUP BY ve ORDER BY
  • SELECT ve FROM

Explanation: LIMIT and OFFSET do pagination.

2 Summary - Ordering and limit: match the terms

Term Meaning
ORDER BY sort
LIMIT limit
OFFSET skip
DESC descending

All lessons in Beginner