Difficulty: Beginner
Estimated time: 10–15 minutes
SQL concepts: ORDER BY, ascending vs descending sort
Goal: Control the order of rows returned by a query.
The Scenario
A product manager asks:
“Can you show me the products sorted by price, from cheapest to most expensive?”
By default, SQL does not guarantee row order.
To control how results appear, you must explicitly use ORDER BY.
Database Table
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
Sample Data
| product_id | name | category | price |
|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 |
| 102 | Mechanical Keyboard | Accessories | 89.00 |
| 103 | 27-inch Monitor | Displays | 229.99 |
| 104 | USB-C Hub | Accessories | 34.50 |
| 105 | Laptop Stand | Workspace | 39.99 |
| 106 | Webcam | Accessories | 59.99 |
Your Task
Write a SQL query that returns:
nameprice
…sorted by price from lowest to highest.
Expected Output
| name | price |
|---|---|
| Wireless Mouse | 24.99 |
| USB-C Hub | 34.50 |
| Laptop Stand | 39.99 |
| Webcam | 59.99 |
| Mechanical Keyboard | 89.00 |
| 27-inch Monitor | 229.99 |
Constraints
- Use
ORDER BY - Sort by
price - Do not use
SELECT *
Hint (Optional)
ASC= ascending (default)DESC= descending
You can write:
ORDER BY price ASC
or simply:
ORDER BY price
SELECT
name,
price
FROM products
ORDER BY price ASC;
Explanation
ORDER BYcontrols the order of rows in the result set.ASCsorts from smallest to largest.- Without
ORDER BY, row order is undefined.
Common Mistakes
- Assuming SQL returns rows in insert order
- Using
ORDER BYon a column not in the SELECT list- Some databases allow this, some don’t.
- Forgetting DESC when reversing order
Optional Extension (Mini Bonus)
Try:
- Sort products by price descending
- Sort by
categoryfirst, thenprice - Combine
ORDER BYwithWHEREto filter before sorting
- Combine
ORDER BYwithWHEREto filter before sorting
Next Challenge
Beginner Challenge #19: Limiting Results with LIMIT
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.