SQL Logic — AND OR NOT — Vista Academy

SQL में Logical Operators — AND, OR, NOT की समझ और Precedence

जानिए SQL AND, OR और NOT कैसे काम करते हैं, उनके precedence (NOT > AND > OR), truth-table और practical WHERE clause उदाहरण — आसान हिंदी + runnable SQL queries. (Targets: sql and or not, logical operators in sql, and or precedence sql.)

TL;DR (हिंदी में)

NOT सबसे पहले लागू होता है, फिर AND, और अंत में OR — इसलिए हमेशा parentheses का सही उपयोग करें। उदाहरण: WHERE NOT (x AND y) OR z का अर्थ precedence के कारण अलग हो सकता है।

sql and or not logical operators in sql and or precedence sql
Quick note (हिंदी):
AND तभी true जब सभी conditions true हों। OR तभी true जब किसी भी एक condition का परिणाम true हो। NOT condition का उल्टा देता है।

🧠 SQL में Logical Operators क्या हैं?

SQL logical operators — AND, OR और NOT — का प्रयोग WHERE क्लॉज़ में कई conditions को मिलाने के लिए होता है। ये operators queries को filter करने और परिणामों की शर्तों को नियंत्रित करने में मदद करते हैं।

Keywords: sql and or not, logical operators in sql, and or precedence.

⚖️ Precedence (Evaluation Order)

नियम (सबसे पहले लागू): NOT > AND > OR. अगर आप चाहें कि कोई हिस्सा पहले आकलित हो तो parentheses का उपयोग करें। उदाहरण: WHERE NOT A AND B OR C का अर्थ अलग होगा बनाम WHERE NOT (A AND (B OR C)).

— precedence example
SELECT * FROM orders WHERE NOT is_cancelled AND amount > 100 OR priority = ‘high’;

📊 Truth Tables (Quick)

Operator Example Result
ANDA = true AND B = truetrue
ORA = false OR B = truetrue
NOTNOT (A = true)false
CombinedNOT A AND B OR CEvaluate using precedence

🧾 SQL Examples (प्रयोग)

— Simple AND
SELECT * FROM students WHERE class = ’10’ AND passed = true;

— OR with parentheses
SELECT * FROM orders WHERE (status = ‘shipped’ OR status = ‘delivered’) AND amount > 500;

— NOT example
SELECT * FROM users WHERE NOT is_active;

⚠️ Common mistakes & Why it doesn’t work

  • Missing parentheses: Precedence causes unexpected results—use () to be explicit.
  • NULL interactions: Comparisons with NULL are unknown — use IS NULL / IS NOT NULL.
  • Operator confusion: Don’t mix logical operators without understanding evaluation order.

📌 TL;DR (Quick)

Precedence: NOT > AND > OR. Use parentheses and check NULL behaviour. Examples above show common patterns.

© Vista Academy • Content updated 2025 • Vista theme — dark, golden & elegant UX for learners.

⚖️ SQL Logical Operator Precedence (AND vs OR vs NOT)

SQL में जब आप AND, OR और NOT एक ही query में इस्तेमाल करते हैं, तो SQL इनका मूल्यांकन (evaluation) एक निश्चित precedence order के अनुसार करता है। सही precedence समझना जरूरी है ताकि आपकी query का result सटीक आए।

📚 SQL Operator Evaluation Order

  • 1️⃣ First: NOT
  • 2️⃣ Second: AND
  • 3️⃣ Third: OR

जब आप इन operators को एक साथ लिखते हैं, SQL पहले NOT को evaluate करता है, फिर AND को और अंत में OR को। अगर आपको किसी हिस्से को पहले evaluate कराना है तो () parentheses का उपयोग करें।

🧾 SQL Example (With Parentheses)

SELECT * FROM employees
WHERE NOT (department = 'HR' AND salary < 40000);
      

ऊपर दिए उदाहरण में NOT सबसे पहले लागू होगा, फिर AND और अंत में OR (अगर मौजूद हो)। Parentheses ( ) लगाने से आप SQL को बताते हैं कि पहले कौन सी condition evaluate करनी है।

💡 Tip: हमेशा parentheses का प्रयोग करें जब query complex हो — इससे readability बढ़ती है और गलत precedence से बचा जा सकता है।

Keywords covered: sql logical operator precedence, and or in sql, and or precedence sql, sql operator evaluation order.

🧠 What Are SQL Logical Operators? (SQL Logical Operators in Hindi)

SQL Logical Operators का उपयोग डेटा को कई शर्तों (conditions) के आधार पर filter करने के लिए किया जाता है। ये operators आपको शक्तिशाली queries बनाने में मदद करते हैं — जिससे आप database से वही records निकाल सकें जो आपके विश्लेषण या रिपोर्टिंग के लिए जरूरी हैं।

💡 Logical Operators in SQL allow you to combine multiple WHERE conditions together. इसका मतलब है कि आप AND, OR और NOT का इस्तेमाल करके एक ही query में कई filters जोड़ सकते हैं।

🎯 ये operators किसी भी SQL query का logical हिस्सा होते हैं — जिससे आप precisely यह नियंत्रित कर सकते हैं कि कौन से records select हों। यह सुविधा data analysts, developers और DBAs के लिए बहुत उपयोगी है।

🔍 The 3 Foundational Logical Operators in SQL

  • AND — Returns results where all specified conditions are true.
    (हिंदी: AND तभी true होगा जब सभी शर्तें सही हों।)
  • OR — Returns results where at least one condition is true.
    (हिंदी: OR तब true होगा जब किसी भी एक शर्त का परिणाम सही हो।)
  • NOT — Negates a condition; returns results where the condition is false.
    (हिंदी: NOT किसी शर्त का उल्टा देता है यानी अगर शर्त true है तो result false होगा।)

🧾 Example:
SELECT * FROM employees WHERE department = 'HR' AND salary > 40000;
यहाँ AND operator यह सुनिश्चित करता है कि दोनों conditions — department और salary — true हों तभी result दिखेगा।

Keywords: sql logical operators in hindi, sql and or not, logical operators in sql examples, and or in sql.

1️⃣ AND Operator क्या है? — समझें आसान भाषा में (AND operator in SQL)

🔍 AND Operator कैसे काम करता है

AND तभी true होता है जब उसके दोनों (या सभी) conditions true हों। इसका उपयोग उन rows को filter करने के लिए होता है जो एक से अधिक शर्तें पूरी करते हैं — जैसे department और salary दोनों मिलकर मैच करें।

🧾 Syntax — उपयोग का तरीका

SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;

(Keywords: and or sql, sql and not or)

💡 Example — व्यावहारिक उपयोग

SELECT name, department, salary
FROM employees
WHERE department = ‘IT’ AND salary > 50000;

ऊपर की query केवल उन्हीं employees को लौटाएगी जिनका department ‘IT’ है और जिनकी salary 50,000 से अधिक है। (हिंदी नोट: दोनों शर्तें पूरी होनी चाहिए।)

💡 Tip: जब multiple AND conditions हों तो readability बढ़ाने के लिए हर condition को अलग line में रखें और parentheses का उपयोग तभी करें जब OR/NOT के साथ mix कर रहे हों।

Targets: sql and or, and or sql, logical operators in sql, sql where condition.

2️⃣ Exploring the OR Operator — SQL में OR Operator क्या होता है?

SQL में OR Operator का उपयोग तब किया जाता है जब आपको उन records को लाना हो जो कम से कम एक condition को पूरा करते हैं। यानी अगर कोई भी condition true है, तो row result में दिखाई जाएगी।

🔍 How the OR Operator Works

The OR operator returns results where any one of the conditions is true. हिंदी में — अगर एक भी शर्त सही है, तो record परिणाम में शामिल होगा। यह operator multi-condition filtering के लिए बहुत उपयोगी होता है।

💡 Syntax — उपयोग का तरीका

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;
      

उदाहरण: आप ऐसी query बना सकते हैं जहाँ किसी भी एक condition के true होने पर result लौटे।

🧾 Example 1 — Simple City Filter

मान लीजिए आपके पास customers नाम की table है। अगर आपको उन ग्राहकों को खोजना है जो या तो New York या Los Angeles में रहते हैं, तो आप लिखेंगे:

SELECT name, city
FROM customers
WHERE city = 'New York' OR city = 'Los Angeles';
      

यह query उन सभी ग्राहकों को लौटाएगी जिनका city इनमें से कोई भी एक है।

🧮 Example 2 — Price Range Filter

अब अगर आप products की list से उन items को ढूंढना चाहते हैं जिनकी price $20 से कम या $100 से ज़्यादा है:

SELECT product_name, price
FROM products
WHERE price < 20 OR price > 100;
      

यहाँ OR यह सुनिश्चित करता है कि दोनों conditions में से कोई एक भी true हो तो record दिखे।

💡 Tips for Using OR Effectively

  • बेकार या दोहराई गई conditions न लिखें — query को साफ और readable रखें।
  • OR को AND के साथ मिलाकर complex filters बनाए जा सकते हैं।
  • Parentheses () का उपयोग करें ताकि SQL सही order में evaluate करे।

Keywords: sql or operator in hindi, sql or example, sql and or precedence, and or in sql.

3️⃣ Mastering the NOT Operator — SQL में NOT Operator क्या होता है?

NOT Operator का उपयोग किसी शर्त (condition) को उल्टा करने के लिए किया जाता है — यानी जब condition false होती है, तभी result true माना जाता है। यह operator उन records को exclude करने के लिए बहुत उपयोगी है जिन्हें आप परिणाम में नहीं दिखाना चाहते।

🚫 How the NOT Operator Works

The NOT operator negates a condition — it returns results only when the condition is false. उदाहरण: अगर आप ‘Electronics’ category के products को छोड़कर बाकी सब दिखाना चाहते हैं, तो आप NOT का उपयोग करेंगे।

💡 Syntax — उपयोग का तरीका

SELECT column1, column2
FROM table_name
WHERE NOT condition;
      

यह query उन rows को लाएगी जिनमें दी गई condition false है।

🧾 Example 1 — Exclude a Category

मान लीजिए आपके पास एक products टेबल है और आपको उन items को लाना है जो “Electronics” category में नहीं हैं:

SELECT product_name, category
FROM products
WHERE NOT category = 'Electronics';
      

यह query सभी products लौटाएगी सिवाय Electronics वाले।

🧮 Example 2 — Find Non-Premium Users

अब अगर आपके पास एक users table है और आप उन users को ढूंढना चाहते हैं जो premium service में subscribed नहीं हैं:

SELECT name, subscription_status
FROM users
WHERE NOT subscription_status = 'Premium';
      

यह query केवल non-premium users को दिखाएगी।

💡 Tips for Using NOT Effectively

  • NOT को AND या OR के साथ मिलाकर complex conditions बना सकते हैं।
  • Always use parentheses () for clarity in multi-condition queries.
  • NULL values पर NOT लगाने से सावधान रहें — SQL में NOT NULL का मतलब “non-empty” होता है।

Keywords: sql not operator in hindi, sql where not, and or not in sql, sql logical operators examples.

4️⃣ AND, OR और NOT को साथ में कैसे उपयोग करें — Combining Logical Operators (हिंदी + Examples)

Complex queries बनाते वक्त AND, OR और NOT को मिलाकर प्रयोग किया जाता है। याद रखें: NOT > AND > OR (precedence)। स्पष्टता के लिए parentheses () का इस्तेमाल करें।

🔗 Combining Logical Operators — तरीका

आप parentheses के साथ छोटे logical blocks बना सकते हैं और उन्हें AND/OR/NOT से जोड़ सकते हैं। यह approach readability बढ़ाता है और unexpected precedence-बग से बचाता है।

  • NOT पहले evaluate होगा।
  • AND उसके बाद।
  • OR सबसे बाद में evaluate होगा।

🧾 Combined Examples (उदाहरण)

SELECT column1, column2
FROM table_name
WHERE (condition1 AND condition2) OR NOT condition3;

Example 1 (Students): उन students को चुनें जो grade = ’10th’ और subject = ‘Math’ हों, या जो History के लिए registered न हों:

SELECT name, grade, subject
FROM students
WHERE (grade = ’10th’ AND subject = ‘Math’) OR NOT registered = ‘History’;

Example 2 (Orders): उन orders को चुनें जो Delivered या Shipped हैं, परन्तु region ‘North’ के नहीं हैं:

SELECT order_id, customer_id, status
FROM orders
WHERE (status = ‘Delivered’ OR status = ‘Shipped’) AND NOT region = ‘North’;

✅ Best Practices

  • Complex logic को parentheses से बाँटें — readability और correctness बढ़ेगी।
  • पहले छोटे parts को test करें — फिर उन्हें combine करें।
  • NULL के साथ behavior अलग हो सकता है — IS NULL / IS NOT NULL का प्रयोग समझकर करें।
Operator Meaning (हिंदी) Example
AND दोनों conditions true होने पर true लौटाता है salary > 50000 AND department = 'IT'
OR किसी भी एक condition true होने पर true लौटाता है age < 25 OR experience > 5
NOT condition का उल्टा (negation) लौटाता है NOT gender = 'Male'

📌 Quick recap

Complex conditions के लिए parentheses का उपयोग करें — precedence याद रखें: NOT > AND > OR. Test small parts before combining.

© Vista Academy • Content updated 2025 • Vista theme — dark, golden & elegant UX for learners.

5️⃣ सामान्य गलतियाँ और उन्हें कैसे टाला जाए — Common Mistakes (हिंदी + Quick Fixes)

अक्सर developers और learners logical operators में कुछ सामान्य गलतियाँ करते हैं — नीचे उन गलतियों, कारण और त्वरित समाधान दिए गए हैं। (Targets: and or precedence sql, sql logical operators mistakes.)

⚠️ Operator Precedence भूलना / Forgetting Precedence

हिंदी: सबसे आम गलती — लोग NOT, AND, OR के precedence को अनदेखा कर देते हैं और unexpected results पाते हैं। याद रखें: NOT > AND > OR
English: If you ignore precedence, queries can return the wrong rows. Use parentheses to be explicit.

— Fix: add parentheses to make intent explicit
SELECT * FROM table WHERE (A AND B) OR C; — clear grouping

🔁 Parentheses का गलत उपयोग / Misusing Parentheses

हिंदी: parentheses की गलत जगह पर रखने से logic बदल सकता है। सही grouping से ही query का intended परिणाम मिलता है।
English: Place parentheses thoughtfully — they control evaluation order and make queries readable.

— Bad: SELECT * FROM t WHERE A AND (B OR C) — maybe OK
— Wrong grouping example: SELECT * FROM t WHERE (A AND B OR C) — ambiguous

🔧 Queries में ज्यादा logique जोड़ना / Overloading Queries

हिंदी: बहुत सारे logical operators से query जटिल और धीमी हो सकती है — पढ़ने और maintain करने में मुश्किल होती है।
English: Break complex logic into subqueries or CTEs for clarity and performance.

— Fix: use CTEs/subqueries
WITH eligible AS (SELECT * FROM students WHERE grade = ’10th’)
SELECT * FROM eligible WHERE condition2;

❗ NULL के साथ ध्यान रखें / Watch out for NULLs

हिंदी: NULL comparisons often yield UNKNOWN — इसलिए IS NULL / IS NOT NULL का प्रयोग करें।
English: Logical expressions with NULL don’t behave like true/false; handle NULL explicitly.

— Example: WHERE status = ‘Active’ AND last_login > ‘2025-01-01’
— If last_login is NULL, whole condition becomes UNKNOWN — use COALESCE or IS NOT NULL

✅ Quick checklist — इन्हें करें

  • Parentheses का उपयोग करके logic स्पष्ट करें।
  • Complex conditions को CTEs या subqueries में तोड़ें।
  • NULL का explicit handling करें — IS NULL / COALESCE() का उपयोग।
  • Step-by-step testing: छोटे हिस्सों को run करके validate करें।

इन सरल नियमों को अपनाएँ और आपके SQL logical expressions अधिक reliable और maintainable बनेंगे। अभ्यास के लिए हमारे अभ्यास सेट देखें।

🚀 अभ्यास करें — Vista Academy

© Vista Academy • Content updated 2025 • Vista theme — dark, golden & elegant UX for learners.

6️⃣ Advanced Use Cases & Optimization Tips — SQL में Logical Operators का Smart उपयोग

SQL में AND, OR और NOT operators का सही उपयोग करने से queries न सिर्फ तेज़ बल्कि अधिक readable बनती हैं। चलिए सीखते हैं — indexing, subqueries और aggregation के साथ इन्हें optimize करने के तरीके।

⚙️ Indexing & Query Optimization (AND / OR Conditions)

Indexes का सही उपयोग query performance को बेहतर बनाता है। जब आप AND और OR conditions का उपयोग करते हैं, तो indexed columns पर filtering तेज़ होती है।

  • Frequently used columns पर index लगाएँ।
  • AND / OR conditions का order execution plan को प्रभावित कर सकता है।
  • Multi-column filters के लिए composite indexes बनाना performance बढ़ाता है।

🧩 Subqueries with Logical Operators

Subqueries आपको एक query के अंदर दूसरी query लिखने की सुविधा देती हैं। इससे complex AND, OR और NOT conditions को व्यवस्थित और readable बनाया जा सकता है।

SELECT column1, column2
FROM table_name
WHERE column1 IN (
  SELECT column1 FROM another_table
  WHERE condition1 AND condition2
);

📊 Using Logical Operators with Aggregate Functions

आप COUNT(), SUM(), AVG(), या MAX() जैसी aggregate functions के साथ logical operators जोड़ सकते हैं ताकि केवल filtered results ही count हों।

SELECT department, COUNT(*)
FROM employees
WHERE department IN (‘HR’, ‘Finance’) AND salary > 50000
GROUP BY department;

🚀 निष्कर्ष (Conclusion)

SQL में logical operators का mastery आपको powerful data filtering का control देता है। Indexing, subqueries और aggregate functions के साथ इन्हें smartly use करके आप queries को तेज़ और maintainable बना सकते हैं।

Practice करें — AND, OR, NOT combinations के साथ experiment करें और real-world datasets पर लागू करें।

👉 For more SQL tutorials and hands-on examples, visit Vista Academy — SQL Logic in Hindi

© Vista Academy • Updated 2025 • SEO-ready bilingual section (AND, OR, NOT in SQL)

❓ अक्सर पूछे जाने वाले प्रश्न — SQL Logical Operators (AND / OR / NOT)

🔹 SQL में Logical Operators का क्या उद्देश्य है? / What is the purpose of logical operators in SQL?

हिंदी: Logical operators का उपयोग कई conditions को मिलाकर डेटा फ़िल्टर करने के लिए किया जाता है — आम operators हैं AND, OR, और NOT
English: Logical operators combine multiple conditions in SQL queries to filter rows based on TRUE/FALSE evaluations.

🔹 SQL NOT operator क्या करता है? / What does the SQL NOT operator do?

हिंदी: NOT किसी condition का उल्टा करता है — यानी condition false होने पर row select होगा।
English: NOT negates a condition; it selects rows where the condition is FALSE. Example: WHERE NOT status = 'Active'.

🔹 AND और OR कैसे काम करते हैं? / How do AND and OR operators work?

हिंदी: AND तब true है जब सभी conditions true हों; OR तब true है जब कोई एक condition true हो।
English: AND returns rows when all conditions are TRUE; OR returns rows when any condition is TRUE.

🔹 SQL में operator precedence क्या है? / What is operator precedence in SQL?

हिंदी: precedence क्रम है — NOT → AND → OR. किसी हिस्से को पहले evaluate करवाने के लिए parentheses () का उपयोग करें।
English: The evaluation order is NOT, then AND, then OR. Use parentheses to override this order.

🔹 क्या मैं एक ही query में कई logical operators इस्तेमाल कर सकता हूँ? / Can I combine multiple logical operators in one SQL query?

हिंदी: हाँ — आप AND, OR, और NOT को एक साथ इस्तेमाल कर सकते हैं; बस precedence और parentheses का ध्यान रखें।
English: Yes — you can combine AND, OR, and NOT in the same WHERE clause; use parentheses for clarity and correctness.

Explore More SQL Resources

Deepen your understanding of SQL with these informative and practical resources.

🔗 SQL Joins: Master the Basics of Combining Data

Learn how to combine data effectively using SQL Joins with real-world examples in MySQL.

🔗 Understanding Commit and Rollback in MySQL

Get a comprehensive understanding of transaction control with Commit and Rollback commands in MySQL.

🔗 Explore the Power of Aggregate Functions in SQL

Discover how to use aggregate functions like COUNT, SUM, AVG, MAX, and MIN for powerful data analysis.

🔗 SQL IS NULL, IS NOT NULL, LIMIT, and OFFSET

Master the use of IS NULL, IS NOT NULL, and optimize queries with LIMIT and OFFSET.

🔗 SQL ORDER BY, DISTINCT, BETWEEN, and NOT BETWEEN

Learn how to use ORDER BY, DISTINCT, and the BETWEEN clause for effective data sorting and filtering.

🔗 Mastering the LIKE Operator and Wildcards

Explore how to search patterns efficiently in MySQL with the LIKE operator and wildcards.

🔗 Mastering SQL Logic: AND, OR, and NOT Operators

Enhance your logic-building skills with SQL’s AND, OR, and NOT operators for complex queries.

Vista Academy – 316/336, Park Rd, Laxman Chowk, Dehradun – 248001
📞 +91 94117 78145 | 📧 thevistaacademy@gmail.com | 💬 WhatsApp
💬 Chat on WhatsApp: Ask About Our Courses