DSA Factory
Free SQL queries lessonsSQL queries · Stage 0 · Reading a table · Step 2

Keep only the rows that pass

Filter rows with WHERE and a comparison. About 7 minutes.

WHERE keeps the rows that pass

The library desk gets a question: which books came out after 1950? You don't want every row, only the ones that pass a test.

WHERE is that test. Write it after FROM, and the database checks each row against it. A row that passes is kept, and a row that fails is dropped. Only then does SELECT pick its columns from the rows that stayed.

WHERE: keep a row only if its test is true.
In code
SELECT title
FROM books
WHERE year > 1950;
WHERE year > 1950
books
idtitlegenreyear
1GitanjaliPoetry1910
2Train to PakistanNovel1956
3The GuideNovel1958
4MadhushalaPoetry1935

Each row is tested against year > 1950, one at a time.

Move 1 of 6

The tests you can write

A test compares a column with a value. You can ask for equal, not equal, smaller, larger, at most or at least. The symbols for each are in the box below.

Text goes inside single quotes, and numbers don't. Text is matched exactly, so Poetry and poetry are different values. Equal is one equals sign, not two.

Quotes: text values need single quotes, numbers don't.
In code
WHERE genre = 'Poetry'    -- equal
WHERE copies >= 3         -- at least
WHERE genre <> 'Novel'    -- not equal
WHERE year < 1950         -- smaller
Quick check

Which rows does WHERE price >= 100 keep?

  1. ARows priced 100 or more
  2. BOnly rows priced above 100
  3. CRows priced 100 or less
Show the answer

Rows priced 100 or more. >= means greater than or equal to, so 100 itself passes.

Your problem

The shortlist

The applicants table has one row per applicant. Show the name of every applicant whose score is above 60.

Tables
applicants(id, name, city, score)

Up to 2,000 applicants

Write the query in your browserFree account, no card. Your query runs on real tables, with hints if you get stuck, and your progress is saved.
Sign up free to solve