IN Operator
In this page:
WHERE column_name IN (value1, value2, value3)
IN Operator का Introduction
IN operator आपको एक WHERE clause में कई values specify करने देता है। यह कई OR conditions लिखने का एक shorthand तरीका है, इसलिए पाँच equals-checks को chain करने के बजाय आप सारी पाँच values एक बार list करते हैं। यह आपकी queries पढ़ना कहीं ज़्यादा आसान और maintain करना कम error-prone बनाता है।
उदाहरण: Introducing the IN Operator
SELECT * FROM users WHERE city IN ('Patna', 'Delhi', 'Mumbai');
Text Lists के साथ IN
आप IN operator को strings की lists के साथ इस्तेमाल कर सकते हैं, जैसे orders को status names के एक set से filter करना। हर string value को single quotes में wrap करना और उन्हें commas से separate करना सुनिश्चित करें। यह OR conditions की एक लंबी chain की तुलना में clean और direct है।
उदाहरण: IN with Text Lists
SELECT * FROM orders WHERE status IN ('pending', 'shipped');
Number Lists के साथ IN
text values के उलट, IN list के अंदर इस्तेमाल होने पर numbers को single quotes नहीं चाहिए। आप बस अपने numeric IDs या statuses को parentheses के अंदर list कर सकते हैं, जैसे WHERE id IN (3, 7, 12)। यह keys के एक known set पर filter करने के लिए तेज़ और efficient है।
उदाहरण: IN with Number Lists
SELECT * FROM orders WHERE id IN (1, 2, 3);
NOT IN Operator
आप values की एक specific list exclude करने के लिए NOT को IN के साथ combine कर सकते हैं, जैसे कुछ product categories skip करना। यह उन सभी rows return करता है जिनकी values आपकी list के किसी item से match नहीं करतीं। यह आपको कई NOT-equals conditions लिखे बिना specific groups आसानी से skip करने में मदद करता है।
उदाहरण: The NOT IN Operator
SELECT * FROM products WHERE category NOT IN ('discontinued', 'archived');
IN को Subqueries के साथ इस्तेमाल करना
IN की असली power तब आती है जब आप एक hardcoded list के बजाय एक subquery इस्तेमाल करते हैं। एक subquery किसी दूसरी query के अंदर एक query है, इसलिए इसका result set वह list बन जाती है जिसके against IN check करता है। यह आपको बदलते data के आधार पर dynamically filter करने देता है, जैसे 'customers who placed an order this month.'
उदाहरण: Using IN with Subqueries
SELECT * FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 100);
- एक numeric column के लिए
INके अंदर numbers को quotes में रखना, जो type problems छुपा सकता है। NULLरखने वाली एक list या subquery परNOT INइस्तेमाल करना, जो कोई rows return नहीं करता।- बिना parentheses के
IN Patna, Delhiलिखना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: