Problem
Task
List all orders so the most recent order appears first.
Schema
Table Schema
orders(id, customer_id, total_amount, ordered_at)
Input
Sample Data
| id | customer_id | total_amount | ordered_at |
|---|---|---|---|
| 101 | 1 | 58.5 | 2026-03-02 |
| 102 | 3 | 19.99 | 2026-03-09 |
| 103 | 2 | 104 | 2026-02-18 |
Output
Expected Output
| id | customer_id | total_amount | ordered_at |
|---|---|---|---|
| 102 | 3 | 19.99 | 2026-03-09 |
| 101 | 1 | 58.5 | 2026-03-02 |
| 103 | 2 | 104 | 2026-02-18 |
Answer
Check Your Solution
Show Answer and Explanation
Correct Answer
SELECT *
FROM orders
ORDER BY ordered_at DESC;
Explanation
ORDER BY sorts the final result set. DESC means descending order, so later dates are placed before earlier dates.
Common Mistakes
- Using ASC, which puts the oldest order first.
- Sorting by id instead of ordered_at.
- Assuming the database returns rows in insertion order without ORDER BY.
Concepts
Related Concepts
ORDER BY
DESC
Sorting
Dates
Next practice