MySQL
MySQL

WHERE

WHERE filters which rows a query returns, based on a condition. Without it, SELECT returns every row; with it, only rows where the condition is true are included. SELECT * FROM students WHERE age > 16; returns only students older than 16. SELECT * FROM students WHERE name = "Ali"; returns only the row (or rows) where name exactly equals "Ali". Text values are wrapped in quotes ("Ali"), while numbers are not (16). This distinction matters comparing a number column to a quoted string, or vice versa, can cause unexpected results. WHERE can use several comparison operators (=, >, <, and more, covered in the next dedicated topic), and can combine multiple conditions using AND / OR, which you'll cover shortly under logical operators. For now, focus on the core idea: WHERE decides which rows make it into the result.

Why this matters

  • ·WHERE lets a query return exactly the rows that matter, not everything in the table.
  • ·Filtering happens on the server, which is far faster than pulling every row and filtering it yourself elsewhere.
  • ·A clear WHERE condition makes a query self-documenting it's obvious what the query is asking for.

Common mistakes

  • ·Forgetting WHERE entirely on an UPDATE or DELETE (covered soon) affects every row in the table this is one of the most damaging beginner mistakes in SQL.
  • ·Comparing text without matching case or exact spelling can miss rows you expected to match.
  • ·A typo in a column name inside WHERE causes an error rather than silently being ignored.
Your codePHP
Expected output
+----+------+-----+
| id | name | age |
+----+------+-----+
| 2  | Sara | 17  |
+----+------+-----+
1 row in set

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Frequently asked questions

SELECT returns every row in the table, with no filtering at all.

Practice questions

Question 1

Write a query that returns only students younger than 17.

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Question 2

Write a query that returns only the student named "Sara".

Your codePHP
Expected output

PHP runs on a server, not in the browser this shows the expected output instead of a live one.

Quick check

Just for this topic answer honestly, then check yourself.

1 / 3

What does WHERE do?