Velamuvi AIAcademy

Lesson 3 of 17 · First Queries

Filtering With WHERE

Filter rows in SQL with WHERE. Learn comparison operators, AND, OR and NOT, and the IN, BETWEEN and LIKE shortcuts, with queries you can run.

  • 9 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • How to keep only the rows that match a condition
  • The comparison operators
  • How to combine conditions with AND, OR and NOT
  • The IN, BETWEEN and LIKE shortcuts

Asking for some rows, not all

SELECT chooses columns. WHERE chooses rows. It comes after FROM and holds a condition. The database checks the condition against every row and keeps only the rows where it is true.

Try it yourselfSQL
SELECT name, country, age
FROM students
WHERE country = 'India';

Twelve students went in. Two came out.

Comparison operators

OperatorMeaningExample
=Equal tocountry = 'India'
<>Not equal tocountry <> 'India'
>Greater thanage > 25
<Less thanage < 25
>=Greater than or equal toage >= 25
<=Less than or equal toage <= 25
Try it yourselfSQL
SELECT name, age
FROM students
WHERE age >= 25;

Numbers are written as they are. Text and dates go in single quotes.

Try it yourselfSQL
SELECT name, joined
FROM students
WHERE joined >= '2026-04-01';

Dates written as year-month-day compare correctly, which is one reason that format is used everywhere in data work.

Combining conditions

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

Try it yourselfSQL
SELECT name, country, age
FROM students
WHERE age < 25 AND country = 'India';
Try it yourselfSQL
SELECT name, country
FROM students
WHERE country = 'Japan' OR country = 'Brazil';

NOT reverses a condition.

Try it yourselfSQL
SELECT title, category
FROM courses
WHERE NOT category = 'Programming';

When you mix AND and OR, use brackets to make your meaning clear. AND is applied before OR, which often surprises people.

Try it yourselfSQL
SELECT name, country, age
FROM students
WHERE (country = 'India' OR country = 'Spain') AND age > 22;

Remove the brackets and run it again. You get an extra row, because the query now means "India, or Spain and older than 22".

IN: matching a list

IN is a shorter way to write several OR conditions on the same column.

Try it yourselfSQL
SELECT name, country
FROM students
WHERE country IN ('India', 'Japan', 'Brazil');

BETWEEN: matching a range

BETWEEN includes both ends of the range.

Try it yourselfSQL
SELECT title, hours
FROM courses
WHERE hours BETWEEN 8 AND 10;

LIKE: matching part of a text

LIKE compares text with a pattern. Two special characters do the work:

  • % stands for any number of characters, including none
  • _ stands for exactly one character
Try it yourselfSQL
SELECT name FROM students WHERE name LIKE 'A%';      -- starts with A

SELECT title FROM courses WHERE title LIKE '%for%';  -- contains "for"

SELECT name FROM students WHERE name LIKE '_a%';     -- second letter is a

Common mistakes

Practice exercise

List the name and age of students who are older than 22 and younger than 28.

Your solutionSQL
-- Show the name and age of every student who is
-- older than 22 AND younger than 28
Knowledge check

Which rows does WHERE hours BETWEEN 8 AND 10 keep?

Summary

  • WHERE keeps only the rows that match a condition.
  • Compare with =, <>, >, <, >= and <=.
  • Combine conditions with AND, OR and NOT, and use brackets when mixing them.
  • IN matches a list, BETWEEN a range and LIKE a text pattern.

Next: putting the result in the order you want.

Finished reading? Tick it off to track your progress.