SQL में Comparison और Logical Operators
Operators क्यों जरूरी हैं
Operators stored values को decisions में बदलते हैं। WHERE condition, JOIN condition, CHECK constraint और CASE expression सभी सही comparison तथा logical grouping पर depend करते हैं। Query syntax सही होते हुए भी गलत students लौटा सकती है यदि logic गलत group हुआ हो।
students में Aarav (X-A, 86.50, Active), Meera (X-A, 91.00, Active), Kabir (X-B, 74.00, Inactive) और Sana (X-B, 88.50, Active) हैं।SELECT full_name, marks
FROM students
WHERE marks >= 85
ORDER BY marks DESC;Comparison Operators
| Operator | अर्थ | Example |
|---|---|---|
= | बराबर | status = 'Active' |
<>, != | बराबर नहीं | class_name <> 'X-A' |
>, >= | बड़ा, कम से कम | marks >= 80 |
<, <= | छोटा, अधिकतम | marks < 75 |
<=> | MySQL NULL-safe equality | marks <=> NULL |
Compatible types compare करें। Quoted numeric strings implicit conversion कराके dirty data छिपा सकती हैं। Marks DECIMAL, dates DATE/TIMESTAMP में रखें और matching values से compare करें।
AND, OR और NOT
SELECT full_name, class_name, marks
FROM students
WHERE status = 'Active'
AND (class_name = 'X-A' OR marks >= 90)
ORDER BY student_id;| A | B | A AND B | A OR B |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
NOT condition उलटता है, लेकिन positive form अधिक clear होता है: status = 'Inactive', NOT status = 'Active' से आसान है।
Precedence और Parentheses
MySQL comparison operators, फिर NOT, फिर AND और अंत में OR evaluate करता है। इसलिए ये समान नहीं हैं:
-- सभी X-A, साथ में active X-B
WHERE class_name = 'X-A'
OR class_name = 'X-B' AND status = 'Active'
-- दोनों classes के केवल active students
WHERE (class_name = 'X-A' OR class_name = 'X-B')
AND status = 'Active'
NULL का Three-Valued Logic
SQL condition TRUE, FALSE या UNKNOWN हो सकती है। NULL के साथ ordinary comparison सामान्यतः UNKNOWN देता है और WHERE केवल TRUE रखता है।
-- गलत: NULL marks नहीं मिलते
SELECT * FROM students WHERE marks = NULL;
-- सही
SELECT * FROM students WHERE marks IS NULL;
-- MySQL-specific NULL-safe comparison
SELECT * FROM students WHERE marks <=> NULL;
NOT (marks = 80) भी NULL marks शामिल नहीं करता, क्योंकि NOT UNKNOWN भी UNKNOWN रहता है। Missing data result में चाहिए या नहीं, explicitly तय करें।
गलतियाँ और अभ्यास
= NULLलिखना।- AND/OR को parentheses बिना मिलाना।
- Numbers को formatted text की तरह compare करना।
- UPDATE/DELETE को non-unique name से target करना।
- Column collation जाँचे बिना text comparison assume करना।
- कम से कम 80 marks वाले active X-B students लौटाएँ।
- Same rule NOT से लिखकर clearer version बताएँ।
- ऊपर के precedence examples का difference predict करें।
- A = UNKNOWN वाली truth-table row बनाएँ।
त्वरित सारांश
- Comparison operators values test और logical operators tests combine करते हैं।
- AND की precedence OR से अधिक, पर parentheses safest हैं।
- NULL UNKNOWN लाता और IS NULL/IS NOT NULL माँगता है।
- Compatible data types तथा precise key conditions उपयोग करें।
Official संदर्भ
- Comparison Functions and Operators — MySQL 8.4
- Logical Operators — MySQL 8.4
- Operator Precedence — MySQL 8.4
References 14 August 2026 को review किए गए। Examples common tutorial dataset उपयोग करते हैं।