SQL Beginner Challenge 18: Sorting Results with ORDER BY

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_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price

Sample Data

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
10327-inch MonitorDisplays229.99
104USB-C HubAccessories34.50
105Laptop StandWorkspace39.99
106WebcamAccessories59.99

Your Task

Write a SQL query that returns:

  • name
  • price

…sorted by price from lowest to highest.

Expected Output

nameprice
Wireless Mouse24.99
USB-C Hub34.50
Laptop Stand39.99
Webcam59.99
Mechanical Keyboard89.00
27-inch Monitor229.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 BY controls the order of rows in the result set.
  • ASC sorts from smallest to largest.
  • Without ORDER BY, row order is undefined.

Common Mistakes

  1. Assuming SQL returns rows in insert order
  2. Using ORDER BY on a column not in the SELECT list
    • Some databases allow this, some don’t.
  3. Forgetting DESC when reversing order

Optional Extension (Mini Bonus)

Try:

  1. Sort products by price descending
  2. Sort by category first, then price
  3. Combine ORDER BY with WHERE to filter before sorting
  1. Combine ORDER BY with WHERE to 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.