Skip to the content

Project: the top three

SQL · Unit 3: Sorting and limiting

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

namepricestock
Cable5.250
Mouse19.930
Keyboard45.512

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 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. 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 name and the price, in that order, of the three most expensive products that are in stock. The most expensive goes first.

  3. 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 name and the price, in that order, of the fourth through sixth most expensive products in stock. The most expensive of the three goes 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 →