Theory
Smart Fine Update
कल्पना कीजिए college principal आपको library में हर उस किताब का price दोगुना करने का आदेश देते हैं जो कभी किसी एक student द्वारा उधार नहीं ली गई। आप अपना SQL editor खोलते हैं। आप एक निश्चित book identification number update करना जानते हैं, पर आप एक बिल्कुल अलग table में छिपे data के आधार पर एक किताब कैसे update करते हैं? IDs की एक सूची हाथ से hardcode करना असंभव है अगर हज़ारों records हों। हम अपने data modifications को इतना smart कैसे बनाएँ कि वे पहले database पढ़ें?
Theory
गुप्तचर की Briefing
एक administrative clerk के बारे में सोचिए जिसे कुछ student profiles पर 'Suspended' मुहर लगाने का काम सौंपा गया है। अनुमान लगाने के बजाय, clerk को hostel warden से एक अलग सूची मिलती है जिसमें उन students के नाम हैं जिन्होंने संपत्ति को नुक़सान पहुँचाया। clerk warden की सूची (subquery) देखता है और इसका इस्तेमाल करके main registry (parent command) update करता है। data modification पूरी तरह inner सूची द्वारा दिए live जवाबों पर निर्भर करता है।
Theory
Subqueries के साथ DML Statements
औपचारिक रूप से, एक Subquery with DML एक SELECT statement को एक INSERT, UPDATE, या DELETE statement के अंदर embed करती है। अपने values, set, या where clauses में hardcoded literal values इस्तेमाल करने के बजाय, parent statement अपना data modification inner query द्वारा return किए dynamic results के आधार पर execute करता है। Oracle SQL में, यह आपको एक दूसरी संबंधित table में मिले criteria, aggregates, या listings के आधार पर एक table में records manipulate करने देता है।
At a glance
inner select queries primary DML actions में कैसे एकीकृत होती हैं
| DML Action | Subquery Placement | Example Purpose |
|---|---|---|
| INSERT | VALUES के बजाय value supply clause के अंदर | flagged overdue records को एक audit table में copy करना |
| UPDATE | SET value assignment या WHERE filter के अंदर | low performance statistics के आधार पर book prices बदलना |
| DELETE | WHERE conditional matching clause के अंदर | उन members को हटाना जिनका सालों से कोई active history नहीं |
Theory
Blacklisted Records मिटाना
आइए एक असली exam पसंदीदा देखें: 1000 रुपये से ज़्यादा क़ीमत वाली किताबों के लिए issue logs delete करना। पहले, inner query books table में खोजती है: SELECT book_id FROM books WHERE price > 1000। यह मेल खाते book IDs की एक सूची return करती है। अगला, outer query सँभालती है: DELETE FROM issues WHERE book_id IN (...);। Oracle inner search एक बार चलाता है, target IDs इकट्ठा करता है, और उन्हें outer delete command को सौंपता है ताकि तुरंत मेल खाती rows clear करे।
Quiz
क्या होता है अगर एक UPDATE statement के लिए एक WHERE clause के अंदर इस्तेमाल की गई एक inner subquery कई rows return करती है, पर आपने साधारण equal sign (=) operator इस्तेमाल किया?
- Oracle केवल पहली row process करता है और बाक़ी को नज़रअंदाज़ करता है।
- Oracle एक Single-Row Subquery Returns More Than One Row runtime error फेंकता है।
- Oracle अपने-आप equal operator को एक IN operator में बदल देता है।
- update statement सफल होता है पर सारे targeted column fields NULL पर set करता है।
Show the answer
Oracle एक Single-Row Subquery Returns More Than One Row runtime error फेंकता है।
single-row equal operator (=) subquery से बिल्कुल एक value की उम्मीद करता है। अगर subquery कई rows पाती है, Oracle एक error के साथ execution रोक देता है। कई values की एक सूची सुरक्षित रूप से सँभालने के लिए, आपको IN जैसे multi-row operators इस्तेमाल करने होंगे।
Watch out
Empty Subquery Null जाल
एक inner subquery के साथ NOT IN इस्तेमाल करते समय सावधान रहिए! अगर आपकी subquery एक NULL value वाली एक भी row return करती है, पूरी outer condition unknown में evaluate होती है। उदाहरण के लिए, उन members को delete करने की कोशिश जहाँ member_id NOT IN (SELECT member_id FROM issues) किसी भी rows को delete करने में पूरी तरह विफल होगी अगर एक blank member ID वाली एक unassigned issue row हो। सुरक्षित रहने के लिए हमेशा अपनी nested queries के अंदर एक WHERE column IS NOT NULL filter शामिल कीजिए।
Think first
Mental Query Formulation जाँच
कल्पना कीजिए आप 500 रुपये से ऊपर क़ीमत वाली सारी किताबें premium_books नामक एक अलग special table में insert करना चाहते हैं। structure मन में लिखिए। क्या आपको इस insert operation के दौरान VALUES keyword शामिल करना होगा? tap करने से पहले ध्यान से सोचिए।
Show the answer
नहीं, जब आप सीधे एक subquery से data insert करते हैं तो आप VALUES keyword इस्तेमाल नहीं करते! सही Oracle SQL format है: 'INSERT INTO premium_books SELECT * FROM books WHERE price > 500;'। यहाँ VALUES keyword शामिल करना एक सीधा syntax error trigger करेगा।
Theory
असली दुनिया का Data Warehousing
बड़े enterprise database configurations में, या Semester 4 projects में database migrations के दौरान, DML operations के भीतर subqueries अहम हैं। वे engineers को गंदा data साफ़ करने, historical records को cold storage tables में archive करने, और Java या Python में धीमे, हाथ के backend loops लिखे बिना staging tables को सहजता से synchronize करने देती हैं।
Summary
Key takeaways
- Subqueries एक data बदलने वाले statement के अंदर एक SELECT operation embed करती हैं।
- INSERT commands एक hardcoded VALUES सूची के बजाय सीधे एक subquery इस्तेमाल करते हैं।
- UPDATE और DELETE statements अपने filter clauses के अंदर nested queries पर निर्भर करते हैं।
- Multi-row subquery results को IN या ANY जैसे explicit set operators चाहिए।
- एक NOT IN subquery के अंदर return किया एक अकेला NULL value पूरी condition expression बिगाड़ देता है।
- Memory hook: Nested queries targets खोजती हैं, outer commands काम पूरा करते हैं!