Difficulty: Beginner
Estimated time: 5–10 minutes
SQL concepts: ORDER BY, sorting results, ascending vs descending
Goal: Control the order of your query results.
The Scenario
You’re reviewing the product list again, but this time the product manager wants the results sorted by price, from the cheapest to the most expensive.
By default, SQL does not guarantee any specific order — so you need to explicitly tell the database how to sort the results.
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 |
| stock_qty | INTEGER | Units in stock |
| created_at | TIMESTAMP | When the product was created |
Sample Data (for context)
| product_id | name | category | price | stock_qty |
|---|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 | 120 |
| 102 | Mechanical Keyboard | Accessories | 89.00 | 45 |
| 103 | 27-inch Monitor | Displays | 229.99 | 18 |
| 104 | USB-C Hub | Accessories | 34.50 | 70 |
| 105 | Laptop Stand | Workspace | 39.99 | 32 |
Your Task
Write a SQL query that returns:
namepricecategory
Sorted by price in ascending order (lowest price first).
Expected Output
| name | price | category |
|---|---|---|
| Wireless Mouse | 24.99 | Accessories |
| USB-C Hub | 34.50 | Accessories |
| Laptop Stand | 39.99 | Workspace |
| Mechanical Keyboard | 89.00 | Accessories |
| 27-inch Monitor | 229.99 | Displays |
Constraints
- Do not use
SELECT * - Use
ORDER BYto sort results - Sort by
pricefrom lowest to highest - Column order must be:
name,price,category
Hint (Optional)
By default, ORDER BY sorts values in ascending order.
You can also specify ASC or DESC explicitly.
SELECT
name,
price,
category
FROM products
ORDER BY price;
Explanation
ORDER BY pricetells SQL how to sort the result set.- Ascending order (
ASC) is the default. - Without
ORDER BY, the database may return rows in any order.
Common Mistakes
- Assuming results are automatically sorted
- Putting
ORDER BYbeforeFROM - Sorting by a column not included in the query (database-dependent behavior)
Optional Extension (Mini Bonus)
Try:
- Sorting by price from highest to lowest
- Sorting by category first, then price
- Combining
WHEREandORDER BY
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Next Challenge
Beginner Challenge #4: Count Rows with COUNT()
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.