HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL WHERE Clause

The WHERE clause filters which rows a query acts on. Without it, a SELECT returns every row, an UPDATE changes every row, and a DELETE removes every row. With WHERE, you narrow the operation down to only the rows that match a specific condition.


Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

The same WHERE clause works identically with UPDATE and DELETE.


Sample Table

idnamecityage
1Rahim UddinDhaka22
2Sara BegumChittagong20
3Karim AliSylhet23
4Nila AkterDhaka21
5Rafi HossainRajshahi24

Comparison Operators

The WHERE clause supports these comparison operators:

OperatorMeaningExample
=Equal toWHERE city = 'Dhaka'
!= or <>Not equal toWHERE city != 'Dhaka'
>Greater thanWHERE age > 21
<Less thanWHERE age < 23
>=Greater than or equalWHERE age >= 22
<=Less than or equalWHERE age <= 21

Text Conditions

String values must be wrapped in single quotes. The comparison is case-insensitive by default in MySQL:

-- Exact match on city
SELECT * FROM students WHERE city = 'Dhaka';
idnamecityage
1Rahim UddinDhaka22
4Nila AkterDhaka21
-- Rows where city is NOT Dhaka
SELECT * FROM students WHERE city != 'Dhaka';

Numeric Conditions

Numbers do not need quotes:

-- Students older than 21
SELECT name, age FROM students WHERE age > 21;
nameage
Rahim Uddin22
Karim Ali23
Rafi Hossain24

Date Conditions

Dates must be wrapped in single quotes and written in YYYY-MM-DD format:

-- Orders placed after a specific date
SELECT * FROM orders WHERE order_date > '2025-01-01';
-- Records from exactly one date
SELECT * FROM orders WHERE order_date = '2025-06-15';

WHERE with UPDATE and DELETE

The WHERE clause works identically in UPDATE and DELETE:

-- Update only Karim Ali's row
UPDATE students SET city = 'Barishal' WHERE id = 3;

-- Delete only students under 21
DELETE FROM students WHERE age < 21;
Remember: Always include WHERE in your UPDATE and DELETE statements. Without it, the operation applies to every row in the table.

Key Points to Remember

  • WHERE filters rows before the query acts on them — only matching rows are returned, updated, or deleted.
  • String values require single quotes; numbers and booleans do not.
  • Date values require single quotes in YYYY-MM-DD format.
  • String comparisons are case-insensitive by default in MySQL.
  • Both != and <> mean "not equal to" — they are interchangeable.
  • WHERE works the same way in SELECT, UPDATE, and DELETE.
What's next? The next lesson covers AND, OR, NOT — how to combine multiple conditions in a single WHERE clause.