All posts
SQLSeptember 1, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What does ORDER BY do in SQL?

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.

idnamepricecategory
1Notebook3.50Stationery
2Backpack42.00Bags
3Pen1.20Stationery
4Water Bottle12.75Kitchen
5Desk Lamp24.99Home

Let's sort those products by price, from cheapest to most expensive.

sql
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.

Sorting in descending order with DESC

If you want the highest price first instead, add the keyword DESC (short for "descending") right after the column name.

sql
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.

Sorting by more than one column

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.

sql
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.

What does LIMIT do in SQL?

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.

sql
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.

Combining ORDER BY and LIMIT

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.

sql
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.

sql
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.

Using ORDER BY and LIMIT with WHERE

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.

sql
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.

The order SQL clauses must appear in

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.

ClausePurposeComes after
SELECTChoose which columns to return
FROMChoose which table to readSELECT
WHEREFilter rows before grouping or sortingFROM
GROUP BYCombine rows into groupsWHERE
ORDER BYSort the result rowsGROUP BY
LIMITCap the number of rows returnedORDER 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 guide

Common mistakes to avoid

  • Using LIMIT without ORDER BY when you need specific rows — without a sort, you can't predict which rows you'll get.
  • Forgetting that ORDER BY sorts text alphabetically, not by length or by how it 'looks' — 'Notebook' sorts before 'Pen' because N comes before P.
  • Mixing up ASC and DESC on multi-column sorts — check each column's direction separately, since they don't have to match.
  • Trying to ORDER BY a column that isn't in the SELECT list in older SQL versions — most modern databases allow this, but it's worth checking your database's documentation if you get an error.
  • Assuming LIMIT works the same everywhere — some databases (like SQL Server) use TOP instead of LIMIT, and Oracle traditionally used ROWNUM, though newer versions support FETCH FIRST.

Quick recap

  1. ORDER BY sorts your results by one or more columns; add DESC for highest-to-lowest, or leave it off for the default ascending order.
  2. LIMIT caps how many rows come back, and should almost always be paired with ORDER BY so the results are predictable.
  3. OFFSET lets you skip rows, which is useful for showing different 'pages' of results.
  4. Clause order matters: SELECT, FROM, WHERE, GROUP BY, ORDER BY, then LIMIT.

Frequently asked questions

What is the difference between ORDER BY and WHERE in SQL?+

WHERE 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.

Does LIMIT work the same in every database?+

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.

Can I sort by a column that isn't in my SELECT list?+

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.

What happens if I use LIMIT without ORDER BY?+

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.

How do I get the second-highest value with SQL?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.