In this pattern, learn to turn a requirement into a filter condition: compare values, combine conditions, match lists and text patterns, and choose the right range boundaries. By the end, you will be able to reason why each row belongs in your result.
WHERE Basics
Below is an orders table with some columns. First, look at the data carefully.
Now let’s say we need orders with an amount greater than 100. Below is a SQL query for the same:
SELECT order_id, customer, amountFROM ordersWHERE amount > 100;WHERE amount > 100 describes which rows match the above condition. Conceptually, SQL checks that condition for each candidate row:
Since the condition matches only Asha’s and Carla’s orders. We got them in the Result.
WHERE keeps a row only when its complete condition evaluates to TRUE.
UNKNOWN when missing values are involved. WHERE excludes that result too. That is why only TRUE rows remain. P02 explains NULL and UNKNOWN Examples in this pattern focus on known values only.Comparison operators
The table below shows all the comparison operators we use in SQL. A table is provided to understand what a requirement might be and how that translates into comparison operations.
Note that the text values are used with single quotes, as in 'paid'. However, numeric values don’t require them.
A comparison expresses one decision. In the next section, we will see how to query multiple conditions.
Combining conditions with AND and OR
Here is an order status table. Using this table, we will introduce multiple SQL conditions like AND and OR.
AND requires every condition to be TRUE
Use AND when you need to check multiple conditions at the same time. A row is included in the result only when all the conditions are satisfied.
SELECT order_id, country, statusFROM order_statusWHERE country = 'India' AND status = 'paid';OR requires at least one of the conditions to be TRUE
Use OR when a row can match any one of several conditions. Unlike AND, which requires every condition to be true, OR includes a row even if only one condition is satisfied.
SELECT order_id, country, statusFROM order_statusWHERE status = 'paid' OR status = 'pending';Now, let’s change the requirement to: Return paid or pending orders from India. Both statuses must be restricted to India.
We have written a query for this requirement. Now, can you predict what this should return?
Which orders does it actually return?
SELECT order_id, country, statusFROM order_statusWHERE country = 'India' AND status = 'paid' OR status = 'pending';Always use parentheses when multiple operations are involved to clarify what the query should do.
IN and NOT IN: To match a list of values
When we have a list of values to match in a given column, IN makes the filter condition easier to read.
So, instead of:
WHERE status = 'paid' OR status = 'pending' OR status = 'processing'we write using,
WHERE status IN ('paid', 'pending', 'processing')Similarly, we can use NOT IN to exclude the listed values.
WHERE status NOT IN ('cancelled', 'refunded');Matching text patterns with LIKE
In SQL, the LIKE operator is used to search for a specific pattern in a string column. It is commonly used when you want to find records that start with, end with, or contain certain characters.
We will use the Customers table to explain the concepts here,
SQL LIKE uses two main wildcard characters:
Use Case:
1. Find names beginning with A:
SELECT customer_id, customerFROM customersWHERE customer LIKE 'A%';The A is fixed; % allows any remaining characters. It can also match zero characters, so the name A would qualify if it existed.
Where you place % determines which values match.
LIKE 'an%' -- begins with anLIKE '%an' -- ends with anLIKE '%an%' -- contains an anywhere2. Match exactly one character with _
SELECT customer_id, customerFROM customersWHERE customer LIKE '_m%';_ allows any first character, m fixes the second character, and % allows the rest. The result is Amit, customer 2.
Similarly, we can use LIKE with a NOT operator to exclude a pattern.
Which customers does this query return?
SELECT customer_id, customerFROM customersWHERE customer NOT LIKE 'A%';- In SQL,
_matches exactly one character, while%matches zero or more characters. LIKE 'SAVE_10'matchesSAVE_10,SAVEA10, andSAVEB10because_is a wildcard.- To match a literal underscore (
_), useESCAPE:
WHERE code LIKE 'SAVE!_10' ESCAPE '!';- Here,
!_matches a literal_. Similarly,!%matches a literal%.
Pause and predict
Which condition finds names whose second character is m?
-- Option AWHERE customer LIKE 'm%' -- Option BWHERE customer LIKE '_m%' -- Option CWHERE customer LIKE '%_m'Match ranges with BETWEEN
The SQL BETWEEN operator filters values within a specified range, including both boundary values.
WHERE column_name BETWEEN lower_value AND upper_valueLet’s consider the ORDERS table
SELECT order_id, amountFROM ordersWHERE amount BETWEEN 100 AND 300;BETWEEN includes both endpoints. The condition means:
WHERE amount >= 100 AND amount <= 300- Always specify the lower boundary first, followed by the upper boundary.
BETWEEN 300 AND 100checks for values>= 300 AND <= 100, so no numeric value can match.- Use
BETWEEN 100 AND 300instead.
Timestamp ranges
We will now use a timestamp filter. Lets see the Orders table below:
Here, if we use the condition created_at BETWEEN '2026-07-01' AND '2026-07-31', then it can miss Order Id 3. Because it will check BETWEEN 2026-07-01 09:00:00 and 2026-07-31 00:00:00.
To ensure we don’t miss any probable Ids, Use the start of the next month as the upper boundary:
SELECT order_id, created_atFROM ordersWHERE created_at >= '2026-07-01' AND created_at < '2026-08-01';Putting all the requirements together
Let’s solve a problem that takes into consideration all the above concepts.
Solution:
First split the requirement one by one as follows:
- Country must be India.
- Order time must fall within July.
- Status must be paid or pending.
- Amount must be from 100 through 500, inclusive.
Then combine these into where clause and display the result as shown below:
SELECT order_id, customer, country, status, amount, created_atFROM ordersWHERE country = 'India' AND created_at >= '2026-07-01' AND created_at < '2026-08-01' AND status IN ('paid', 'pending') AND amount BETWEEN 100 AND 500;Summary
In this section, we learned how to use the WHERE clause to filter rows using operators such as AND, OR, IN, NOT IN, LIKE, and BETWEEN.
We also explored how to combine conditions and avoid common filtering mistakes. With these fundamentals in place, we’re ready to move on to Pattern P02.