Lesson 22 of 60 – Comparison Operators
37%

MySQL Comparison Operators

Comparison operators are used to compare two values in MySQL. They are commonly used with the WHERE clause to filter records based on specific conditions.

Note: Comparison operators help you find records based on conditions such as equal to, greater than, less than, and not equal to.

1. What are Comparison Operators?

Comparison operators compare one value with another value.

SELECT *
FROM students
WHERE age > 20;

Here, > compares the age column with 20.

2. Common Comparison Operators

Operator Meaning
= Equal to
!= Not equal to
<> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

3. Equal To (=)

The = operator checks whether two values are equal.

SELECT *
FROM students
WHERE age = 20;

This returns students whose age is exactly 20.

4. Equal To with Text

The equal operator can also compare text values.

SELECT *
FROM students
WHERE course = 'Python';

This returns students whose course is Python.

5. Not Equal To (!=)

The != operator checks whether two values are different.

SELECT *
FROM students
WHERE age != 20;

This returns students whose age is not 20.

6. Not Equal To (<>)

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

SELECT *
FROM students
WHERE course <> 'Python';

This returns students whose course is not Python.

7. Greater Than (>)

The > operator finds values greater than a specified value.

SELECT *
FROM students
WHERE age > 18;

This returns students older than 18.

8. Less Than (<)

The < operator finds values less than a specified value.

SELECT *
FROM students
WHERE age < 25;

This returns students younger than 25.

9. Greater Than or Equal To (>=)

The >= operator includes the specified value as well as larger values.

SELECT *
FROM students
WHERE age >= 18;

This returns students whose age is 18 or greater.

10. Less Than or Equal To (<=)

The <= operator includes the specified value as well as smaller values.

SELECT *
FROM students
WHERE age <= 25;

This returns students whose age is 25 or less.

11. Comparison with Decimal Values

Comparison operators can be used with decimal values.

SELECT *
FROM students
WHERE fee > 10000.00;

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

12. Comparing Dates

Dates can also be compared using comparison operators.

SELECT *
FROM students
WHERE admission_date > '2026-01-01';

This returns students whose admission date is after January 1, 2026.

13. Comparing Text Values

Text columns can be compared with text values.

SELECT *
FROM students
WHERE name = 'Rahul';

This finds records with the specified name.

14. Comparison with WHERE

Comparison operators are frequently used inside WHERE.

SELECT name, age, fee
FROM students
WHERE fee >= 15000;

Only records satisfying the condition are returned.

15. Comparison with AND

You can combine comparison operators using AND.

SELECT *
FROM students
WHERE age > 18
AND fee > 10000;

Both conditions must be true.

16. Comparison with OR

OR allows either comparison condition to be true.

SELECT *
FROM students
WHERE age < 18
OR fee > 20000;

A record is returned if at least one condition is satisfied.

17. Multiple Comparisons

Several comparison operators can be used in one query.

SELECT *
FROM students
WHERE age >= 18
AND age <= 25
AND fee > 10000;

This finds students aged from 18 to 25 whose fee is greater than 10,000.

18. Comparison with NOT

NOT can reverse a comparison condition.

SELECT *
FROM students
WHERE NOT age > 20;

This selects records where the condition age > 20 is not true.

19. Comparison with NULL

Normal comparison operators should not be used to test whether a value is NULL.

Incorrect:

SELECT *
FROM students
WHERE mobile = NULL;

Correct:

SELECT *
FROM students
WHERE mobile IS NULL;

20. Comparison with IS NOT NULL

Use IS NOT NULL when you want records containing a value.

SELECT *
FROM students
WHERE mobile IS NOT NULL;

21. Comparison Operators with UPDATE

Comparison operators can be used with UPDATE to select specific records.

UPDATE students
SET fee = fee + 1000
WHERE fee < 15000;

This increases the fee for students whose current fee is less than 15,000.

22. Comparison Operators with DELETE

Comparison operators can also be used with DELETE.

DELETE FROM students
WHERE age < 18;

This removes records where the age is less than 18.

Warning: Always test the condition using SELECT before deleting records.

23. Comparison Operators with ORDER BY

Comparison conditions can be combined with sorting.

SELECT name, course, fee
FROM students
WHERE fee > 10000
ORDER BY fee DESC;

First, records are filtered and then sorted by fee.

24. Comparison Operators with LIMIT

LIMIT can restrict the number of matching records.

SELECT *
FROM students
WHERE age > 18
LIMIT 5;

This returns up to five students older than 18.

25. Comparison Operators with BETWEEN

BETWEEN is useful when you need to compare a value against a range.

SELECT *
FROM students
WHERE age BETWEEN 18 AND 25;

This includes both boundary values.

26. Comparison Operators with IN

IN can be used when comparing a column with multiple possible values.

SELECT *
FROM students
WHERE age IN (18, 20, 22, 25);

This returns records whose age matches one of the listed values.

27. Comparison Operators with LIKE

LIKE performs pattern matching and is commonly used with text columns.

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

This finds names that begin with A.

28. Common Comparison Operator Mistakes

  • Confusing = with !=
  • Using = NULL instead of IS NULL
  • Forgetting quotes around text values
  • Using the wrong comparison operator
  • Forgetting that > does not include the boundary value
  • Forgetting that >= includes the boundary value
  • Using UPDATE or DELETE without checking the condition

29. Practical Student Search

Suppose we want students whose age is at least 18 and whose fee is less than or equal to 20,000.

SELECT id, name, course, age, fee
FROM students
WHERE age >= 18
AND fee <= 20000
ORDER BY fee DESC;

This query uses two comparison operators together.

30. Complete Comparison Operators 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', 17, 10000.00),
('Ravi', 'JavaScript', 26, 22000.00);

SELECT id, name, course, age, fee
FROM students
WHERE age >= 18
AND fee <= 20000
AND course != 'PHP'
ORDER BY fee DESC;

This query uses >=, <=, and != to filter student records.

📌 Key Points

  • = means equal to.
  • != and <> mean not equal to.
  • > means greater than.
  • < means less than.
  • >= means greater than or equal to.
  • <= means less than or equal to.
  • Comparison operators are commonly used with WHERE.
  • They can be combined with AND, OR, and NOT.
  • Use IS NULL and IS NOT NULL for NULL values.
  • Always check conditions carefully before UPDATE or DELETE.

🧠 Quick Quiz

Question: Which operator means "greater than or equal to" in MySQL?