Lesson 2 of 12 · 7 min

Filtering with WHERE

WHERE filters rows. Only rows where the condition is true are returned:

SELECT name, price FROM products WHERE price > 100;

Text values go in single quotes:

SELECT name FROM customers WHERE city = 'Harare';

Comparisons: = (equal), <> or != (not equal), <, >, <=, >=.

Combining conditions

  • AND: both must be true.
  • OR: at least one must be true.
  • NOT: turns true into false.
SELECT name FROM products
WHERE category = 'Furniture' OR price < 5;

A few handy shortcuts:

WHERE price BETWEEN 50 AND 200        -- 50 to 200, inclusive
WHERE city IN ('Harare', 'Mutare')    -- any of these
WHERE name LIKE 'P%'                  -- starts with P (% means "anything")

A query can span several lines; SQL only cares where the semicolon is.

Your task

Show the name and price of products in the Electronics category that cost less than 300.

query.sql
-- Electronics under 300

SQLite runs right here in your browser. The first run downloads about 0.7 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…