Subquery in FROM
In this page:
SELECT alias.column
FROM (SELECT columns FROM table_name) AS alias;
FROM में एक Subquery क्या है?
FROM clause में एक subquery एक query है जो किसी दूसरे के अंदर nested है, एक real table की जगह खड़ी होती है। यह एक temporary table जैसा act करती है। आप इससे data select कर सकते हैं बिल्कुल किसी regular table की तरह, outer query में इसे आगे join या filter करते हुए।
उदाहरण: What is a Subquery in FROM?
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 50), (2, 1, 70), (3, 2, 20);
SELECT * FROM (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id) AS totals;
Table Aliases इस्तेमाल करना
MySQL में, आपको FROM clause में एक subquery को हमेशा एक alias देना ज़रूरी है, कहीं और इस्तेमाल होने वाली subqueries के उलट। यह temporary table को एक नाम देता है ताकि आप outer query से इसे और इसके columns को reference कर सकें।
उदाहरण: Using Table Aliases
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 50), (2, 1, 70);
SELECT totals.customer_id, totals.spend
FROM (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id) AS totals;
Aggregates को Aggregate करना
आप directly aggregate functions आसानी से nest नहीं कर सकते, जैसे एक SUM() के result पर AVG() call करना। FROM clause में एक subquery आपको पहले से aggregated data को फिर से summarize करने में मदद करती है, पहले aggregation के output को summarize करने के लिए एक fresh table की तरह treat करके।
उदाहरण: Aggregating Aggregates
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 50), (2, 1, 70), (3, 2, 20), (4, 2, 30);
SELECT AVG(spend) AS avg_customer_spend
FROM (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id) AS totals;
Subquery के अंदर Filtering
बाद में outer query में filter करने के बजाय, nested subquery के अंदर filter करना performance optimize कर सकता है। यह outer query को process करने के लिए ज़रूरी rows की संख्या कम कर देता है, खासकर जब inner filter effectively एक index इस्तेमाल कर सके।
उदाहरण: Filtering inside the Subquery
CREATE TABLE orders (id INT, status TEXT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 'paid', 1, 50), (2, 'unpaid', 1, 70);
SELECT customer_id, SUM(total) AS paid_total
FROM (SELECT * FROM orders WHERE status = 'paid') AS paid_orders
GROUP BY customer_id;
Subqueries बनाम CTEs
Common Table Expressions (CTEs), WITH से लिखे गए, FROM clause में subqueries जैसे काम करते हैं लेकिन पहले से एक defined name और complex queries के लिए बेहतर readability के साथ। Subqueries simple, quick, one-off logic blocks के लिए बढ़िया हैं जिन्हें एक से ज़्यादा बार reference करने की ज़रूरत न हो।
उदाहरण: Subqueries vs CTEs
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 50), (2, 1, 70);
-- Subquery in FROM
SELECT * FROM (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id) AS totals;
-- Equivalent CTE
WITH totals AS (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id)
SELECT * FROM totals;
- एक derived table के लिए required alias भूल जाना, जो
Every derived table must have its own aliasदेता है। - subquery के अंदर एक ऐसा column refer करना जो उसमें select नहीं की गई।
- subquery के बाद filter करना जबकि इसे अंदर filter किया जाना चाहिए, MySQL को ज़्यादा rows process करवाते हुए।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: