mahir_data
Chapters0 / 26 completed

Chapter 03 · basic · 6 min

WHERE: filtering rows

SELECT chooses columns. WHERE chooses rows: it keeps only the rows where a condition is true and quietly drops the rest. This is where SQL starts to feel powerful, since instead of eyeballing a whole table, you describe exactly what you want and the database hands back only the matching rows.

The shape is always the same: WHERE goes after the table, then a condition.

SELECT column1, column2
FROM table_name
WHERE condition;

We'll filter an e-commerce orders table throughout this chapter.

The dataset

An `orders` table: each order has a customer, a total in ringgit, a status, and the state it shipped to. One order (Gina's) has no total recorded, a NULL, which will matter shortly.

A single condition

Say you want every order worth more than RM 100. Put the condition after WHERE; the database checks it against each row and keeps only the rows where it's true.

Run the example below. Out of seven orders, only three come back:

customertotal
Aisyah120.00
Chong310.00
Farid210.00

Notice who's missing: Gina, whose total is NULL. A comparison against NULL is never "true" (it's unknown), so that row is dropped, a trap we'll cover properly in the NULL chapter. For now, just know NULLs quietly fall out of a WHERE.

Orders over RM 100

Editable, try changing it

The comparison operators

> is just one of six. Here's the full set you'll use in WHERE conditions, worth committing to memory because they're the same in every SQL dialect:

OperatorMeaningExampleReads as
=equal tostatus = 'pending'status is exactly 'pending'
<> or !=not equal tostatus <> 'delivered'status is anything but 'delivered'
<less thantotal < 50total below 50
>greater thantotal > 100total above 100
<=less than or equaltotal <= 50total is 50 or below
>=greater than or equaltotal >= 100total is 100 or above

A common surprise for beginners: equality is a single =, not == like most programming languages.

Filtering on text, not just numbers

The same operators work on text and dates too. For text, = (an exact match) is the one you'll reach for most; just remember to wrap the value in single quotes.

customertotal
Aisyah120.00
Chong310.00
Aisyah95.00

Want the opposite, everything that is not delivered? Swap = for <> and you'd get Bala, Devi, Farid, and Gina instead.

Only delivered orders

Editable, try changing it

Why WHERE matters more than it looks

WHERE isn't just a convenience for tidier output. On real data it's a cost and speed lever. Production tables have millions or billions of rows; a query that filters down to the 200 rows you actually need reads far less data than one that drags back the whole table. On a cloud warehouse like BigQuery or Snowflake, where you're billed by data scanned, a careless unfiltered query can cost real money. Filtering early is one of the first habits that separates a working query from a good one.

If you've used Excel or Google Sheets

You already know this concept; you've just clicked it instead of typing it. In Sheets, Data → Create a filter, then Filter by condition → Greater than → 100 keeps only the rows over 100 and hides the rest. WHERE total > 100 does exactly the same thing.

The difference is that SQL states the intent explicitly and repeatably: no clicking through menus, no manually re-applying the filter when the data changes. Write it once and it runs the same way every time.

Ready to try it for real? The Touch 'n Go active wallets question in the practice set below is a pure WHERE exercise, a good first one to attempt.

One condition already lets you ask real questions of a table instead of scanning it by eye. But real questions are rarely just one condition; you usually want orders that are delivered and over RM 100, or shipped to Selangor or Penang. That's the next chapter: combining conditions with AND, OR, and NOT.

Sign in to track your progress.

Now practise it

Questions in the bank that drill this chapter's concept: