SQL & Databases Essentials

Lesson 6 of 26

Filtering with WHERE

So far you've selected whole columns. The real power of SQL is filtering — returning only the rows that match a condition. The WHERE clause is how you do it, and it's one of the most important parts of SQL. "Which customers are in Lagos?" is a WHERE question.

The WHERE clause

Add WHERE condition to return only rows where the condition is true:

SQL — Try it Yourself
Loading editor…
Result

Click Run to execute the query.

This returns only customers whose city equals 'Lagos' — Ada and Sam. WHERE city = 'Lagos' is the filter: the database checks each row and keeps only the matching ones. Everything else is left out. This is how you get exactly the rows you care about instead of the whole table.

Text values go in quotes

Notice 'Lagos' is in single quotes — text (string) values in SQL must be quoted. Numbers are not quoted:

SQL — Try it Yourself
Loading editor…
Result

Click Run to execute the query.

WHERE age = 30 finds the customer aged exactly 30 (Ada) — and 30 is a number, so no quotes. Text ('Lagos') gets single quotes; numbers (30) don't. Mixing this up is a common early error.

WHERE with different columns

You can filter on any column:

SQL — Try it Yourself
Loading editor…
Result

Click Run to execute the query.

This selects the name and age columns, but only for rows where the city is Abuja (Lin and Tom). WHERE and column selection work together: SELECT chooses the columns, WHERE chooses the rows.

The structure of a query so far

Your queries now have a clear shape:

SELECT   which columns
FROM     which table
WHERE    which rows (the condition)

This SELECT-FROM-WHERE structure is the backbone of SQL. You'll add more clauses (ORDER BY, GROUP BY) but this core — choose columns, from a table, where a condition holds — underlies almost every query.

The mistake beginners make

Forgetting the quotes around text (WHERE city = Lagos without quotes errors, because SQL thinks Lagos is a column name), or adding quotes around numbers (WHERE age = '30' may work by coercion but is wrong practice). Also, using = correctly here — note SQL uses a single = for comparison in WHERE (unlike JavaScript's ===). Text in single quotes, numbers unquoted, single = to compare.

Your turn

SQL — Try it Yourself
Loading editor…
Result

Click Run to execute the query.


Your turn

  1. Filter customers by city with WHERE city = 'Lagos'.
  2. Filter by a number: WHERE age = 30 (no quotes around the number).
  3. Combine column selection and filtering: select name and age WHERE city = 'Abuja'.

Key points

  • WHERE condition returns only the rows where the condition is true — the heart of SQL filtering.
  • Text values go in single quotes ('Lagos'); numbers do not (30).
  • SQL uses a single = for comparison in WHERE (unlike JavaScript's ===).
  • The core query structure: SELECT columns FROM table WHERE condition.

Q&A · 0

Enrol to ask questions and join the discussion.

No questions yet — be the first to ask.