Lesson 11 / 37 · Ordering and limits

ORDER BY comes late, and ties are arbitrary

Until ORDER BY, rows have no guaranteed order. Alice, Diana, Grace and Ines all share region 1; their order among themselves is whatever the database found convenient.

The query

SELECT name, region_id
FROM customers
ORDER BY region_id

Try ORDER BY region_id, name: the name breaks every tie, so the order is the same every time.

Next: Where NULL sorts is up to you · All lessons