← Back to MySQL Course | Chapter 11: Subqueries | Lesson 4 of 6

Correlated Subquery

एक correlated subquery एक coach जैसा है जो हर player के लिए, उस player के बारे में एक fresh detail check करता है। inner question हर row के साथ बदलता है।
Syntax
sql
SELECT columns
FROM outer_table AS o
WHERE column operator (
  SELECT aggregate(column) FROM inner_table AS i
  WHERE i.column = o.column
);

एक Correlated Subquery क्या है?

एक correlated subquery एक inner query है जो outer query की current row से values पढ़ती है। यह outer query द्वारा process की गई हर row के लिए एक बार चलती है, एक standalone subquery के उलट जो सिर्फ एक बार execute होती है।

उदाहरण: What is a Correlated Subquery?

sql
CREATE TABLE products (id INT, category TEXT, price INT);
INSERT INTO products VALUES (1, 'Books', 10), (2, 'Books', 40), (3, 'Toys', 20);
SELECT * FROM products p1
WHERE price > (SELECT AVG(price) FROM products p2 WHERE p2.category = p1.category);

Correlated Subqueries कैसे काम करती हैं

Database हर iteration के लिए outer table की current row से एक column value inner table को pass करता है। inner query उस specific value इस्तेमाल करके evaluate होकर अपना result return करती है, फिर अगली outer row पर move करती है।

उदाहरण: How Correlated Subqueries Work

sql
CREATE TABLE products (id INT, category TEXT, price INT);
INSERT INTO products VALUES (1, 'Books', 10), (2, 'Books', 40), (3, 'Toys', 20);
SELECT p1.id, p1.category, p1.price,
  (SELECT AVG(price) FROM products p2 WHERE p2.category = p1.category) AS category_avg
FROM products p1;

WHERE में Correlated Subquery

आप अक्सर WHERE clause के अंदर correlated subqueries देखेंगे, related data के against प्रति-row check के आधार पर outer rows filter करते हुए। यह dynamic, row-by-row filter rules बनाने के लिए उपयोगी है जिन्हें एक plain join हमेशा उतनी cleanly express नहीं कर सकता।

उदाहरण: Correlated Subquery in WHERE

sql
CREATE TABLE customers (id INT, name TEXT);
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO customers VALUES (1, 'Amit'), (2, 'Priya');
INSERT INTO orders VALUES (101, 1, 500);
SELECT * FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.total > 100);

SELECT में Correlated Subquery

आप हर returned row के लिए dynamic columns calculate करने के लिए SELECT clause में correlated subqueries इस्तेमाल कर सकते हैं, जैसे हर customer को उनके सबसे हाल के order date के साथ दिखाना जो on the fly compute किया गया।

उदाहरण: Correlated Subquery in SELECT

sql
CREATE TABLE customers (id INT, name TEXT);
CREATE TABLE orders (id INT, customer_id INT, order_date TEXT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1, '2024-01-01'), (102, 1, '2024-06-01');
SELECT name,
  (SELECT MAX(order_date) FROM orders WHERE orders.customer_id = customers.id) AS latest_order
FROM customers;

Performance Considerations

Correlated subqueries बड़ी tables पर धीमी हो सकती हैं क्योंकि वे कितनी बार re-execute होती हैं। क्योंकि वे हर row के लिए एक बार चलती हैं, वे बड़े datasets के लिए हमेशा ideal नहीं होतीं। अगर speed एक issue बन जाए तो इसके बजाय JOINS इस्तेमाल करें, क्योंकि optimizer आमतौर पर उन्हें ज़्यादा efficiently plan कर सकता है।

उदाहरण: Performance Considerations

sql
CREATE TABLE customers (id INT, name TEXT);
CREATE TABLE orders (id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'Amit');
INSERT INTO orders VALUES (101, 1);
-- Correlated subquery: re-runs once per outer row
SELECT name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
-- Often faster as a JOIN on large tables
SELECT DISTINCT customers.name FROM customers JOIN orders ON orders.customer_id = customers.id;
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. inner और outer tables को link करना भूल जाना, ताकि यह correlated न हो और हर row के लिए same value return करे।
  2. बिना aliases के दोनों queries में same table name इस्तेमाल करना, ताकि reference ambiguous हो जाए।
  3. बिना indexes वाली बड़ी tables पर इसे इस्तेमाल करना, जब inner query हर outer row के लिए चलती है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

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