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

MySQL ANY और ALL Operators

ANY और ALL 'क्या मेरा score उनमें से कम से कम एक से बेहतर है' बनाम 'हर एक से बेहतर है' पूछने जैसे हैं।
Syntax
sql
WHERE column operator ANY (SELECT column FROM table_name)
WHERE column operator ALL (SELECT column FROM table_name)

ANY और ALL क्या हैं?

ANY और ALL आपको एक single scalar value के against नहीं, बल्कि एक subquery द्वारा return की गई हर row के against एक value compare करने देते हैं।

ANY true return करता है अगर comparison subquery result में कम से कम एक row के लिए hold करे, जबकि ALL को comparison हर row के लिए hold करना ज़रूरी है। वे हमेशा =, >, या < जैसे एक comparison operator के साथ pair होते हैं जो सीधे ANY या ALL से पहले रखा जाता है।

उदाहरण: What Are ANY and ALL?

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

= ANY IN जैसा Behave करता है

= ANY(subquery) इस्तेमाल करना same subquery के साथ IN operator इस्तेमाल करने के functionally identical है -- दोनों check करते हैं कि value subquery result की कम से कम एक row से match करती है या नहीं। MySQL आपको दोनों form लिखने देता है, लेकिन simple equality checks के लिए IN आमतौर पर ज़्यादा readable है, जबकि plain equality से आगे जाने पर ANY ज़्यादा उपयोगी है।

उदाहरण: = ANY Behaves Like IN

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

ALL के साथ Compare करना

> ALL(subquery) जैसा एक comparison सिर्फ तभी true है जब value subquery द्वारा return की गई हर single row से बड़ी हो, ALL को effectively subquery की maximum (> के लिए) या minimum (< के लिए) value के against एक check बनाते हुए। यह अक्सर सीधे MAX() या MIN() से लिखना clearer है, लेकिन ALL तब उपयोगी है जब subquery logic एक single aggregate से ज़्यादा complex हो।

उदाहरण: Comparing with ALL

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

Empty Subqueries के साथ ANY/ALL

अगर subquery zero rows return करे, ANY comparisons हमेशा false evaluate होते हैं क्योंकि condition match करने के लिए कोई row नहीं है, जबकि ALL comparisons हमेशा true evaluate होते हैं क्योंकि condition एक empty set की हर row के लिए trivially hold करता है। यह edge case तब मायने रखता है जब subquery एक ऐसे filter पर depend करती है जो legitimately कुछ भी match न करे।

उदाहरण: ANY/ALL with Empty Subqueries

sql
CREATE TABLE products (id INT, price INT);
INSERT INTO products VALUES (1, 20);
SELECT * FROM products WHERE price > ANY (SELECT price FROM products WHERE id = 999);
SELECT * FROM products WHERE price > ALL (SELECT price FROM products WHERE id = 999);

ANY/ALL बनाम EXISTS

ANY और ALL एक specific column value को subquery के results के against compare करते हैं, जबकि EXISTS actual values ignore करते हुए बस check करता है कि subquery कोई rows return करता है या नहीं। जब comparison value खुद मायने रखे तो ANY/ALL इस्तेमाल करें, और जब आपको सिर्फ इसकी परवाह हो कि एक related row मौजूद है या नहीं तो EXISTS, जैसे यह check करना कि किसी customer ने कम से कम एक order place किया है या नहीं।

उदाहरण: ANY/ALL vs EXISTS

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);
SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.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. यह सोचना कि > ANY का मतलब सबसे बड़ा है, जबकि इसका मतलब कम से कम एक value से बड़ा है, और > ALL का मतलब है हर value से बड़ा।
  2. एक empty subquery पर ALL इस्तेमाल करना, जो हर row के लिए true है।
  3. <> ANY इस्तेमाल करना, जो लगभग हर चीज़ के लिए true है, जब NOT IN मतलब था।
चैप्टर सारांश
  • एक subquery किसी दूसरी query के अंदर nested एक query है, WHERE या FROM में इस्तेमाल होती है।
  • Correlated subqueries outer query से values पर depend करती हैं।
  • EXISTS, NOT EXISTS, ANY, और ALL subquery results के against compare करते हैं।
🔒

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.