The WHERE clause is used to filter records in MySQL. It allows you to retrieve, update, or delete only the records that satisfy a specific condition.
The WHERE clause specifies a condition that records must satisfy.
SELECT *
FROM students
WHERE age = 20;
This query returns only students whose age is 20.
The basic syntax is:
SELECT column_name
FROM table_name
WHERE condition;
Example:
SELECT name
FROM students
WHERE age = 21;
The equal operator = checks whether two values are equal.
SELECT *
FROM students
WHERE course = 'Python';
This returns students enrolled in Python.
Numeric values can be compared directly.
SELECT *
FROM students
WHERE age = 25;
There is no need to put numeric values inside quotes.
Text values are normally written inside quotes.
SELECT *
FROM students
WHERE name = 'Rahul';
This returns records where the name matches the specified value.
The > operator returns values greater than the specified value.
SELECT *
FROM students
WHERE age > 18;
This returns students older than 18.
The < operator returns values less than the specified value.
SELECT *
FROM students
WHERE age < 25;
This returns students whose age is less than 25.
The >= operator checks for values greater than or equal to a specified value.
SELECT *
FROM students
WHERE age >= 18;
The <= operator checks for values less than or equal to a specified value.
SELECT *
FROM students
WHERE fee <= 15000;
The != operator checks for values that are not equal.
SELECT *
FROM students
WHERE course != 'Python';
This returns students whose course is not Python.
MySQL also supports <> as a not-equal operator.
SELECT *
FROM students
WHERE age <> 20;
This returns records where age is not 20.
The AND operator allows multiple conditions.
SELECT *
FROM students
WHERE age > 18
AND course = 'Python';
Both conditions must be true.
The OR operator returns records when at least one condition is true.
SELECT *
FROM students
WHERE course = 'Python'
OR course = 'Java';
You can use more than two conditions with AND.
SELECT *
FROM students
WHERE age > 18
AND fee > 10000
AND course = 'Python';
All three conditions must be satisfied.
AND and OR can be combined in one query.
SELECT *
FROM students
WHERE course = 'Python'
AND (age > 20 OR fee > 15000);
Parentheses help clearly define the logical condition.
BETWEEN can be used to filter values within a range.
SELECT *
FROM students
WHERE age BETWEEN 18 AND 25;
This includes values from 18 through 25.
IN checks whether a value matches one of several specified values.
SELECT *
FROM students
WHERE course IN ('Python', 'Java', 'PHP');
This returns students enrolled in any of the listed courses.
LIKE is used for pattern matching.
SELECT *
FROM students
WHERE name LIKE 'A%';
This finds names beginning with the letter A.
Use IS NULL to find records where a column contains NULL.
SELECT *
FROM students
WHERE mobile IS NULL;
Do not use = NULL to test for NULL values.
IS NOT NULL finds records where a value exists.
SELECT *
FROM students
WHERE mobile IS NOT NULL;
WHERE can be used with UPDATE to modify specific records.
UPDATE students
SET fee = 16000
WHERE id = 1;
Only the student with ID 1 is updated.
WHERE can be used with DELETE to remove specific records.
DELETE FROM students
WHERE id = 5;
Only the record with ID 5 is deleted.
WHERE can also filter decimal values.
SELECT *
FROM students
WHERE fee > 10000.00;
This returns students whose fee is greater than 10,000.
WHERE can be used to filter date values.
SELECT *
FROM students
WHERE admission_date = '2026-09-21';
This returns students admitted on the specified date.
WHERE can be combined with ORDER BY.
SELECT name, course, fee
FROM students
WHERE fee > 10000
ORDER BY fee DESC;
First the records are filtered, then the matching records are sorted.
WHERE can also be combined with LIMIT.
SELECT *
FROM students
WHERE course = 'Python'
LIMIT 5;
This returns up to five Python students.
Suppose we want students who are older than 20 and enrolled in Python.
SELECT id, name, mobile, course
FROM students
WHERE age > 20
AND course = 'Python';
A simple WHERE workflow is:
SELECT name, course, fee
FROM students
WHERE fee > 10000
AND age >= 18
ORDER BY fee DESC
LIMIT 5;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
course VARCHAR(100),
age INT,
fee DECIMAL(10,2)
);
INSERT INTO students
(name, course, age, fee)
VALUES
('Rahul', 'Python', 22, 15000.00),
('Priya', 'Java', 21, 18000.00),
('Amit', 'Python', 24, 12000.00),
('Neha', 'PHP', 20, 10000.00);
SELECT id, name, course, fee
FROM students
WHERE course = 'Python'
AND fee > 10000
ORDER BY fee DESC;
This query finds Python students whose fee is greater than 10,000 and displays them in descending fee order.
Question: Which clause is used to filter records in a MySQL query?