← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 3 of 8

IN Operator

IN 'इस छोटी list में से किसी को चुनो' कहने जैसा है। यह एक साथ कई OR choices लिखने का एक tidy तरीका है।
Syntax
sql
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

sql
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

sql
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

sql
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

sql
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

sql
SELECT * FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 100);
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. एक numeric column के लिए IN के अंदर numbers को quotes में रखना, जो type problems छुपा सकता है।
  2. NULL रखने वाली एक list या subquery पर NOT IN इस्तेमाल करना, जो कोई rows return नहीं करता।
  3. बिना parentheses के IN Patna, Delhi लिखना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.