SQL
5 min read
BEGINNER
ORDER BY
Quick AnswerORDER BY specifies the order of returned rows. ASC means ascending and DESC means descending. Without ORDER BY, do not rely on the order of results.
Core idea
ORDER BY specifies the order of returned rows. ASC means ascending and DESC means descending. Without ORDER BY, do not rely on the order of results.
Minimal example
sql
SELECT id, name FROM products ORDER BY price_cents ASC, id ASC;Expected SQLite rows:
text
4 | Pencil2 | Pen1 | Notebook3 | BagHow it works
Place ORDER BY after WHERE when present. Each sort expression can have its own direction. Sorting does not change the stored rows. Here prices are integers in cents.
Practical example
sql
SELECT id FROM products ORDER BY price_cents DESC, id ASC;Expected SQLite rows:
text
3124Common mistake and practice
Do not assume insertion order. Sort the products by price descending; Bag should appear first.