SQL WHERE

The WHERE clause keeps only the rows that match a condition. Without it, a query returns every row in the table, which is rarely what you want on real data.

A condition compares a column with a value using an operator such as =, <>, <, >, <= or >=. Text and dates go in single quotes; numbers don't.

Syntax

SELECT columns
FROM table_name
WHERE condition;

Try it yourself

Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.

where.sql · PostgreSQL
Loading editor…

Examples

1. Filter on text

SELECT first_name, last_name, city FROM customers WHERE country = 'Germany';

Text comparisons are case-sensitive in PostgreSQL.

2. Filter on a number

SELECT name, price FROM products WHERE price >= 100;

Returns products costing 100 or more.

3. Filter on a date

SELECT id, order_date FROM orders WHERE order_date > '2025-10-01';

Dates are written as 'YYYY-MM-DD'.

Common mistakes

  • Writing WHERE email = NULL. Nothing is equal to NULL; use IS NULL.
  • Using double quotes around text values: WHERE country = "Germany" looks for a column named Germany.
  • Filtering on an aggregate such as WHERE COUNT(*) > 5. That belongs in HAVING.

Practice: test yourself

This challenge is from our interactive course. Write the query and press Check answer.

Challenge · 10 pts

Return first_name, last_name and city of customers whose country is 'Germany'.

challenge.sql · PostgreSQL
Loading editor…

FAQ

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT or SUM.

What does <> mean?

It means "not equal to". PostgreSQL also accepts != for the same thing.

Learn SQL step by step

65 hands-on exercises, streaks, a leaderboard and a certificate.

Start free →