Start learning free

LESSON 37 OF 173 · BACKEND: SQL

Filter Rows with WHERE in TypeScript

Practice

So far every query returned all 8 products. To keep only some rows, add a WHERE line after FROM. It holds a condition: the database checks it on every row and keeps the rows where it is true, like an if inside a for loop, or .filter() on a TypeScript array.

The comparisons are the ones you know from TypeScript: <, <=, > and >=. The condition can test any column of the table, even one you don't select. This keeps the products with 100 or more in stock, the Pen and the Gift card:

const query: string = [
  "SELECT name",
  "FROM products",
  "WHERE stock >= 100",
].join(" ");

The clauses always come in this order: SELECT, FROM, WHERE. Watch the edge of the range: <= keeps a value equal to the limit, < doesn't.

Your query must print the products that cost 10 or less:

name | price
Notebook | 7
Pen | 2
Stapler | 9
(3 rows)

Your task: write a query that reads each product's name and price from the products table, and keeps only the products whose price is 10 or less.


Assignment
Follow the Coding Tutorial to choose the columns, read the products table, then filter its rows with a WHERE condition.


Hint
The WHERE line goes after FROM, as one more string in the array. "10 or less" means the price may also equal 10, so use <=, written with no space between the two symbols.


STEP 1 OF 3
    Ctrl + Enter to check

    Sql Where in TypeScript

    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free