Sub topic 7
Why LIMIT Needs ORDER BY
Assume you know basic SELECT, FROM, WHERE, and ORDER BY syntax. This chapter focuses on a key rule: LIMIT controls how many rows you receive, but ORDER BY controls which rows receive those positions.
LIMIT without ORDER BY
LIMIT stops a query after it returns a specified number of rows:
SELECT order_id, order_date, total
FROM orders
LIMIT 5;This query returns five rows, but it does not request any particular five rows. SQL tables do not have an inherent order. The database may read rows through a table scan, an index, or another execution plan. Changes to indexes, statistics, storage, or the database version can change which rows appear first.
The result may look stable during testing. That stability is not a guarantee. Without ORDER BY, the database can return any five matching rows.
LIMIT with ORDER BY
Use ORDER BY when the selected rows must have a meaning:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC
LIMIT 5;This asks for the five rows with the greatest order_date values. The database sorts the result according to the requested rule, then applies the limit.
However, equal sort values create another source of uncertainty. If several orders have the same date, their relative order is unspecified:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC
LIMIT 5;If the fifth position falls inside a group of orders with the same date, different executions may choose different rows from that tied group.
Add a unique or sufficiently specific tie-breaker. Here, order_id identifies each order:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 5;Now the database sorts first by date, newest first. For equal dates, it sorts by ID, highest first. Every row has a definite position, so the top five are deterministic unless the data changes.
How the database applies the clauses
For a simple query, it helps to think of the operations in this order:
ORDER BY does not permanently arrange rows in the table. It arranges the rows in this query's result. LIMIT then keeps the beginning of that requested sequence.
A deterministic query needs sort keys that distinguish every row, or at least every row that could compete for the boundary. A primary key is a common final tie-breaker.
Common mistakes
- Assuming physical storage order means insertion order.
- Using
LIMIT 1to find the newest row withoutORDER BY. - Sorting by a non-unique column and assuming ties have a fixed order.
- Relying on a query plan that happens to return the expected rows today.
For example, this query has a clear meaning:
SELECT order_id, order_date, total
FROM orders
WHERE total > 100
ORDER BY order_date DESC, order_id DESC
LIMIT 1;It returns the newest qualifying order, using the highest order_id to break a date tie.
Exercise
Rewrite this query so it always returns the five lowest-priced products, with a deterministic order: SELECT product_id, price FROM products LIMIT 5;
Show answer
SELECT productid, price FROM products ORDER BY price ASC, productid ASC LIMIT 5;