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 Compiler

Result:

product_nameprice
Herbal Face Wash199.00
The Psychology of Money299.00
Wings of Fire350.00
Steel Water Bottle 1L549.00
Yoga Mat 6mm799.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 Compiler

Result:

product_nameprice
Samsung 43" Smart TV31990.00
Redmi Note 13 Pro23999.00
Women's Silk Saree8499.00
Cricket Bat (Kashmir Willow)3499.00
Kumkumadi Face Oil2995.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 Compiler

Result:

customer_namecity
Aarav SharmaBengaluru
Divya MenonBengaluru
Karan MehtaMumbai
Meera KulkarniPune

(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 Compiler

Result:

product_namemargin
Samsung 43" Smart TV5990.00
Redmi Note 13 Pro4499.00
Women's Silk Saree3299.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 Compiler

Result:

order_idstatusdelivery_date
1023PendingNULL
1020ShippedNULL
1004CancelledNULL
1022ShippedNULL
1012CancelledNULL
1024PendingNULL

(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 Compiler

Result:

product_nameprice
Running Shoes2499.00
Prestige Pressure Cooker 5L2199.00
Men's Cotton Kurta1899.00
boAt Airdopes 1411299.00
Non-stick Tawa899.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 5 picks one arbitrarily. Add a tie-breaker column (ORDER BY price DESC, product_id) or use RANK.
  • Large OFFSETs. OFFSET 100000 still 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 Compiler

Result:

order_idorder_datestatus
10232026-01-26Pending
10202025-12-03Shipped
10182025-10-28Delivered

(3 rows)

Key takeaways

  • ORDER BY sorts ascending by default; add DESC for descending.
  • Multiple sort columns break ties left to right.
  • LIMIT n returns the first n rows after sorting; OFFSET skips rows.
  • Always pair LIMIT with ORDER BY for 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.

Newsletter

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)