SQL ORDER BY and LIMIT Explained for Beginners Guide
SQL ORDER BY and LIMIT explained for beginners: learn to sort query results ascending or descending, then limit how many rows come back, with examples.
SQL ORDER BY and LIMIT explained for beginners: learn to sort query results ascending or descending, then limit how many rows come back, with examples.
Short answer: SQL ORDER BY sorts the rows a query returns, and SQL LIMIT caps how many of those rows you actually get back. Put them together and you can answer questions like "what are my 5 newest customers?" or "which product is the cheapest?" in one simple query. This guide shows you exactly how, with small examples you can copy, paste, and run yourself.
This post assumes you already know the basics of SELECT and WHERE. If you don't, start with our guide on SQL SELECT and WHERE first, then come back here.
When you run a SELECT query, the database does not promise any particular order for the rows it returns. It might come back in the order the rows were inserted, or in some order the database engine finds efficient. If you want the rows sorted in a specific way — by price, by name, by date — you add an ORDER BY clause.
Here is a table called products that we will use throughout this guide.
| id | name | price | category |
|---|---|---|---|
| 1 | Notebook | 3.50 | Stationery |
| 2 | Backpack | 42.00 | Bags |
| 3 | Pen | 1.20 | Stationery |
| 4 | Water Bottle | 12.75 | Kitchen |
| 5 | Desk Lamp | 24.99 | Home |
Let's sort those products by price, from cheapest to most expensive.
SELECT name, price
FROM products
ORDER BY price;Line by line: SELECT name, price picks the two columns we want to see. FROM products says which table to read from. ORDER BY price tells the database to sort the results by the price column before returning them. By default, ORDER BY sorts in ascending order — smallest number first, or earliest letter first for text.
If you want the highest price first instead, add the keyword DESC (short for "descending") right after the column name.
SELECT name, price
FROM products
ORDER BY price DESC;This is the same query as before, but DESC flips the sort order so the most expensive product appears first. If you want to be explicit about ascending order, you can write ASC, but it's optional since ascending is already the default.
Sometimes one column isn't enough to fully sort your data. For example, you might want products sorted by category first, and then by price within each category. You do this by listing multiple columns, separated by commas.
SELECT name, category, price
FROM products
ORDER BY category ASC, price DESC;The database sorts by category first (alphabetically, A to Z). Then, for any rows that share the same category, it sorts those rows by price, from highest to lowest. Each column in an ORDER BY list can have its own direction — here category uses ASC and price uses DESC.
LIMIT restricts how many rows a query returns. If your table has a million rows but you only want to see 10, LIMIT stops the database from sending back the rest.
SELECT name, price
FROM products
LIMIT 3;This query returns only 3 rows from the products table. On its own, LIMIT doesn't say which 3 rows you get — that depends on whatever order the database happens to return rows in, which is not guaranteed. That's why LIMIT is almost always used together with ORDER BY.
This is where the real power shows up. By sorting first and then limiting, you can answer very specific, useful questions. Let's find the 3 cheapest products.
SELECT name, price
FROM products
ORDER BY price ASC
LIMIT 3;Here's what happens, step by step: the database looks at the products table, sorts every row by price from lowest to highest, and then keeps only the first 3 rows of that sorted list. Change ASC to DESC and you'd get the 3 most expensive products instead.
You can also skip rows before limiting, using OFFSET. This is handy for pagination — showing page 2, page 3, and so on.
SELECT name, price
FROM products
ORDER BY price ASC
LIMIT 3 OFFSET 3;OFFSET 3 tells the database to skip the first 3 sorted rows, then LIMIT 3 grabs the next 3. If LIMIT 3 was page 1, this query gives you page 2 of the same sorted list.
You can combine ORDER BY and LIMIT with a WHERE clause to filter rows before sorting them. Let's find the cheapest item in the Stationery category only.
SELECT name, price
FROM products
WHERE category = 'Stationery'
ORDER BY price ASC
LIMIT 1;WHERE category = 'Stationery' keeps only rows where the category column equals 'Stationery'. ORDER BY price ASC sorts what's left by price, lowest first. LIMIT 1 keeps just the top row — the single cheapest stationery item.
SQL is picky about the order you write your clauses in, even though the database doesn't run them in that exact order internally. Here's the sequence to follow when you use them together.
| Clause | Purpose | Comes after |
|---|---|---|
| SELECT | Choose which columns to return | — |
| FROM | Choose which table to read | SELECT |
| WHERE | Filter rows before grouping or sorting | FROM |
| GROUP BY | Combine rows into groups | WHERE |
| ORDER BY | Sort the result rows | GROUP BY |
| LIMIT | Cap the number of rows returned | ORDER BY |
Writing LIMIT before ORDER BY, or WHERE after ORDER BY, will cause a syntax error in most databases. If you've already read our guide to SQL GROUP BY, this table shows exactly where ORDER BY and LIMIT slot in after it.
Not sure how GROUP BY fits into a full query yet? Read our beginner-friendly walkthrough first.
Read the SQL GROUP BY guideWHERE filters which rows are included in the result, removing rows that don't match a condition. ORDER BY doesn't remove any rows — it just changes the order the remaining rows are returned in. You often use both together: WHERE to narrow down the data, then ORDER BY to sort what's left.
No. LIMIT is supported by MySQL, PostgreSQL, and SQLite, but SQL Server uses TOP instead, for example SELECT TOP 3 * FROM products. Oracle and SQL Server both also support the newer FETCH FIRST 3 ROWS ONLY syntax, which works across more databases. Always check your specific database's documentation if a query doesn't run as expected.
In most databases, yes — you can ORDER BY any column from the table, even if you didn't select it. For example, SELECT name FROM products ORDER BY price still works and sorts by price, even though price isn't shown in the output.
The query still runs and returns the number of rows you asked for, but there's no guarantee which rows they'll be. The database can return rows in any internal order it chooses, and that order can even change between runs. Always add ORDER BY if you need consistent, predictable results.
Sort in descending order and skip the first row with OFFSET, then limit to 1 row: ORDER BY price DESC LIMIT 1 OFFSET 1. This sorts prices highest to lowest, skips the top result, and returns the next one — the second-highest price.
Want more hands-on SQL practice, straight from your browser?
Get in touch with FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
What is an index in SQL? A plain-English guide with copy-paste examples showing how indexes speed up queries and when you actually need one.
Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.