LIMIT
SQL · Unit 3: Sorting and limiting
- Full plan
- SQL
- 10 exercises
What are the three biggest orders the store has had? You already know how to sort. What's new is keeping only the ones on top.
LIMIT goes at the very end, after the ORDER BY, with the number of rows you want. SQL sorts the whole table and cuts there.
SELECT id, quantity
FROM orders
ORDER BY quantity DESC
LIMIT 3;Returns
| id | quantity |
|---|---|
| 4 | 10 |
| 3 | 5 |
| 8 | 3 |
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
A customer asks for the cheapest things in the store. What does this query return?
2. Put the lines in order
Which two products does the warehouse have the most of? Put the query in order. One line doesn't belong.
3. Multiple choice
Someone wants the cheapest product and writes this. What does it return?
4. Predict the output
In the first lesson, the person looking for a gift under 100 got a list of five products. Now they want a single answer. What does this query return?
5. Find the bug
The owner gives a prize to the four biggest orders and, on a tie, the most recent wins. Three orders of 2 units are fighting over fourth place, and it should go to order 10, from January 15. Order 1 shows up instead. Which line has the problem?
6. Multiple choice
Someone asked for only the name of the youngest customer and got "Hannah," with nothing odd in sight. What change to the query would have warned them?
7. Complete the code
What's the second most expensive product in the store? Fill in the blank to skip the first one.
8. Predict the output
The biggest order already has its prize. Now it's the turn of the second and the third. What does this query return?
9. Find the bug
The catalog goes from cheapest to most expensive, with 3 products per screen. The third screen should show the last two, the Tablet and the Laptop. It shows three others. Which line has the problem?
10. Write the code
Which two products have the most of the owner's money tied up in them? Write a query with two columns, in this order: the
nameand the value of its stock, which isprice * stock. Show only the two products with the most value, highest first.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.