Before you start, you need
Get a short list in a stable order
To see the three most expensive products you need to sort first and choose afterwards. ORDER BY decides the order of the rows; LIMIT says how many to show.
ASC sorts from lowest to highest and is the default if you write nothing. DESC sorts from highest to lowest. So ORDER BY price DESC puts the highest prices first.
If two products have the same price, add another column to decide which comes first. In ORDER BY price DESC, id ASC, the id is used only when prices are tied.
LIMIT 3 keeps the first three rows of that list. Without ORDER BY, you do not say which ones you want to choose. The stored catalogue does not change, and the column order still depends on SELECT.
SELECT columns
FROM table_name
ORDER BY column_name DESC, id ASC
LIMIT 3;See it in action
What are the three most expensive products?
SELECT id, name, price
FROM products
ORDER BY price DESC, id ASC
LIMIT 3;| id | name | price |
|---|---|---|
| 4 | Pro Laptop | 999 |
| 7 | Monitor 27 | 249 |
| 3 | Monitor 24 | 179 |
- price DESC puts the highest prices first. id ASC fixes the order if two products cost the same.
- LIMIT 3 keeps the first three rows once they are sorted. It does not change the catalogue.
- LIMIT without ORDER BY does not say which three rows they should be. The ordering is written before the limit.
- In the challenge, LIMIT cuts right inside a tie: you will see why a second key is needed.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Choose the two lowest stock levels
Write a query that returns the two products with the least stock, showing name and stock. If they are tied, sort by name alphabetically.
Expected result: 2 rows with the columns name, stock, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Look for the lowest stock first, and decide the order between tied products.
Add name ASC as a second key to break ties.
Limit the result to two rows.
Solution explained
SELECT name, stock FROM products ORDER BY stock ASC, name ASC LIMIT 2;Stock decides the priority, and the name makes the order deterministic when two products have the same stock.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Cut the list inside a tie
Show id, name and price of the two most expensive products. If two prices tie, the lower id wins.
Expected result: 2 rows with columns id, name, price, in that order. Respect the requested row order.
This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.
View tables and data
Open this panel to see the full data.
Look in the data for two products with the same price near second place.
Sort by price in descending order and add a second key for the tie.
ORDER BY price DESC, id ASC comes before LIMIT 2.
Solution explained
SELECT id, name, price FROM products ORDER BY price DESC, id ASC LIMIT 2;Pro Laptop takes first place. Monitor 27 and Tied price both cost 249: id ASC keeps Monitor 27 (id 7) and leaves out id 20. Without a tiebreaker, the engine could pick either of them.
Key takeaways
- ORDER BY sets the order of the rows.
- ASC goes from lowest to highest; DESC, from highest to lowest.
- A second key breaks ties.
- LIMIT takes the first rows after sorting.
Go deeper
To read the next slice, LIMIT 3 OFFSET 3 skips the first three sorted rows. Keep the same ordering and tiebreaker between slices. Variants of this syntax depend on the engine.
Going further: read the next three prices, keeping the same order.
SELECT id, name, price FROM products ORDER BY price DESC, id ASC LIMIT 3 OFFSET 3;| id | name | price |
|---|---|---|
| 8 | SSD Drive | 89 |
| 5 | Headphones | 69.9 |
| 10 | Speaker | 54 |
- OFFSET 3 skips the first three rows of the sorted result. LIMIT 3 takes the next ones. For another page, change the offset while keeping the ordering and the tiebreaker.