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;
namecity
Somchai JaideeBangkok
Malee SrisukChiang Mai
Anan WongsaBangkok
Pim ChaiyaPhuket
Narin ThongdeeKhon Kaen
Kanya BoonmeeChiang Mai
Wichai SaelimBangkok
Siriporn KaewdeePhuket
Tawan RungruangKhon Kaen
Lalita MeesukBangkok
10 rows

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;
nameprice
Pen15.0
Potato Chips25.0
Highlighter25.0
Notebook30.0
Croissant35.0
Orange Juice40.0
Green Tea45.0
Coffee55.0
Banana Bread60.0
Dried Mango90.0
Cheesecake120.0
Cashew Nuts150.0
12 rows

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;
nameprice
Cashew Nuts150.0
Cheesecake120.0
Dried Mango90.0
Banana Bread60.0
Coffee55.0
Green Tea45.0
Orange Juice40.0
Croissant35.0
Notebook30.0
Potato Chips25.0
Highlighter25.0
Pen15.0
12 rows

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;
namecity
Anan WongsaBangkok
Kanya BoonmeeChiang Mai
Lalita MeesukBangkok
Malee SrisukChiang Mai
Narin ThongdeeKhon Kaen
Pim ChaiyaPhuket
Siriporn KaewdeePhuket
Somchai JaideeBangkok
Tawan RungruangKhon Kaen
Wichai SaelimBangkok
10 rows
SELECT name, joined FROM customer ORDER BY joined DESC;
namejoined
Lalita Meesuk2026-08-03
Tawan Rungruang2026-05-21
Siriporn Kaewdee2026-02-14
Wichai Saelim2026-01-09
Kanya Boonmee2025-10-05
Narin Thongdee2025-08-30
Pim Chaiya2025-06-11
Anan Wongsa2025-04-20
Malee Srisuk2025-03-02
Somchai Jaidee2025-01-15
10 rows

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;
namepricestock
Cheesecake120.08
Banana Bread60.012
Cashew Nuts150.020
Croissant35.030
Dried Mango90.045
Notebook30.075
Green Tea45.080
Coffee55.0120
Potato Chips25.0200
Pen15.0300
10 rows

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;
namecityjoined
Lalita MeesukBangkok2026-08-03
Wichai SaelimBangkok2026-01-09
Anan WongsaBangkok2025-04-20
Somchai JaideeBangkok2025-01-15
Kanya BoonmeeChiang Mai2025-10-05
Malee SrisukChiang Mai2025-03-02
Tawan RungruangKhon Kaen2026-05-21
Narin ThongdeeKhon Kaen2025-08-30
Siriporn KaewdeePhuket2026-02-14
Pim ChaiyaPhuket2025-06-11
10 rows

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;
namecategory_idprice
Coffee155.0
Green Tea145.0
Orange Juice140.0
Cheesecake2120.0
Banana Bread260.0
Croissant235.0
Cashew Nuts3150.0
Dried Mango390.0
Potato Chips325.0
Notebook430.0
Highlighter425.0
Pen415.0
12 rows

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'
01
1 row

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;
namepricestockprice * stock
Coffee55.01206600.0
Potato Chips25.02005000.0
Pen15.03004500.0
Dried Mango90.0454050.0
Green Tea45.0803600.0
Cashew Nuts150.0203000.0
Notebook30.0752250.0
Croissant35.0301050.0
Cheesecake120.08960.0
Banana Bread60.012720.0
Orange Juice40.000.0
Highlighter25.000.0
12 rows

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;
namestock_value
Coffee6600.0
Potato Chips5000.0
Pen4500.0
Dried Mango4050.0
Green Tea3600.0
Cashew Nuts3000.0
Notebook2250.0
Croissant1050.0
Cheesecake960.0
Banana Bread720.0
Orange Juice0.0
Highlighter0.0
12 rows

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;
nameprice
Cashew Nuts150.0
Cheesecake120.0
Dried Mango90.0
Banana Bread60.0
Coffee55.0
Green Tea45.0
Orange Juice40.0
Croissant35.0
Notebook30.0
Highlighter25.0
Potato Chips25.0
Pen15.0
12 rows

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;
namephone
Anan WongsaNULL
Siriporn KaewdeeNULL
Somchai Jaidee081-234-5678
Narin Thongdee082-777-8888
Wichai Saelim083-999-0000
Kanya Boonmee084-246-1357
Tawan Rungruang085-123-4567
Pim Chaiya086-555-0101
Lalita Meesuk087-654-3210
Malee Srisuk089-111-2222
10 rows

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;
namephone
Somchai Jaidee081-234-5678
Narin Thongdee082-777-8888
Wichai Saelim083-999-0000
Kanya Boonmee084-246-1357
Tawan Rungruang085-123-4567
Pim Chaiya086-555-0101
Lalita Meesuk087-654-3210
Malee Srisuk089-111-2222
Anan WongsaNULL
Siriporn KaewdeeNULL
10 rows
SELECT name, phone FROM customer ORDER BY phone DESC NULLS FIRST;
namephone
Anan WongsaNULL
Siriporn KaewdeeNULL
Malee Srisuk089-111-2222
Lalita Meesuk087-654-3210
Pim Chaiya086-555-0101
Tawan Rungruang085-123-4567
Kanya Boonmee084-246-1357
Wichai Saelim083-999-0000
Narin Thongdee082-777-8888
Somchai Jaidee081-234-5678
10 rows

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;
nameprice
Cashew Nuts150.0
Cheesecake120.0
Dried Mango90.0
3 rows

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;
nameprice
Pen15.0
Potato Chips25.0
2 rows

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;
nameprice
Pen15.0
Highlighter25.0
2 rows

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;
idname
1Coffee
2Green Tea
3Orange Juice
4Croissant
4 rows
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 4;
idname
5Banana Bread
6Cheesecake
7Potato Chips
8Dried Mango
4 rows
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 8;
idname
9Cashew Nuts
10Notebook
11Pen
12Highlighter
4 rows
SELECT id, name FROM product ORDER BY id LIMIT 4 OFFSET 12;
idname
no rows

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.

  1. Show the 5 cheapest products.
  2. Show the customers sorted by city, and within each city by name.
  3. Show the 3 most recent orders.
  4. 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;
nameprice
Pen15.0
Highlighter25.0
Potato Chips25.0
Notebook30.0
Croissant35.0
5 rows
namecity
Anan WongsaBangkok
Lalita MeesukBangkok
Somchai JaideeBangkok
Wichai SaelimBangkok
Kanya BoonmeeChiang Mai
Malee SrisukChiang Mai
Narin ThongdeeKhon Kaen
Tawan RungruangKhon Kaen
Pim ChaiyaPhuket
Siriporn KaewdeePhuket
10 rows
idcustomer_idordered
1572026-11-25
1432026-11-18
1362026-11-11
3 rows
name
Croissant
Dried Mango
Green Tea
Highlighter
4 rows

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