All 40 lessons
Lesson 8 of 40 · SQL Basics
SQL ORDER BY and LIMIT
By Skillancy Editorial Team
Every example runs on the ShopKart dataset, already loaded in the free SQL Compiler. Click Try it in the SQL Compiler under a query to open it there, then press Run.
ORDER BY sorts your results, and LIMIT caps how many rows come back. Together they answer every "top 5" or "latest 10" question.
Syntax
SELECT columns
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC]
LIMIT n OFFSET m;Examples
1. Sort ascending (the default)
Cheapest products first:
SELECT product_name, price
FROM products
ORDER BY price
LIMIT 5;Try it in the SQL CompilerResult:
| product_name | price |
|---|---|
| Herbal Face Wash | 199.00 |
| The Psychology of Money | 299.00 |
| Wings of Fire | 350.00 |
| Steel Water Bottle 1L | 549.00 |
| Yoga Mat 6mm | 799.00 |
(5 rows)
2. Sort descending
Most expensive products first:
SELECT product_name, price
FROM products
ORDER BY price DESC
LIMIT 5;Try it in the SQL CompilerResult:
| product_name | price |
|---|---|
| Samsung 43" Smart TV | 31990.00 |
| Redmi Note 13 Pro | 23999.00 |
| Women's Silk Saree | 8499.00 |
| Cricket Bat (Kashmir Willow) | 3499.00 |
| Kumkumadi Face Oil | 2995.00 |
(5 rows)
That's a "top 5 products by price" query, the classic top-N pattern.
3. Sort by multiple columns
Sort by city, then by name within each city:
SELECT customer_name, city
FROM customers
WHERE city IN ('Bengaluru', 'Mumbai', 'Pune')
ORDER BY city, customer_name;Try it in the SQL CompilerResult:
| customer_name | city |
|---|---|
| Aarav Sharma | Bengaluru |
| Divya Menon | Bengaluru |
| Karan Mehta | Mumbai |
| Meera Kulkarni | Pune |
(4 rows)
The second column only breaks ties in the first.
4. Sort by a calculated column or alias
Unlike WHERE, ORDER BY runs after SELECT, so aliases work:
SELECT product_name,
price - cost_price AS margin
FROM products
ORDER BY margin DESC
LIMIT 3;Try it in the SQL CompilerResult:
| product_name | margin |
|---|---|
| Samsung 43" Smart TV | 5990.00 |
| Redmi Note 13 Pro | 4499.00 |
| Women's Silk Saree | 3299.00 |
(3 rows)
5. Control where NULLs go
Undelivered orders have a NULL delivery_date. Put them first to see what's pending:
SELECT order_id, status, delivery_date
FROM orders
ORDER BY delivery_date DESC NULLS FIRST
LIMIT 6;Try it in the SQL CompilerResult:
| order_id | status | delivery_date |
|---|---|---|
| 1023 | Pending | NULL |
| 1020 | Shipped | NULL |
| 1004 | Cancelled | NULL |
| 1022 | Shipped | NULL |
| 1012 | Cancelled | NULL |
| 1024 | Pending | NULL |
(6 rows)
6. Pagination with OFFSET
Show products 6 to 10 by price (page 2 with 5 per page):
SELECT product_name, price
FROM products
ORDER BY price DESC
LIMIT 5 OFFSET 5;Try it in the SQL CompilerResult:
| product_name | price |
|---|---|
| Running Shoes | 2499.00 |
| Prestige Pressure Cooker 5L | 2199.00 |
| Men's Cotton Kurta | 1899.00 |
| boAt Airdopes 141 | 1299.00 |
| Non-stick Tawa | 899.00 |
(5 rows)
Page p with n rows per page uses LIMIT n OFFSET (p - 1) * n.
Common mistakes
- Using LIMIT without ORDER BY. "Top 5" means nothing without a sort; the database can return any 5 rows.
- Ties at the cut-off. If the 5th and 6th rows have the same value,
LIMIT 5picks one arbitrarily. Add a tie-breaker column (ORDER BY price DESC, product_id) or use RANK. - Large OFFSETs.
OFFSET 100000still reads and throws away 100,000 rows. For deep pagination, filter on the last seen value instead.
Practice question
Show the 3 most recent orders paid by UPI.
Show solution
SELECT order_id, order_date, status
FROM orders
WHERE payment_method = 'UPI'
ORDER BY order_date DESC
LIMIT 3;Try it in the SQL CompilerResult:
| order_id | order_date | status |
|---|---|---|
| 1023 | 2026-01-26 | Pending |
| 1020 | 2025-12-03 | Shipped |
| 1018 | 2025-10-28 | Delivered |
(3 rows)
Key takeaways
ORDER BYsorts ascending by default; addDESCfor descending.- Multiple sort columns break ties left to right.
LIMIT nreturns the first n rows after sorting;OFFSETskips rows.- Always pair
LIMITwithORDER BYfor a predictable result.
Frequently asked questions
What is the default sort order in SQL?
ORDER BY sorts in ascending order (ASC) by default: smallest to largest, A to Z, oldest to newest.
What is the SQL Server version of LIMIT?
SQL Server uses SELECT TOP 5 ... or OFFSET ... FETCH NEXT 5 ROWS ONLY. Oracle uses FETCH FIRST 5 ROWS ONLY. PostgreSQL and MySQL use LIMIT.
Where do NULLs appear when sorting?
In PostgreSQL, NULLs sort last in ascending order and first in descending order. You can override this with NULLS FIRST or NULLS LAST.
Keep learning SQL
One email a week with SQL and analytics guides, new practice questions and cohort dates. No spam, unsubscribe anytime.
Prefer a one-page summary? Download the free SQL cheat sheet (PDF)