← Databases & SQL
4 / 25
Lesson 4 — Sorting
Sorting with ORDER BY
Rows come back in no guaranteed order unless you ask. ORDER BY column sorts ascending (small to big, A to Z); add DESC for descending. You can sort by several columns: the second one breaks ties in the first.
SELECT name, price FROM products ORDER BY price DESC, name
Why it matters: "cheapest first", "newest first", "alphabetical" — users expect sorted lists, and sorting in the database is faster than sorting in your code.
Want it another way?
Task
Sort the products by price, cheapest first; for equal prices sort by name. Expected: Pen and Pencil (both 2), then Mug, then Lamp.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.