Explain it simply
Explain it simply
WHERE works like a sieve. Pour every row of the table into it and only the rows that satisfy the condition stay; the rest fall through. With Country = 'Mexico', only the Mexican customers remain.
Behind every search box, filter button, admin report and user dashboard there is a WHERE clause. On a shop site, WHERE category = 'Laptop' shows only laptops, and your order history page uses WHERE customer_id = (your ID) to show only your orders.
Problem → Why → Solution
The problem: zero rows although the data is there
On an admin page, a user types mexico in lowercase into the search box. The backend drops that text into a WHERE clause and runs the query. The table holds two Mexican customers, yet the result is (0 rows), and the user files a bug report saying the data has vanished.
SELECT CustomerName, Country
FROM Customers
WHERE Country = 'mexico';
-- (0 rows) although 'Mexico' is in the tableWhy are there zero rows?
In PostgreSQL, comparing text with = checks every character exactly: 'mexico' and 'Mexico' differ by one capital M, and a trailing space would make them differ too. MySQL's default collation is case-insensitive, which is why people who learned there are surprised. Writing WHERE lower(Country) = 'mexico' searches without caring about case.
Core concept: a true-or-false question for every row
The condition after WHERE (also called a predicate) is evaluated once for every row. Rows where it is true go into the result; rows where it is false are dropped. There is a third possibility: when a column is NULL the outcome is unknown, and those rows are dropped too. Lesson 7 covers that case in detail.
| Operator | Meaning | Example |
|---|---|---|
| = | equal to | Country = 'Mexico' |
| <> or != | not equal to | Country <> 'UK' |
| < , > | less / greater than | CustomerID > 2 |
| <= , >= | at most / at least | OrderDate >= '2024-09-19' |
<> is the SQL standard's not-equal operator, and PostgreSQL, MySQL and SQLite all accept != as well. How an operator behaves depends on the column's data type: numbers compare by value, dates by the calendar, and text by its collation (the rules for ordering characters). In OrderDate >= '2024-09-19' the date sits inside quotes, but PostgreSQL sees it is being compared with a date column and converts it to a date.
The database does not evaluate a query in the order it is written but in the logical order FROM → WHERE → SELECT. So when WHERE runs, the aliases in the SELECT list do not exist yet: WHERE n = 'Ana Trujillo' with an alias n fails with column "n" does not exist. Use the table's real column names in WHERE. A column tested in WHERE does not need to appear in the SELECT list either; you can filter on Country and display only CustomerName, and the filter still works exactly the same.
- FROM supplies every row
- Test the condition on each row
- Keep true; drop false or unknown
- SELECT shapes the survivors
WHERE runs before SELECT, so it cannot see SELECT's aliases.
General pattern
SELECT column_a, column_b
FROM table_name
WHERE some_column operator value;
-- operator: = <> != < > <= >=WHERE comes after FROM. Write text values in single quotes, numbers bare, and dates as 'YYYY-MM-DD' inside quotes.
Example: filter the Mexican customers
The Customers table from Lesson 3 holds four customers. This query compares the Country column with 'Mexico' on every row and shows the name and country of the matches. Guess the number of rows before you run it. Then try changing 'Mexico' to 'UK', and to 'mexico', and predict each result before pressing run; the second change is the bug from the problem section. Small experiments like these build intuition faster than reading alone.
SELECT CustomerName, Country
FROM Customers
WHERE Country = 'Mexico';Code breakdown
- Line 1
SELECT CustomerName, CountryShows two columns of the rows that survive the filter. SELECT is written first but evaluated after WHERE.
- Line 2
FROM CustomersThe source table, which feeds all four rows into the WHERE test.
- Line 3
WHERE Country = 'Mexico';Tests Country = 'Mexico' on each row. The Germany and UK rows are false and dropped; the two Mexico rows remain.
- Output
Four customers exist, but only the two that match appear. Writing 'mexico' in lowercase would have returned (0 rows).
customername | country
----------------+---------
Ana Trujillo | Mexico
Antonio Moreno | Mexico
(2 rows)Comparing numbers and dates
Operators such as < and >= are used most on number and date columns. The query below finds orders placed on or after 19 September 2024. Because it uses >=, the order from the 19th itself is included; with > it would not be. Always decide whether the boundary value belongs in the result when you pick the operator. Comparing CustomerID > 2 works the same way on a number column: customers 3 and 4 qualify, customer 2 does not.
SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate >= '2024-09-19';
-- 10309 | 2024-09-19
-- 10310 | 2024-09-20In real projects
Where this shows up in real projects
In web apps WHERE also matters for security. An order history page that is not restricted with WHERE customer_id = (the logged-in user's ID) can show other people's orders. Multi-tenant SaaS apps often have a rule that every query must carry WHERE tenant_id = ....
For search and filters, the value a user types is sent as a parameter ($1) instead of being pasted into the SQL text, which prevents SQL injection (Lesson 26). Reports filter by date ranges such as WHERE order_date >= '2024-09-01' every day, and on large tables that column usually gets an index (Lesson 30). With an index, the database can jump straight to the matching rows instead of testing millions of them one by one.
Common mistakes
Common mistakes
1. Using a SELECT alias in WHERE
SELECT CustomerName AS n FROM Customers WHERE n = 'Ana Trujillo'; fails with column "n" does not exist, because WHERE runs before SELECT. Write WHERE CustomerName = 'Ana Trujillo' using the real column name.
2. Leaving text unquoted
WHERE Country = Mexico without quotes makes PostgreSQL treat Mexico as a column name and fail with column "mexico" does not exist. Put every text value in single quotes; numbers need none.
3. Getting the boundary wrong
"From ID 3 onwards" means >= 3; with > 3, customer 3 silently disappears. Off-by-one mistakes like this raise no error, just one row too few, so they are hard to spot. Always test with data that includes the boundary row.
Best practices
Best practices
- Before running a query, count in the sample data which rows you expect the condition to keep.
- For ranges, decide whether each boundary is included; pairing >= with < is the cleanest way to express a date range.
- When typed text must match regardless of case, apply lower() to both the column and the value.
- Send values from application code as parameters, never pasted into the SQL text.
Mini challenge
Mini challenge: three filters
Using the Customers and Orders tables, write three queries, each with a different comparison operator.
- CustomerName and Country of customers not in Germany
- CustomerID and CustomerName of customers whose ID is less than 3
- OrderID and OrderDate of orders placed before 2024-09-20 (the 20th excluded)
-- TODO 1: everyone outside Germany (which "not equal" operator?)
-- TODO 2: customers with an ID below 3
-- TODO 3: orders before 20 September 2024 (is the 20th included?)Expected: Query 1: three customers (the two in Mexico and the one in the UK). Query 2: 1 Alfreds Futterkiste and 2 Ana Trujillo. Query 3: 10308 (2024-09-18) and 10309 (2024-09-19).
Show hint
Use <> for the first query. "Before" in the third means <, with the date written as '2024-09-20' in quotes.
Show solution
SELECT CustomerName, Country
FROM Customers
WHERE Country <> 'Germany';
SELECT CustomerID, CustomerName
FROM Customers
WHERE CustomerID < 3;
SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate < '2024-09-20';Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- WHERE tests a condition on each row and keeps only the rows where it is true.
- Use =, <> (!=), <, >, <= and >= on text, numbers and dates, and decide whether the boundary is included.
- PostgreSQL compares text case-sensitively: 'Mexico' is not 'mexico'.
- WHERE runs before SELECT, so use real column names, not aliases.
PostgreSQL SQL Tutorial — PostgreSQL Global Development Group