Nishaglobal Education logo

WHERE: filtering rows

Filter SQL results with WHERE: compare numbers, text and dates, combine conditions with AND and OR, and use IN, BETWEEN and LIKE pattern matching.

Lesson 3 of 1230 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Keep only the rows you need with WHERE
  • Combine conditions with AND, OR and brackets
  • Use IN, BETWEEN and LIKE to write shorter, clearer filters
Outline
  1. What you'll learn and why it matters
  2. Your first filter
  3. Comparison operators
  4. Combining conditions: AND and OR
  5. Shortcuts: IN and BETWEEN
  6. Pattern matching with LIKE
  7. Text is matched exactly
  8. Practice
  9. Common mistakes
  10. Recap
  11. Quiz
  12. Mark lesson complete

What you'll learn and why it matters

SELECT chooses columns; WHERE chooses rows. Almost every real question has a filter in it: customers in India, products under ₹300, orders since October. WHERE is the clause you will write more than any other.

Your first filter

SQL
SELECT name, city
FROM customers
WHERE country = 'India';

Result: 4 rows

Query result
namecity
Asha PatelMumbai
Ravi KumarBengaluru
Meera NairKochi
Arjun SinghDelhi
customersnamecountryAsha PatelIndia✓John SmithUK✗Ravi KumarIndia✓Li WeiSingapore✗Meera NairIndia✓WHERE country = 'India'resultnamecountryAsha PatelIndiaRavi KumarIndiaMeera NairIndia
WHERE checks the condition for every row. Rows where it is true are kept; the rest are left out of the result.

The database looks at each of the 10 customers and keeps the 4 rows where country is 'India'. Text values go in single quotes. Numbers do not need quotes.

Comparison operators

OperatorMeaningExample
=equal tocountry = 'UK'
<> or !=not equal tostatus <> 'cancelled'
< >less than, greater thanprice < 300
<= >=less/greater than or equal toquantity >= 2
SQL
SELECT name, price
FROM products
WHERE price < 300;

Result: 4 rows

Query result
nameprice
Notebook A5120
Gel Pens (pack of 5)90
USB-C Cable299
Coffee Mug249

Dates in our database are stored as text in the form YYYY-MM-DD. Because the year comes first, comparing them as text also puts them in date order, so this finds orders from October 2025 onwards:

SQL
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date >= '2025-10-01';

Result: 5 rows

Query result
order_idcustomer_idorder_date
1192025-10-02
1212025-10-05
1342025-10-08
1452025-10-11
1532025-10-15

Combining conditions: AND and OR

AND keeps a row only when both conditions are true. OR keeps it when at least one is true.

SQL
SELECT name, category, price
FROM products
WHERE category = 'Books' AND price < 400;

Result: 1 row

Query result
namecategoryprice
SQL Pocket GuideBooks350
SQL
SELECT name, category, price
FROM products
WHERE category = 'Books' OR category = 'Home';

Result: 4 rows

Query result
namecategoryprice
Python Basics BookBooks450
SQL Pocket GuideBooks350
Water BottleHome399
Coffee MugHome249

Shortcuts: IN and BETWEEN

IN checks against a list, replacing a chain of ORs:

SQL
SELECT name, country
FROM customers
WHERE country IN ('UK', 'Canada', 'Australia');

Result: 3 rows

Query result
namecountry
John SmithUK
Sara LopezCanada
Emma BrownAustralia

BETWEEN checks a range and includes both ends:

SQL
SELECT name, price
FROM products
WHERE price BETWEEN 200 AND 500;

Result: 5 rows

Query result
nameprice
Python Basics Book450
SQL Pocket Guide350
USB-C Cable299
Water Bottle399
Coffee Mug249

Add NOT to reverse either one: country NOT IN ('India'), price NOT BETWEEN 200 AND 500.

Pattern matching with LIKE

LIKE matches text against a pattern. % stands for “any number of characters (including none)” and _ for “exactly one character”.

SQL
SELECT name
FROM products
WHERE name LIKE '%book%';

Result: 2 rows

Query result
name
Notebook A5
Python Basics Book

Look closely: Notebook matched too, because it contains “book”. Patterns match exactly what you write, so check your results. In SQLite, LIKE ignores the difference between capital and small letters for English letters; = does not.

PatternMatches
'A%'starts with A
'%a'ends with a
'%pen%'contains pen anywhere
'_i%'second letter is i

Text is matched exactly

SQL
SELECT name, city
FROM customers
WHERE country = 'india';

Result: 0 rows

Query result
namecity
(no rows)

No rows! With =, 'india' is not the same as 'India'. An empty result is not an error, which makes this mistake easy to miss. When a filter returns nothing, check capitals, spaces and spelling first.

Practice

Guided exercise

Bulk delivered orders

Find delivered orders where the customer bought more than one of the item. Show the order id, quantity and status.

  1. Select order_id, quantity, status from orders.
  2. Filter with status = 'delivered'.
  3. Add AND quantity > 1.
Show the solution
Solution
SELECT order_id, quantity, status
FROM orders
WHERE status = 'delivered' AND quantity > 1;

Result: 4 rows

Query result
order_idquantitystatus
23delivered
42delivered
95delivered
132delivered

How to get there: Both conditions must be true, so AND is right. Order 8 has a quantity of 2 but is only shipped, so it is left out.

Your turn

Early customers

Show the name and email of customers who joined before 1 April 2025.

Show the solution
Solution
SELECT name, email
FROM customers
WHERE joined_on < '2025-04-01';

Result: 4 rows

Query result
nameemail
Asha Patelasha@example.com
Ravi Kumarravi@example.com
Meera NairNULL
John Smithjohn@example.com

How to get there: Dates written as YYYY-MM-DD compare correctly as text. < means strictly before, so someone who joined on 1 April would not be included.

Common mistakes

Double quotes around text: country = "India"

You see: Works in SQLite but fails in other databases, or is treated as a column name

Fix: Always use single quotes for text values.

Wrong capitals or extra spaces in text

You see: An empty result, no error

Fix: Match the stored value exactly, or use LIKE for a case-insensitive match.

Mixing AND and OR without brackets

You see: Extra rows you did not expect

Fix: Put brackets around the OR part.

Writing price > 200 AND < 500

You see: near "<": syntax error

Fix: Repeat the column: price > 200 AND price < 500, or use BETWEEN.

Recap

  • WHERE keeps only the rows where its condition is true.
  • Compare with =, <>, <, >, <=, >=. Text goes in single quotes.
  • AND needs both conditions, OR needs either. Use brackets when mixing them.
  • IN checks a list, BETWEEN an inclusive range, and LIKE a pattern with % and _.

Key terms

Condition
A test that is true or false for each row.
Wildcard
A symbol in a LIKE pattern: % for any characters, _ for one character.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which filter keeps products costing from 100 to 300, including both 100 and 300?

  2. 2. Which names does `WHERE name LIKE 'S%'` match?

  3. 3. `WHERE country = 'uk'` returns no rows, but there is a customer from the UK. Why?

  4. 4. Which is the same as `WHERE country = 'UAE' OR country = 'UK'`?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.