Start learning free

LESSON 39 OF 173 · BACKEND: SQL

IN and BETWEEN

Practice

Two shortcuts make conditions shorter and easier to read. IN tests whether a value is one of a list: the column, IN, then the values in parentheses, separated by commas. It does the job of several = joined with OR, like includes with an array in TypeScript.

BETWEEN tests whether a value is in a range, both ends included: stock BETWEEN 1 AND 10 means stock >= 1 AND stock <= 10. The AND inside it only separates the two ends.

This query keeps the Mug (stock 10) and the Lamp (stock 1), which sit right on the ends of the range. The Pen is in the list, but with 120 in stock it is left out:

const query: string = [
  "SELECT name, stock",
  "FROM products",
  "WHERE name IN ('Mug', 'Lamp', 'Pen')",
  "AND stock BETWEEN 1 AND 10",
].join(" ");

Your query must print the home and kitchen products that cost from 5 to 15:

name | category | price
Mug | kitchen | 12
Plant | home | 15
(2 rows)

Your task: write a query that reads each product's name, category and price from products. Keep the products whose category is 'home' or 'kitchen', using IN, and whose price is from 5 to 15, using BETWEEN.


Assignment
Follow the Coding Tutorial to filter the products with IN for the categories and BETWEEN for the price range.


Hint
The values in the IN list are text, so each goes in single quotes. The price condition is joined to the first with AND, and BETWEEN has its own AND between the low and the high end. The Plant costs exactly 15: it belongs in the answer.


STEP 1 OF 4
    Ctrl + Enter to check