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

Subquery Basics

एक subquery एक question के अंदर एक question जैसा है: बड़े का जवाब देने के लिए, आप पहले parentheses के अंदर लिखे एक छोटे का answer ढूँढते हैं।
Syntax
sql
SELECT column1
FROM table_name
WHERE column2 operator (SELECT column FROM other_table);

एक Subquery क्या है?

एक subquery एक nested query है जो किसी दूसरी SQL query के अंदर, parentheses में wrapped लिखी जाती है। inner query पहले चलती है और अपने results outer query को pass करती है, जो फिर उस result को एक literal value या table की तरह इस्तेमाल करती है।

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

sql
CREATE TABLE products (id INT, price INT);
INSERT INTO products VALUES (1, 10), (2, 50), (3, 90);
SELECT * FROM products WHERE price > (SELECT AVG(price) FROM products);

Scalar Subqueries

एक scalar subquery exactly एक value return करता है (एक row और एक column), जैसे सभी products में average price। आप एक WHERE clause में सीधे scalar subqueries के साथ =, <, या > जैसे standard comparison operators इस्तेमाल कर सकते हैं।

उदाहरण: Scalar Subqueries

sql
CREATE TABLE products (id INT, price INT);
INSERT INTO products VALUES (1, 10), (2, 50), (3, 90);
SELECT * FROM products WHERE price = (SELECT MAX(price) FROM products);

Multi-Row Subqueries

एक multi-row subquery एक single value के बजाय values की एक list return करता है, जैसे एक specific region से सभी customer IDs। क्योंकि यह कई rows return करता है, आपको list के against values compare करने के लिए IN, ANY, या ALL जैसे operators इस्तेमाल करने ज़रूरी हैं।

उदाहरण: Multi-Row Subqueries

sql
CREATE TABLE customers (id INT, region TEXT);
CREATE TABLE orders (id INT, customer_id INT);
INSERT INTO customers VALUES (1, 'North'), (2, 'South');
INSERT INTO orders VALUES (101, 1), (102, 2);
SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE region = 'North');

SELECT Clause में Subqueries

आप row-by-row calculations perform करने के लिए अपनी SELECT clause के अंदर एक subquery इस्तेमाल कर सकते हैं। यह related tables से counts fetch करने के लिए उपयोगी है, जैसे हर customer को उनके orders की total संख्या के साथ दिखाना।

उदाहरण: Subqueries in the SELECT Clause

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), (102, 1);
SELECT name, (SELECT COUNT(*) FROM orders WHERE orders.customer_id = customers.id) AS order_count
FROM customers;

FROM Clause में Subqueries

आप अपनी FROM clause में एक subquery को एक temporary table की तरह इस्तेमाल कर सकते हैं, इसे join करने से पहले data को pre-filter या pre-aggregate करने के लिए उपयोगी। MySQL में, FROM clause में इस्तेमाल किए गए subqueries को एक alias name assign करना ज़रूरी है, नहीं तो query fail हो जाएगी।

उदाहरण: Subqueries in the FROM Clause

sql
CREATE TABLE orders (id INT, customer_id INT, total INT);
INSERT INTO orders VALUES (1, 1, 50), (2, 1, 70), (3, 2, 20);
SELECT customer_id, spend
FROM (SELECT customer_id, SUM(total) AS spend FROM orders GROUP BY customer_id) AS totals
WHERE spend > 30;
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. एक scalar subquery इस्तेमाल करना जो एक से ज़्यादा row = के साथ return करता है, जो एक error देता है।
  2. subquery के आसपास parentheses भूल जाना।
  3. एक subquery लिखना जो कई columns return करता है जहाँ एक expected है।
🔒

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.