Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 48

Comparison and Logical Operators in SQL

Why Operators Matter

Operators turn stored values into decisions. A WHERE condition, JOIN condition, CHECK constraint and CASE expression all depend on correct comparisons and logical grouping. A query can be syntactically valid yet return the wrong students when its logic is grouped incorrectly.

Common learning data: students contains Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) and Sana (X-B, 88.50, Active).
SELECT full_name, marks
FROM students
WHERE marks >= 85
ORDER BY marks DESC;
Result Meera | 91.00 Sana | 88.50 Aarav | 86.50

Comparison Operators

OperatorMeaningExample
=equalstatus = 'Active'
<>, !=not equalclass_name <> 'X-A'
>, >=greater than, at leastmarks >= 80
<, <=less than, at mostmarks < 75
<=>MySQL NULL-safe equalitymarks <=> NULL

Compare compatible types. Quoted numeric strings can trigger implicit conversion and hide dirty data. Store marks as DECIMAL, dates as DATE/TIMESTAMP and compare them with matching values.

AND, OR and NOT

SELECT full_name, class_name, marks
FROM students
WHERE status = 'Active'
  AND (class_name = 'X-A' OR marks >= 90)
ORDER BY student_id;
Result Aarav | X-A | 86.50 Meera | X-A | 91.00
ABA AND BA OR B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE

NOT reverses a condition, but a positive form is often clearer: status = 'Inactive' is easier to review than NOT status = 'Active'.

Operator Precedence and Parentheses

MySQL evaluates comparison operators before NOT, NOT before AND, and AND before OR. Therefore these statements are not equivalent:

-- X-A students, plus active X-B students
WHERE class_name = 'X-A'
   OR class_name = 'X-B' AND status = 'Active'

-- Only active students from either class
WHERE (class_name = 'X-A' OR class_name = 'X-B')
  AND status = 'Active'
Faculty rule: whenever AND and OR appear together, add parentheses that express the spoken business rule. Do not depend on the reader remembering precedence.

Three-Valued Logic with NULL

SQL conditions can be TRUE, FALSE or UNKNOWN. Any ordinary comparison with NULL generally becomes UNKNOWN, and WHERE keeps only TRUE.

-- Wrong: returns no NULL marks
SELECT * FROM students WHERE marks = NULL;

-- Correct
SELECT * FROM students WHERE marks IS NULL;

-- MySQL-specific NULL-safe comparison
SELECT * FROM students WHERE marks <=> NULL;

Also remember that NOT (marks = 80) does not include NULL marks, because NOT UNKNOWN remains UNKNOWN. Decide explicitly whether missing data belongs in the result.

Common Mistakes and Practice

  • Using = NULL instead of IS NULL.
  • Mixing AND/OR without parentheses.
  • Comparing numbers as formatted text.
  • Using a non-unique name to target UPDATE/DELETE.
  • Assuming collation-sensitive text comparison without checking the column collation.
  1. Return active X-B students with marks at least 80.
  2. Write the same rule with NOT and explain which version is clearer.
  3. Predict the difference between the two precedence examples above.
  4. Create a truth table row where A is UNKNOWN.

Quick Summary

  • Comparison operators test values; logical operators combine tests.
  • AND has higher precedence than OR, but explicit parentheses are safest.
  • NULL introduces UNKNOWN and requires IS NULL/IS NOT NULL.
  • Use compatible data types and precise key conditions.

Official References

References reviewed 14 August 2026. Examples use the common tutorial dataset.

Frequently Asked Questions

What are comparison operators in SQL?
They compare two expressions and return TRUE, FALSE or, when NULL is involved, UNKNOWN. Common operators are =, <>, !=, >, >=, < and <=.
What is the difference between AND and OR?
AND is TRUE only when both required conditions are TRUE. OR is TRUE when at least one condition is TRUE. Use parentheses when mixing them.
Which has higher precedence, AND or OR?
In MySQL, AND has higher precedence than OR. Parentheses are still recommended because they make business intent explicit.
Why does column = NULL not work?
NULL means unknown, so equality with NULL produces UNKNOWN rather than TRUE. Use IS NULL or IS NOT NULL.
Are != and <> the same in MySQL?
Both mean not equal in MySQL. The <> form is standard SQL and is usually preferred for portable SQL.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.