Sub Queries with Insert, Update, Delete

Subqueries भीतरी dynamic जासूसों की तरह काम करती हैं, वे बिल्कुल तथ्य लाती हैं जो एक outer INSERT, UPDATE, या DELETE command को अपना काम करने के लिए चाहिए।

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

आप एक अज्ञात तथ्य के साथ हज़ार records कैसे बदलते हैं?

कल्पना कीजिए TicketDesk system एक आपातकाल झेलता है। Amit नामक एक agent अचानक छुट्टी पर चला गया है, और आपको आदेश है कि उसके सारे open tickets को तुरंत Critical priority पर escalate करें। अगर आप जानते हैं Amit का agent ID 4 है, यह एक सरल query है। पर क्या हो अगर आपके पास केवल उसका नाम है, और उसका ID एक विशाल database में कोई बेतरतीब अंक हो सकता है? आप एक exam या एक production crash के दौरान पहले हाथ से ID look up करके इसे एक दूसरी query में type नहीं कर सकते। आपको एक तरीक़ा चाहिए ताकि एक query जवाब सीधे दूसरी में feed करे।

Theory

भीतरी लिफ़ाफ़े वाला Courier

एक executive assistant के बारे में सोचिए जो एक sealed निर्देश पाता है: 'यह bonus check उस employee को पहुँचाओ जिसने इस महीने top performance award जीता।' assistant अभी नहीं जानता कि award किसने जीता। पहले, वे विजेता का नाम, Vijay, रखता एक भीतरी लिफ़ाफ़ा खोलते हैं। एक बार वे वह नाम पढ़ते हैं, वे check पर Vijay का नाम लिखकर बाहरी निर्देश पूरा करते हैं। SQL में, एक data modification query एक छिपी inner query रख सकती है जो पहले अज्ञात data value खोजती है।

Theory

DML Operations में Subqueries परिभाषित करना

Oracle SQL में, एक Subquery (या nested query) एक SELECT statement है जो एक दूसरे SQL statement के अंदर embedded है। जब INSERT, UPDATE, या DELETE जैसे Data Manipulation Language (DML) statements के भीतर इस्तेमाल होती है, inner subquery बिल्कुल एक बार चलती है ताकि एक data value या values की एक सूची लाए। outer DML statement फिर उस परिणाम को तुरंत इस्तेमाल करके बिल्कुल pinpoint करता है कि database schema से कौन सी rows insert, modify, या हटानी हैं।

At a glance

Oracle SQL में subqueries अलग data modification commands को कैसे सशक्त करती हैं

DML ActionSubquery की भूमिकाPractical TicketDesk Example
INSERTजोड़ी जाने वाली rows या values देता हैपुराने resolved tickets को एक अलग archive table में copy करना
UPDATEनया value खोजता है या target rows filter करता हैएक ख़ास team द्वारा सँभाले सारे tickets के लिए ticket status High पर set करना
DELETErow removal के लिए criteria पहचानता हैनिष्क्रिय agent accounts से जुड़े सारे tickets मिटाना

Practical

UPDATE और DELETE Statements के भीतर Subqueries Execute करना

-- Step 1: Escalate tickets for an agent when you only know their name
UPDATE tickets 
SET priority = 'Critical' 
WHERE agent_id = (SELECT id FROM agents WHERE name = 'Amit');

-- Step 2: Delete tickets associated with agents whose names start with Test
DELETE FROM tickets 
WHERE agent_id IN (SELECT id FROM agents WHERE name LIKE 'Test%');

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

क्या होगा अगर एक UPDATE statement 'WHERE agent_id = (SELECT id FROM agents WHERE name = Liam)' के अंदर subquery 3 अलग IDs return करती है?

  1. Oracle सफलतापूर्वक 3 IDs में से किसी से मेल खाते सारे tickets update करेगा
  2. Oracle एक runtime error फेंकेगा क्योंकि एक single value operator (=) केवल 1 row की उम्मीद करता है
  3. Oracle update को पूरी तरह नज़रअंदाज़ करेगा और बिना एक error के अगले command पर कूद जाएगा
  4. Oracle अपने-आप equality operator को एक IN operator में बदल देगा
Show the answer

Oracle एक runtime error फेंकेगा क्योंकि एक single value operator (=) केवल 1 row की उम्मीद करता है

equality operator (=) एक single row operator है। अगर subquery कई rows return करती है (जैसे 3 अलग IDs), Oracle तय नहीं कर सकता कि किसे इस्तेमाल करे और एक error फेंकेगा: single-row subquery returns more than one row। कई rows सँभालने के लिए, आपको (=) के बजाय IN operator इस्तेमाल करना होगा।

Think first

Mental Challenge: INSERT के साथ Subquery

कल्पना कीजिए आपके पास history_logs(ticket_id, title) नामक एक ख़ाली table है। एक INSERT statement मन में बनाइए जो एक subquery इस्तेमाल करके status 'Closed' वाले सारे tickets की id और title इस table में copy करे। reveal करने से पहले कोशिश कीजिए।

Show the answer

query है: INSERT INTO history_logs (ticket_id, title) SELECT id, title FROM tickets WHERE status = 'Closed'; ध्यान दीजिए कि जब सीधे एक subquery से rows insert करते हैं, आप VALUES keyword इस्तेमाल नहीं करते!

Watch out

VALUES Keyword की ग़लती

कई rows copy करने के लिए एक INSERT statement के साथ एक subquery इस्तेमाल करते समय, exam marks गँवाने वाली एक बड़ी ग़लती INSERT INTO table VALUES (SELECT ...); लिखना है। Oracle SQL में, VALUES को एक multi-row subquery के साथ जोड़ना एक syntax error पैदा करता है। VALUES keyword को पूरी तरह हटा दीजिए और INSERT statement को सीधे data-producing SELECT query structure का पालन करने दीजिए।

Theory

असली दुनिया के Operations और Sem 3 Threads

DML commands के साथ subqueries इस्तेमाल करना batch operations सँभालते database administrators के लिए एक बुनियादी कौशल है। rows को एक-एक करके update करने के लिए Python या Java में अलग backend loops लिखने के बजाय, एक अकेली SQL query server side पर तुरंत लाखों changes सँभालती है। अपने Sem 3 SQLite curriculum में, आप इन बिल्कुल subquery techniques का इस्तेमाल local mobile app storage को कुशलता से साफ़ करने के लिए करेंगे।

Summary

Key takeaways

  • एक subquery एक भीतरी SELECT statement है जो एक outer query statement को data values देती है।
  • Subqueries को INSERT, UPDATE, और DELETE operations के अंदर embed किया जा सकता है ताकि data changes dynamic हों।
  • (=) जैसे single row operators केवल तब इस्तेमाल कीजिए जब आप निश्चित हों कि subquery बिल्कुल एक value return करती है।
  • IN जैसे multi row operators तब इस्तेमाल कीजिए जब inner subquery कई मेल खाते records return कर सकती है।
  • एक subquery statement से निकाली rows insert करते समय VALUES keyword इस्तेमाल मत कीजिए।
  • Memory hook: Inner query तथ्य इकट्ठा करती है, Outer query physical modifications करती है!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction of Relational Database

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Sub Queries with Insert, Update, Delete · Mastering SQL - PL/SQL (SEC-02 option A) · Gri-Learn