Skip to the content

LIMIT

SQL · Unit 3: Sorting and limiting

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

idquantity
410
35
83

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. 1. Predict the output

    A customer asks for the cheapest things in the store. What does this query return?

  2. 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. 3. Multiple choice

    Someone wants the cheapest product and writes this. What does it return?

  4. 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. 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. 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. 7. Complete the code

    What's the second most expensive product in the store? Fill in the blank to skip the first one.

  8. 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. 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. 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 name and the value of its stock, which is price * stock. Show only the two products with the most value, highest first.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →