SQL 101· Level 2

WHERE

WHERE keeps only the rows that pass a test. It goes after FROM.

  • Text goes in single quotes: state = 'NSW'. Numbers do not: mark >= 50.
  • Compare with =, <> (not equal), >, >=, < and <=.
  • Combine tests with AND (both must be true) or OR (either can be true).
  • IN ('NSW', 'VIC') matches any value in a list.

Missing values. An empty cell is called NULL. It means "unknown", so SQL cannot say whether it equals anything. email = NULL never matches a row, and neither does email <> NULL. Use IS NULL or IS NOT NULL instead.

Worked example

Which students come from Queensland?

SELECT first_name, last_name, state, commencement_year
FROM students
WHERE state = 'QLD'
Getting the practice database ready…

This takes a few seconds the first time. Nothing leaves your computer.

Exercises in this level