6. ORDER BY and LIMIT
Put the rows in the order you want, and take only the first few: top lists and pages.
Without ORDER BY, there is no promised order
So far, the rows have come out in the order of id, the order they were put in:
SELECT name, city FROM customer;| name | city |
|---|---|
| Somchai Jaidee | Bangkok |
| Malee Srisuk | Chiang Mai |
| Anan Wongsa | Bangkok |
| Pim Chaiya | Phuket |
| Narin Thongdee | Khon Kaen |
| Kanya Boonmee | Chiang Mai |
| Wichai Saelim | Bangkok |
| Siriporn Kaewdee | Phuket |
| Tawan Rungruang | Khon Kaen |
| Lalita Meesuk | Bangkok |
That is a habit of the database, not a promise. A table is a heap of rows with no order of its own. The database hands them over in whatever order is quickest for it, and that order may change when the data grows, when an index is added, or in another database program. If the order matters, say so with ORDER BY.
Never rely on the order of rows from a SELECT without ORDER BY, however steady it looks. It can change one day without a word.
ORDER BY: small to large, or large to small
ORDER BY comes after FROM (and after WHERE, if there is one), followed by the column to sort by. Products from cheapest to dearest:
SELECT name, price FROM product ORDER BY price;| name | price |
|---|---|
| Pen | 15.0 |
| Potato Chips | 25.0 |
| Highlighter | 25.0 |
| Notebook | 30.0 |
| Croissant | 35.0 |
| Orange Juice | 40.0 |
| Green Tea | 45.0 |
| Coffee | 55.0 |
| Banana Bread | 60.0 |
| Dried Mango | 90.0 |
| Cheesecake | 120.0 |
| Cashew Nuts | 150.0 |
That is ascending order (ASC, small to large), the default, so ASC needn't be written. For descending (large to small), add DESC:
SELECT name, price FROM product ORDER BY price DESC;| name | price |
|---|---|
| Cashew Nuts | 150.0 |
| Cheesecake | 120.0 |
| Dried Mango | 90.0 |
| Banana Bread | 60.0 |
| Coffee | 55.0 |
| Green Tea | 45.0 |
| Orange Juice | 40.0 |
| Croissant | 35.0 |
| Notebook | 30.0 |
| Potato Chips | 25.0 |
| Highlighter | 25.0 |
| Pen | 15.0 |
Text sorts in alphabetical order, and dates in YYYY-MM-DD form sort from old to new. Customers by name, and then the newest members first:
SELECT name, city FROM customer ORDER BY name;| name | city |
|---|---|
| Anan Wongsa | Bangkok |
| Kanya Boonmee | Chiang Mai |
| Lalita Meesuk | Bangkok |
| Malee Srisuk | Chiang Mai |
| Narin Thongdee | Khon Kaen |
| Pim Chaiya | Phuket |
| Siriporn Kaewdee | Phuket |
| Somchai Jaidee | Bangkok |
| Tawan Rungruang | Khon Kaen |
| Wichai Saelim | Bangkok |
SELECT name, joined FROM customer ORDER BY joined DESC;| name | joined |
|---|---|
| Lalita Meesuk | 2026-08-03 |
| Tawan Rungruang | 2026-05-21 |
| Siriporn Kaewdee | 2026-02-14 |
| Wichai Saelim | 2026-01-09 |
| Kanya Boonmee | 2025-10-05 |
| Narin Thongdee | 2025-08-30 |
| Pim Chaiya | 2025-06-11 |
| Anan Wongsa | 2025-04-20 |
| Malee Srisuk | 2025-03-02 |
| Somchai Jaidee | 2025-01-15 |
With WHERE, the order of the parts is SELECT ... FROM ... WHERE ... ORDER BY: first the rows are chosen, then sorted. Products in stock, those with the fewest left first:
SELECT name, price, stock FROM product WHERE stock > 0 ORDER BY stock;| name | price | stock |
|---|---|---|
| Cheesecake | 120.0 | 8 |
| Banana Bread | 60.0 | 12 |
| Cashew Nuts | 150.0 | 20 |
| Croissant | 35.0 | 30 |
| Dried Mango | 90.0 | 45 |
| Notebook | 30.0 | 75 |
| Green Tea | 45.0 | 80 |
| Coffee | 55.0 | 120 |
| Potato Chips | 25.0 | 200 |
| Pen | 15.0 | 300 |
Sorting by several columns
Several customers live in the same city. To sort by city, and within each city by joining date, newest first, list the columns separated by commas. Each column gets its own ASC or DESC:
SELECT name, city, joined FROM customer ORDER BY city, joined DESC;| name | city | joined |
|---|---|---|
| Lalita Meesuk | Bangkok | 2026-08-03 |
| Wichai Saelim | Bangkok | 2026-01-09 |
| Anan Wongsa | Bangkok | 2025-04-20 |
| Somchai Jaidee | Bangkok | 2025-01-15 |
| Kanya Boonmee | Chiang Mai | 2025-10-05 |
| Malee Srisuk | Chiang Mai | 2025-03-02 |
| Tawan Rungruang | Khon Kaen | 2026-05-21 |
| Narin Thongdee | Khon Kaen | 2025-08-30 |
| Siriporn Kaewdee | Phuket | 2026-02-14 |
| Pim Chaiya | Phuket | 2025-06-11 |
The second column only decides between rows whose first column is the same: the 4 Bangkok customers come first (B is first in the alphabet), and among them, the newest member, Lalita, leads.
Products by category, and the dearest first within each category:
SELECT name, category_id, price FROM product ORDER BY category_id, price DESC;| name | category_id | price |
|---|---|---|
| Coffee | 1 | 55.0 |
| Green Tea | 1 | 45.0 |
| Orange Juice | 1 | 40.0 |
| Cheesecake | 2 | 120.0 |
| Banana Bread | 2 | 60.0 |
| Croissant | 2 | 35.0 |
| Cashew Nuts | 3 | 150.0 |
| Dried Mango | 3 | 90.0 |
| Potato Chips | 3 | 25.0 |
| Notebook | 4 | 30.0 |
| Highlighter | 4 | 25.0 |
| Pen | 4 | 15.0 |
One thing to know about sorting text: capital letters come before small ones. 'Zebra' sorts before 'apple', because in the computer's table of characters every capital comes before every small letter. The shop's names all start with a capital, so it doesn't show here, but it will in your own data:
SELECT 'apple' < 'Banana', 'Zebra' < 'apple';| 'apple' < 'Banana' | 'Zebra' < 'apple' |
|---|---|
| 0 | 1 |
Thai text has its own story of order, told in chapter 33.
Sorting by a calculation, or by a name you give it
ORDER BY may sort by a calculation, not only a column. The value of each product's stock (price times quantity), the largest first:
SELECT name, price, stock, price * stock FROM product ORDER BY price * stock DESC;| name | price | stock | price * stock |
|---|---|---|---|
| Coffee | 55.0 | 120 | 6600.0 |
| Potato Chips | 25.0 | 200 | 5000.0 |
| Pen | 15.0 | 300 | 4500.0 |
| Dried Mango | 90.0 | 45 | 4050.0 |
| Green Tea | 45.0 | 80 | 3600.0 |
| Cashew Nuts | 150.0 | 20 | 3000.0 |
| Notebook | 30.0 | 75 | 2250.0 |
| Croissant | 35.0 | 30 | 1050.0 |
| Cheesecake | 120.0 | 8 | 960.0 |
| Banana Bread | 60.0 | 12 | 720.0 |
| Orange Juice | 40.0 | 0 | 0.0 |
| Highlighter | 25.0 | 0 | 0.0 |
Writing the calculation twice is tiresome. Give it a name with AS after SELECT, and sort by that name (chapter 7 tells more about AS):
SELECT name, price * stock AS stock_value FROM product ORDER BY stock_value DESC;| name | stock_value |
|---|---|
| Coffee | 6600.0 |
| Potato Chips | 5000.0 |
| Pen | 4500.0 |
| Dried Mango | 4050.0 |
| Green Tea | 3600.0 |
| Cashew Nuts | 3000.0 |
| Notebook | 2250.0 |
| Croissant | 1050.0 |
| Cheesecake | 960.0 |
| Banana Bread | 720.0 |
| Orange Juice | 0.0 |
| Highlighter | 0.0 |
You may also sort by the position of a column in the SELECT list: ORDER BY 2 DESC, 1 is "by the second column, large to small, then by the first". It is short, but it breaks quietly when someone changes the list, so names are safer:
SELECT name, price FROM product ORDER BY 2 DESC, 1;| name | price |
|---|---|
| Cashew Nuts | 150.0 |
| Cheesecake | 120.0 |
| Dried Mango | 90.0 |
| Banana Bread | 60.0 |
| Coffee | 55.0 |
| Green Tea | 45.0 |
| Orange Juice | 40.0 |
| Croissant | 35.0 |
| Notebook | 30.0 |
| Highlighter | 25.0 |
| Potato Chips | 25.0 |
| Pen | 15.0 |
Where NULL goes: NULLS FIRST and NULLS LAST
In SQLite and Phai SQL, NULL counts as smaller than any value, so it comes first in ascending order and last in descending. The 2 customers without a phone lead the list:
SELECT name, phone FROM customer ORDER BY phone;| name | phone |
|---|---|
| Anan Wongsa | NULL |
| Siriporn Kaewdee | NULL |
| Somchai Jaidee | 081-234-5678 |
| Narin Thongdee | 082-777-8888 |
| Wichai Saelim | 083-999-0000 |
| Kanya Boonmee | 084-246-1357 |
| Tawan Rungruang | 085-123-4567 |
| Pim Chaiya | 086-555-0101 |
| Lalita Meesuk | 087-654-3210 |
| Malee Srisuk | 089-111-2222 |
NULLS LAST and NULLS FIRST after the column say where NULL goes, whatever the direction:
SELECT name, phone FROM customer ORDER BY phone NULLS LAST;| name | phone |
|---|---|
| Somchai Jaidee | 081-234-5678 |
| Narin Thongdee | 082-777-8888 |
| Wichai Saelim | 083-999-0000 |
| Kanya Boonmee | 084-246-1357 |
| Tawan Rungruang | 085-123-4567 |
| Pim Chaiya | 086-555-0101 |
| Lalita Meesuk | 087-654-3210 |
| Malee Srisuk | 089-111-2222 |
| Anan Wongsa | NULL |
| Siriporn Kaewdee | NULL |
SELECT name, phone FROM customer ORDER BY phone DESC NULLS FIRST;| name | phone |
|---|---|
| Anan Wongsa | NULL |
| Siriporn Kaewdee | NULL |
| Malee Srisuk | 089-111-2222 |
| Lalita Meesuk | 087-654-3210 |
| Pim Chaiya | 086-555-0101 |
| Tawan Rungruang | 085-123-4567 |
| Kanya Boonmee | 084-246-1357 |
| Wichai Saelim | 083-999-0000 |
| Narin Thongdee | 082-777-8888 |
| Somchai Jaidee | 081-234-5678 |
Not every database puts NULL in the same place when you don't say: some put it last in ascending order. Writing NULLS FIRST or NULLS LAST makes the order the same everywhere.
LIMIT and OFFSET: the first few rows, and pages
LIMIT n keeps only the first n rows of the result. With ORDER BY, that makes a top list: the 3 dearest products:
SELECT name, price FROM product ORDER BY price DESC LIMIT 3;| name | price |
|---|---|
| Cashew Nuts | 150.0 |
| Cheesecake | 120.0 |
| Dried Mango | 90.0 |
LIMIT cuts after sorting, so "the 3 dearest" really is the 3 dearest. Without ORDER BY, LIMIT 3 gives "some 3 rows", whichever came first.
Ties. Two products cost 25 baht. The 2 cheapest products:
SELECT name, price FROM product ORDER BY price LIMIT 2;| name | price |
|---|---|
| Pen | 15.0 |
| Potato Chips | 25.0 |
Pen is certain, but which of the two 25-baht products comes second is not promised: by price alone, they are equal. Add a column that settles ties, such as the name, and the answer is always the same:
SELECT name, price FROM product ORDER BY price, name LIMIT 2;| name | price |
|---|---|
| Pen | 15.0 |
| Highlighter | 25.0 |
OFFSET m skips the first m rows. Together they make pages: a website showing 4 products a page shows page 1 with LIMIT 4, page 2 with LIMIT 4 OFFSET 4, page 3 with LIMIT 4 OFFSET 8. In general, page p is OFFSET (p - 1) * 4:
SELECT id, name FROM product ORDER BY id LIMIT 4;| id | name |
|---|---|
| 1 | Coffee |
| 2 | Green Tea |
| 3 | Orange Juice |
| 4 | Croissant |
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 4;| id | name |
|---|---|
| 5 | Banana Bread |
| 6 | Cheesecake |
| 7 | Potato Chips |
| 8 | Dried Mango |
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 8;| id | name |
|---|---|
| 9 | Cashew Nuts |
| 10 | Notebook |
| 11 | Pen |
| 12 | Highlighter |
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 12;| id | name |
|---|
The shop has 12 products, so page 4 is empty: that is how a program knows the last page has passed.
Pages need an ORDER BY that settles every tie (here id, which never repeats). Without it, a row can show up on two pages, or on none.
- Show the 5 cheapest products.
- Show the customers sorted by city, and within each city by name.
- Show the 3 most recent orders.
- Sort the products by name and show the second page, 4 products a page.
Show the answer
SELECT name, price FROM product ORDER BY price, name LIMIT 5;
SELECT name, city FROM customer ORDER BY city, name;
SELECT id, customer_id, ordered FROM orders ORDER BY ordered DESC LIMIT 3;
SELECT name FROM product ORDER BY name LIMIT 4 OFFSET 4;| name | price |
|---|---|
| Pen | 15.0 |
| Highlighter | 25.0 |
| Potato Chips | 25.0 |
| Notebook | 30.0 |
| Croissant | 35.0 |
| name | city |
|---|---|
| Anan Wongsa | Bangkok |
| Lalita Meesuk | Bangkok |
| Somchai Jaidee | Bangkok |
| Wichai Saelim | Bangkok |
| Kanya Boonmee | Chiang Mai |
| Malee Srisuk | Chiang Mai |
| Narin Thongdee | Khon Kaen |
| Tawan Rungruang | Khon Kaen |
| Pim Chaiya | Phuket |
| Siriporn Kaewdee | Phuket |
| id | customer_id | ordered |
|---|---|---|
| 15 | 7 | 2026-11-25 |
| 14 | 3 | 2026-11-18 |
| 13 | 6 | 2026-11-11 |
| name |
|---|
| Croissant |
| Dried Mango |
| Green Tea |
| Highlighter |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made