The WHERE clause is used to filter records in a SQL query. It allows you to retrieve only the rows that satisfy a specific condition.
Suppose a Students table contains many records. If you want to find only students who are 20 years old, you can use WHERE.
SELECT *
FROM Students
WHERE age = 20;
Only records where the age is 20 will be returned.
SELECT column1, column2
FROM table_name
WHERE condition;
The condition determines which records should be returned.
SELECT *
FROM Students
WHERE age = 21;
This retrieves all columns for students whose age is 21.
SELECT name
FROM Students
WHERE age = 21;
Only the names of students whose age is 21 are returned.
Text values are normally written inside single quotes.
SELECT *
FROM Students
WHERE course = 'Python';
This retrieves students enrolled in Python.
SELECT *
FROM Students
WHERE age = 20;
Numeric values normally do not require quotation marks.
The = operator checks whether two values are equal.
SELECT *
FROM Students
WHERE age = 20;
SELECT *
FROM Students
WHERE age > 20;
This returns students whose age is greater than 20.
SELECT *
FROM Students
WHERE age < 20;
This returns students whose age is less than 20.
SELECT *
FROM Students
WHERE age >= 20;
This returns students whose age is 20 or greater.
SELECT *
FROM Students
WHERE age <= 20;
This returns students whose age is 20 or less.
The <> operator means not equal.
SELECT *
FROM Students
WHERE course <> 'Python';
This returns students whose course is not Python.
In MySQL, != can also be used for not equal.
SELECT *
FROM Students
WHERE age != 20;
Use AND when all specified conditions must be true.
SELECT *
FROM Students
WHERE age > 18
AND course = 'Python';
Both conditions must be satisfied.
Use OR when at least one condition should be true.
SELECT *
FROM Students
WHERE course = 'Python'
OR course = 'Java';
The NOT operator reverses a condition.
SELECT *
FROM Students
WHERE NOT course = 'Python';
This returns students whose course is not Python.
The BETWEEN operator checks whether a value is within a range.
SELECT *
FROM Students
WHERE age BETWEEN 18 AND 25;
The boundary values are included.
The IN operator allows multiple possible values.
SELECT *
FROM Students
WHERE course IN ('Python', 'Java', 'PHP');
The LIKE operator 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 has a NULL value.
SELECT *
FROM Students
WHERE mobile IS NULL;
SELECT *
FROM Students
WHERE mobile IS NOT NULL;
This returns records where the mobile column contains a non-NULL value.
WHERE can be combined with ORDER BY.
SELECT *
FROM Students
WHERE age > 18
ORDER BY age DESC;
First the records are filtered, then the result is sorted.
SELECT *
FROM Students
WHERE course = 'Python'
LIMIT 5;
In MySQL, this returns up to five matching records.
WHERE is very important when updating specific records.
UPDATE Students
SET fee = 18000
WHERE id = 1;
Only the student whose ID is 1 will be updated.
WHERE can also be used to delete a specific record.
DELETE FROM Students
WHERE id = 5;
Only the record with ID 5 is targeted.
You can combine several conditions.
SELECT *
FROM Students
WHERE age >= 18
AND course = 'Python'
AND city = 'Patna';
All three conditions must be satisfied.
Parentheses can make complex conditions easier to understand and control the logical grouping.
SELECT *
FROM Students
WHERE age > 18
AND (course = 'Python' OR course = 'Java');
This means the student must be older than 18 and must be enrolled in either Python or Java.
WHERE can also filter decimal values such as fees.
SELECT *
FROM Students
WHERE fee > 10000;
This returns students whose fee is greater than 10000.
You can filter records using date values.
SELECT *
FROM Students
WHERE admission_date = '2026-09-20';
This returns records with the specified admission date.
SELECT name, course, fee
FROM Students
WHERE age >= 18
AND fee > 5000
AND course IN ('Python', 'Java')
ORDER BY fee DESC
LIMIT 10;
This query:
Question: Which SQL clause is used to filter records according to a condition?