Project: the top three
SQL · Unit 3: Sorting and limiting
- Full plan
- SQL
- Project
- 3 exercises
The store is about to launch a home screen with three products. The first try is the three cheapest, to draw people in.
Look at the first row. The home screen would advertise the Cable, but there are none left. ORDER BY and LIMIT don't care about stock. You have to say that yourself, earlier in the query.
SELECT name, price, stock
FROM products
ORDER BY price
LIMIT 3;Returns
| name | price | stock |
|---|---|---|
| Cable | 5.25 | 0 |
| Mouse | 19.9 | 30 |
| Keyboard | 45.5 | 12 |
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Put the lines in order
Fix the deals on the home screen. Put in order the query that shows the three cheapest products that are in stock. One line doesn't belong.
2. Write the code
The owner has a change of heart and wants to show off the best the store has. Write a query that shows the
nameand theprice, in that order, of the three most expensive products that are in stock. The most expensive goes first.3. Write the code
The home screen has a "See more" button, which shows the next three products. Finish the query so that it shows the
nameand theprice, in that order, of the fourth through sixth most expensive products in stock. The most expensive of the three goes first.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.