LearnContact
Lesson 2317 min read

WHERE Clause

Learn how to filter query results using the WHERE clause and combine multiple conditions with AND and OR.

Introduction

Selecting every row from a table is rarely useful once a table grows beyond a handful of rows. Most of the time, you only care about rows that meet some specific condition — students above a certain age, orders placed today, or products that are out of stock.

The WHERE clause is how SQL filters rows. It sits right after FROM and lets you describe exactly which rows should be included in the result.

What You Will Learn
  • The basic syntax of the WHERE clause.
  • How to filter rows using a single condition.
  • How to combine multiple conditions with AND and OR.
  • A worked example filtering the students table.

Basic WHERE Syntax

A WHERE clause comes after the table name and contains a condition. Only rows for which the condition is true are included in the result.

SELECT column1, column2
FROM table_name
WHERE condition;

We will keep using the same students table from the previous lesson.

idnameagegrademarks
1Alex16A92
2Sam17B74
3Priya16A88
4Jordan18C61
5Maya17B79

Filtering by One Condition

The simplest WHERE clause tests one column against a value, using an operator like = (equals) or > (greater than).

SELECT name, grade
FROM students
WHERE grade = 'A';
Result
+-------+-------+
| name  | grade |
+-------+-------+
| Alex  | A     |
| Priya | A     |
+-------+-------+

Notice that only the two rows where grade equals 'A' are returned — Sam, Jordan, and Maya are filtered out entirely.

Combining Conditions with AND

AND requires that every listed condition be true for a row to be included. This narrows the result further.

SELECT name, age, grade
FROM students
WHERE grade = 'A' AND age = 16;
Result
+-------+-----+-------+
| name  | age | grade |
+-------+-----+-------+
| Alex  | 16  | A     |
| Priya | 16  | A     |
+-------+-----+-------+

In this case both Alex and Priya happen to satisfy both conditions, so the result looks the same as before — but if any grade-A student were a different age, AND would exclude them.

Combining Conditions with OR

OR requires that at least one of the listed conditions be true. This tends to widen the result rather than narrow it.

SELECT name, age, grade
FROM students
WHERE grade = 'A' OR age = 17;
Result
+-------+-----+-------+
| name  | age | grade |
+-------+-----+-------+
| Alex  | 16  | A     |
| Sam   | 17  | B     |
| Priya | 16  | A     |
| Maya  | 17  | B     |
+-------+-----+-------+

Here, Sam and Maya are included even though their grade is not A, because they satisfy the second condition: age = 17.

Worked Example

Suppose we want every student who is either 16 years old or has marks of 80 or above — a group worth reviewing for an honors list.

honors_check.sql
SELECT name, age, marks
FROM students
WHERE age = 16 OR marks >= 80;
Result
+-------+-----+-------+
| name  | age | marks |
+-------+-----+-------+
| Alex  | 16  | 92    |
| Priya | 16  | 88    |
+-------+-----+-------+

Alex and Priya both qualify — in this data set they satisfy both conditions at once, but OR only requires satisfying at least one.

Looking Ahead

More Operators Coming Soon

This lesson only scratches the surface of what can go inside a WHERE clause. Dedicated upcoming lessons will cover comparison operators (like !=, <, <=, >=) and logical operators (including NOT and combining AND/OR with parentheses) in much more depth.

Common Mistakes

Avoid These Mistakes
  • Using a single = sign correctly is fine in MySQL, but forgetting quotes around text values (like grade = A instead of grade = 'A') causes errors.
  • Mixing up AND and OR, which can silently return far more or far fewer rows than intended.
  • Forgetting that WHERE filters rows, not columns — it does not remove columns from the result.
  • Placing WHERE before FROM, which is invalid syntax.

Best Practices

  • Always quote text and date values in conditions.
  • Test a WHERE condition on a SELECT * first to sanity-check which rows it matches.
  • Use AND when a row must satisfy every condition, and OR when any one condition is enough.
  • Keep conditions readable — break complex ones across multiple lines.

Frequently Asked Questions

Can I use WHERE with SELECT *?

Yes. WHERE works the same way regardless of which columns you select — it only affects which rows are returned.

What happens if no rows match the condition?

MySQL returns an empty result set — no error, just zero rows.

Can I combine more than two conditions?

Yes, you can chain as many AND and OR conditions as needed; the next lessons cover how parentheses control the order they are evaluated in.

Is WHERE required in every SELECT statement?

No, WHERE is optional. Without it, every row in the table is returned.

Key Takeaways

  • WHERE filters which rows a query returns, based on a condition.
  • AND requires all conditions to be true.
  • OR requires at least one condition to be true.
  • Text values in a condition must be wrapped in quotes.

Summary

The WHERE clause lets you retrieve only the rows that matter, instead of every row in a table. Combining conditions with AND and OR gives you fine control over exactly which rows qualify.

Next, you will learn how to control the order in which matching rows are returned, using ORDER BY.

Lesson 23 Completed
  • You can filter rows using a WHERE clause.
  • You can combine multiple conditions with AND and OR.
  • You know dedicated lessons on operators are coming up next.
Next Lesson →

ORDER BY