← Back to MySQL Course | Chapter 8: Operators & Conditional Logic | Lesson 3 of 5

Logical Operators

Logical operators and, or, not जैसे छोटे words हैं जो आपको कई rules को एक बड़े question में combine करने देते हैं।
Syntax
sql
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition

AND Operator

AND operator एक WHERE clause में conditions combine करता है। यह सिर्फ तभी true return करता है जब सभी individual conditions true हों, इसलिए ज़्यादा AND conditions add करना सिर्फ आपके results को narrow कर सकता है, कभी widen नहीं।

उदाहरण: The AND Operator

sql
CREATE TABLE products (id INT, price INT, stock INT);
INSERT INTO products VALUES (1, 20, 5), (2, 80, 5), (3, 20, 0);
SELECT * FROM products WHERE price = 20 AND stock > 0;

OR Operator

OR operator कई conditions evaluate करता है। यह true return करता है अगर conditions में से कम से कम एक true हो, इसलिए यह कई acceptable values या states में से किसी से match करने के लिए उपयोगी है।

उदाहरण: The OR Operator

sql
CREATE TABLE products (id INT, category TEXT);
INSERT INTO products VALUES (1, 'Books'), (2, 'Toys'), (3, 'Food');
SELECT * FROM products WHERE category = 'Books' OR category = 'Toys';

NOT Operator

NOT operator किसी condition की truth value reverse कर देता है। यह true को false, और false को true बना देता है, जो पूरी condition को इसके opposite की तरह फिर से लिखे बिना एक specific case exclude करने के लिए handy है।

उदाहरण: The NOT Operator

sql
CREATE TABLE products (id INT, category TEXT);
INSERT INTO products VALUES (1, 'Books'), (2, 'Toys'), (3, 'Food');
SELECT * FROM products WHERE NOT category = 'Food';

XOR Operator

XOR operator exclusive OR के लिए है। यह true return करता है अगर exactly एक condition true हो, दोनों नहीं, जो 'either but not both' logic के लिए उपयोगी है जिसे plain OR directly express नहीं कर सकता।

उदाहरण: The XOR Operator

sql
CREATE TABLE users (id INT, has_email INT, has_phone INT);
INSERT INTO users VALUES (1, 1, 0), (2, 1, 1), (3, 0, 0);
SELECT * FROM users WHERE has_email XOR has_phone;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Logical Operator Precedence

SQL एक WHERE clause में AND, OR, और NOT combine करते समय operators को एक specific order में evaluate करता है। NOT पहले evaluate होता है, फिर AND, और आख़िर में OR। इस precedence को explicitly बदलने के लिए parentheses इस्तेमाल करें बजाय reader के default order याद रखने पर भरोसा करने के।

उदाहरण: Logical Operator Precedence

sql
CREATE TABLE products (id INT, category TEXT, price INT);
INSERT INTO products VALUES (1, 'Books', 20), (2, 'Toys', 80), (3, 'Food', 20);
SELECT * FROM products WHERE category = 'Books' OR category = 'Toys' AND price > 50;
SELECT * FROM products WHERE (category = 'Books' OR category = 'Toys') AND price > 50;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना parentheses के AND और OR mix करना, ताकि precedence गलत rows दे।
  2. यह उम्मीद करना कि XOR तब true है जब दोनों conditions true हों, जबकि इसे exactly एक चाहिए।
  3. && या || इस्तेमाल करना और string joining की उम्मीद करना, जबकि MySQL में || default रूप से OR है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.