Nested queries

Nested queries loops के अंदर search loops की तरह काम करती हैं, आपको पहले एक रहस्यमय value खोजने देते हुए ताकि main query इसका इस्तेमाल करके अंतिम जवाब ला सके।

11 min read · 11 cards · 2 checks

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


Theory

किताबी कीड़ों का रहस्य

आपके HOD उस student को एक विशेष certificate देना चाहते हैं जिसने हमारे CampusLib database में सबसे महँगी किताब उधार ली। आप अपनी tables देखते हैं। members table student names जानती है, पर book prices के बारे में कुछ नहीं। books table prices जानती है, पर इसके बारे में कुछ नहीं कि किसने उन्हें उधार लिया। आप एक सरल filter नहीं लिख सकते क्योंकि आप पहले से अधिकतम price नहीं जानते। आप एक ऐसी समस्या कैसे हल करते हैं जहाँ filter value ख़ुद तब तक पूर्ण रहस्य है जब तक आप इसे look up न करें?

Theory

Principal का पूछताछ कार्यालय

कल्पना कीजिए आपके college principal कहते हैं, 'सबसे कम attendance वाले section के class representative को बुलाओ।' पालन करने के लिए, आपको पहले attendance desk पर जाना होगा, देखना होगा किस section की सबसे कम attendance है (मान लीजिए Section B), और फिर उनके representative को खोजने Section B तक चलना होगा। आपने अपने दिमाग़ में दो queries चलाईं! पहली query ने वह जवाब खोजा जो दूसरी query को काम पूरा करने के लिए चाहिए था। यह query-के-अंदर-query structure एक nested query है।

Theory

एक Nested Query क्या है?

एक Nested Query, जिसे subquery भी कहते हैं, एक inner SELECT statement है जो एक outer query के WHERE या HAVING clause के अंदर embedded है। Oracle SQL में, स्वतंत्र inner query parent outer query चलने से पहले बिल्कुल एक बार execute होती है। इस inner execution का परिणाम सीधे outer query के filter clause में प्रतिस्थापित होता है। यह आपको dynamic searches लिखने देता है जो आपकी database tables समय के साथ update या विस्तार होने पर अनुकूलित होती हैं।

At a glance

inner query output formats के आधार पर subqueries का वर्गीकरण

Subquery TypeOperator RequiredExpected Internal Return
Single-Row Subquery=, >, <, >=, <=एक single price cell जैसा बिल्कुल एक scalar value return करता है
Multi-Row SubqueryIN, ANY, ALLएक column से कई values की एक ऊर्ध्वाधर सूची return करता है
Multi-Column SubqueryINstructural pairs या tuples से मेल खाते कई columns return करता है

Practical

सबसे महँगी किताब वाले Members खोजना

-- Step 1: Find the max price from books
-- Step 2: Use that price to find matching book IDs
-- Step 3: Match those book IDs to member IDs in issues
SELECT name 
FROM members 
WHERE member_id IN (
  SELECT member_id 
  FROM issues 
  WHERE book_id IN (
    SELECT book_id 
    FROM books 
    WHERE price = (SELECT MAX(price) FROM books)
  )
);

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

Follow along

सबसे-भीतरी-पहले पढ़ने की आदत

  1. 1. सबसे भीतरी Query चलाएँ Oracle सबसे गहरी subquery को पहले evaluate करता है ताकि MAX(price) के ज़रिए सबसे ऊँचा numeric value calculate करे।
  2. 2. Values ऊपर पास करें परिणामी value inner bracket की जगह लेता है, book filter में feed होते हुए।
  3. 3. बीच की Query Evaluate करें parent query उस value से मेल खाते book codes खोजती है और IDs का एक संग्रह return करती है।
  4. 4. अंतिम Outer Query Execute करें सबसे बाहरी SELECT उन IDs को member profiles से match करता है और अंतिम student names print करता है।

Quiz

अगर आप equal operator (=) इस्तेमाल करके एक nested query लिखते हैं पर inner query ग़लती से एक ही अधिकतम price साझा करती दो अलग किताबें पाती है, Oracle SQL क्या करेगा?

  1. यह अपने-आप पहली मेल खाती row चुनेगा और चुपचाप दूसरी row को नज़रअंदाज़ करेगा।
  2. यह एक runtime error फेंकेगा क्योंकि एक single-row operator कई rows नहीं पा सकता।
  3. यह सफलतापूर्वक अपना mode update करेगा और बिना शिकायत दोनों मेल खाते records return करेगा।
  4. यह पूरे database connection pool को crash करेगा और physical table storage को भ्रष्ट करेगा।
Show the answer

यह एक runtime error फेंकेगा क्योंकि एक single-row operator कई rows नहीं पा सकता।

equal (=) या less-than (<) जैसे single-row comparison operators इस्तेमाल करना माँगता है कि subquery बिल्कुल एक value return करे। अगर यह कई records return करती है, Oracle एक साफ़ error flag के साथ रुक जाता है। संभावित multi-row outputs सुरक्षित रूप से सँभालने के लिए, आपको operator को IN में बदलना होगा।

Watch out

Correlated Performance गड्ढा

Correlated Subqueries के साथ बेहद सावधान रहिए! standard nested queries के उलट जो केवल एक बार execute होती हैं, एक correlated subquery outer table से उत्पन्न एक column को reference करती है। यह Oracle को outer table में मौजूद हर एक row के लिए एक बार पूरा inner query block फिर से चलाने को मजबूर करता है! अगर आपकी members table में 10,000 students हैं, inner lookup 10,000 बार चलती है, एक विशाल processing अड़चन पैदा करते हुए जो आपको university lab exams में marks गँवाएगी।

Think first

Select List Subquery Challenge

Mental Challenge: इस setup का विश्लेषण कीजिए: 'SELECT name, (SELECT MAX(price) FROM books) FROM members;'। क्या यह layout Oracle SQL में सफलतापूर्वक execute होगा, या यह एक invalid statement है? tap करने से पहले structural placement मन में विश्लेषित कीजिए।

Show the answer

यह बिल्कुल सही execute होगा! यह एक Scalar Subquery के रूप में जाना जाता है। Oracle SQL में, आप एक nested query को column selection list के ठीक अंदर रख सकते हैं, बशर्ते यह एक single evaluation value की guarantee दे। यह global अधिकतम book price हर individual member record row के साथ अगल-बगल दिखाएगा।

Theory

Dialect भिन्नताएँ और Career Tracking

जबकि subqueries Semester 3 में SQLite जैसे dialects भर एक जैसा बर्ताव करती हैं, Oracle SQL proprietary optimization engines देता है जो पर्दे के पीछे nested subqueries को चपटे internal semi-joins में बदल देते हैं। यह सबसे-भीतरी-पहले पढ़ने की मानसिकता विकसित करना आपको technical campus placements के दौरान optimal code लिखने और backend data architecture rounds clear करने के लिए तैयार करता है!

Summary

Key takeaways

  • एक nested query एक outer filter expression के अंदर एक द्वितीयक SELECT statement को अलग करती है।
  • Oracle non-correlated subqueries को अंदर से बाहर process करता है, सबसे गहरा block पहले ख़त्म करते हुए।
  • Single-row comparison operators inner nested selection से बिल्कुल एक value माँगते हैं।
  • Multi-row returns को errors से बचने के लिए IN, ANY, या ALL जैसे विशेष operators इस्तेमाल करके filter करना होगा।
  • Correlated subqueries outer query columns को refer करती हैं और हर row के लिए बार-बार evaluate होती हैं।
  • Memory hook: अंदर से बाहर सोचिए, main route clear करने से पहले inner पहेली हल कीजिए!

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 Advanced SQL

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

Nested queries · Concepts of Relational Database Management Systems · Gri-Learn