Skip to the content

Project: the store's search box

SQL · Unit 2: Filtering

A store's search box is a WHERE that someone builds without knowing it. They pick a category, set a top price, type a few letters, and each of those becomes a condition.

This is the search of someone who picked Accessories and a cap of 20. Look at what it offers them: the Cable, which is out of stock. This search box is missing a filter.

SELECT name, price, stock
FROM products
WHERE category = 'Accessories'
  AND price <= 20;

Returns

namepricestock
Mouse19.930
Cable5.250

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

    Someone types the letter M and sets a cap of 100. Put their search in order. One line doesn't belong.

  2. 2. Write the code

    Someone else picks the Audio category and a price from 50 to 70, both ends included. Finish the query so it shows the name and the price, in that order, of what it finds.

  3. 3. Write the code

    The whole search box. Someone types the letter a, sets a cap of 100, and checks the box for in-stock items only. Write a query that shows the name and the price, in that order, of the products that fit all three: the name contains a, they cost 100 or less, and they're in stock.

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 →