Lesson 19 of 60 – WHERE
32%

MySQL WHERE

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.

Note: WHERE is commonly used with SELECT, UPDATE, and DELETE statements to work with specific records instead of the entire table.

1. What is WHERE?

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.

2. Basic WHERE Syntax

The basic syntax is:

SELECT column_name
FROM table_name
WHERE condition;

Example:

SELECT name
FROM students
WHERE age = 21;

3. WHERE with Equal (=)

The equal operator = checks whether two values are equal.

SELECT *
FROM students
WHERE course = 'Python';

This returns students enrolled in Python.

4. WHERE with Numeric Values

Numeric values can be compared directly.

SELECT *
FROM students
WHERE age = 25;

There is no need to put numeric values inside quotes.

5. WHERE with Text Values

Text values are normally written inside quotes.

SELECT *
FROM students
WHERE name = 'Rahul';

This returns records where the name matches the specified value.

6. WHERE with Greater Than (>)

The > operator returns values greater than the specified value.

SELECT *
FROM students
WHERE age > 18;

This returns students older than 18.

7. WHERE with Less Than (<)

The < operator returns values less than the specified value.

SELECT *
FROM students
WHERE age < 25;

This returns students whose age is less than 25.

8. WHERE with Greater Than or Equal (>=)

The >= operator checks for values greater than or equal to a specified value.

SELECT *
FROM students
WHERE age >= 18;

9. WHERE with Less Than or Equal (<=)

The <= operator checks for values less than or equal to a specified value.

SELECT *
FROM students
WHERE fee <= 15000;

10. WHERE with Not Equal (!=)

The != operator checks for values that are not equal.

SELECT *
FROM students
WHERE course != 'Python';

This returns students whose course is not Python.

11. WHERE with Not Equal (<>)

MySQL also supports <> as a not-equal operator.

SELECT *
FROM students
WHERE age <> 20;

This returns records where age is not 20.

12. WHERE with AND

The AND operator allows multiple conditions.

SELECT *
FROM students
WHERE age > 18
AND course = 'Python';

Both conditions must be true.

13. WHERE with OR

The OR operator returns records when at least one condition is true.

SELECT *
FROM students
WHERE course = 'Python'
OR course = 'Java';

14. WHERE with Multiple AND Conditions

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.

15. WHERE with AND and OR

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.

16. WHERE with BETWEEN

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.

17. WHERE with IN

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.

18. WHERE with LIKE

LIKE is used for pattern matching.

SELECT *
FROM students
WHERE name LIKE 'A%';

This finds names beginning with the letter A.

19. WHERE with NULL

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.

20. WHERE with IS NOT NULL

IS NOT NULL finds records where a value exists.

SELECT *
FROM students
WHERE mobile IS NOT NULL;

21. WHERE with UPDATE

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.

22. WHERE with DELETE

WHERE can be used with DELETE to remove specific records.

DELETE FROM students
WHERE id = 5;

Only the record with ID 5 is deleted.

23. WHERE with Decimal Values

WHERE can also filter decimal values.

SELECT *
FROM students
WHERE fee > 10000.00;

This returns students whose fee is greater than 10,000.

24. WHERE with Dates

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.

25. WHERE with ORDER BY

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.

26. WHERE with LIMIT

WHERE can also be combined with LIMIT.

SELECT *
FROM students
WHERE course = 'Python'
LIMIT 5;

This returns up to five Python students.

27. Practical Student Search

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';

28. Common WHERE Mistakes

  • Forgetting the WHERE condition
  • Using incorrect column names
  • Forgetting quotes around text values
  • Using = NULL instead of IS NULL
  • Writing an invalid comparison operator
  • Using AND when OR is required
  • Using OR when all conditions must be true

29. WHERE Query Workflow

A simple WHERE workflow is:

  1. Select the required columns.
  2. Specify the table.
  3. Write the WHERE keyword.
  4. Write the condition.
  5. Add AND or OR when necessary.
  6. Add ORDER BY or LIMIT if required.
SELECT name, course, fee
FROM students
WHERE fee > 10000
AND age >= 18
ORDER BY fee DESC
LIMIT 5;

30. Complete WHERE Example

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.

📌 Key Points

  • WHERE is used to filter records.
  • WHERE can be used with SELECT, UPDATE, and DELETE.
  • Comparison operators include =, !=, <>, >, <, >=, and <=.
  • AND requires multiple conditions to be true.
  • OR allows at least one condition to be true.
  • BETWEEN filters values within a range.
  • IN checks against multiple possible values.
  • LIKE is used for pattern matching.
  • Use IS NULL and IS NOT NULL for NULL values.
  • WHERE should be used carefully with UPDATE and DELETE.

🧠 Quick Quiz

Question: Which clause is used to filter records in a MySQL query?