← Back to MySQL Course | Chapter 9: Joins | Lesson 9 of 10

MySQL UNION Operator

UNION names की दो lists को एक-दूसरे के ऊपर stack करने जैसा है ताकि एक लंबी list बने, matching columns के साथ और repeats हटाए हुए।
Syntax
sql
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;

UNION क्या है?

UNION दो या ज़्यादा SELECT statements के result sets को एक single result set में combine करता है, उनकी rows को एक-दूसरे के ऊपर stack करते हुए बजाय एक JOIN की तरह columns को side by side match करने के।

Union के हर SELECT को same संख्या में columns return करना ज़रूरी है, और columns के data types same order में compatible होने चाहिए। यह अक्सर अलग tables से लाया गया similar data merge करने के लिए इस्तेमाल होता है, जैसे current customers को archived customers के साथ combine करना।

उदाहरण: What Is UNION?

sql
CREATE TABLE current_customers (name TEXT);
CREATE TABLE archived_customers (name TEXT);
INSERT INTO current_customers VALUES ('Amit'), ('Priya');
INSERT INTO archived_customers VALUES ('Rahul');
SELECT name FROM current_customers
UNION
SELECT name FROM archived_customers;

UNION Duplicates हटाता है

Default रूप से, UNION combined result sets में हर row compare करता है और exact duplicate rows हटा देता है, बिल्कुल जैसे एक single query पर SELECT DISTINCT करेगा।

बड़े result sets पर इस deduplication की एक real performance cost है, क्योंकि MySQL को matches ढूँढने के लिए rows sort और compare करने पड़ते हैं। अगर आपको पता है कि आपका source data पहले से unique है, या आप specifically duplicates रखना चाहते हैं, UNION ALL तेज़ choice है।

उदाहरण: UNION Removes Duplicates

sql
CREATE TABLE current_customers (name TEXT);
CREATE TABLE archived_customers (name TEXT);
INSERT INTO current_customers VALUES ('Amit');
INSERT INTO archived_customers VALUES ('Amit');
SELECT name FROM current_customers
UNION
SELECT name FROM archived_customers;

UNION ALL Duplicates रखता है

UNION ALL बस हर SELECT statement से हर row बिना duplicates check किए append कर देता है, जो इसे बड़ी tables पर plain UNION से noticeably तेज़ बनाता है क्योंकि MySQL deduplication pass पूरी तरह skip कर देता है। इसे तब इस्तेमाल करें जब combined sets में duplicate rows acceptable हों, या जब आपको actually repeats शामिल करने वाला एक count चाहिए हो, जैसे दो log tables से activity totaling।

उदाहरण: UNION ALL Keeps Duplicates

sql
CREATE TABLE current_customers (name TEXT);
CREATE TABLE archived_customers (name TEXT);
INSERT INTO current_customers VALUES ('Amit');
INSERT INTO archived_customers VALUES ('Amit');
SELECT name FROM current_customers
UNION ALL
SELECT name FROM archived_customers;

Column Count और Type Rules

एक UNION में हर SELECT statement को exact same संख्या में columns project करना ज़रूरी है, और MySQL उन्हें position से match करता है, name से नहीं -- final result set पहले SELECT से column names इस्तेमाल करता है।

अगर corresponding columns के अलग लेकिन compatible types हों, जैसे एक INT और दूसरा DECIMAL, MySQL implicitly उन्हें एक common type में convert कर देता है। Mismatched column counts तुरंत एक error raise करते हैं।

उदाहरण: Column Count and Type Rules

sql
CREATE TABLE online_sales (product TEXT, amount INT);
CREATE TABLE store_sales (product TEXT, amount DECIMAL(10,2));
INSERT INTO online_sales VALUES ('Pen', 10);
INSERT INTO store_sales VALUES ('Pencil', 5.50);
SELECT product, amount FROM online_sales
UNION
SELECT product, amount FROM store_sales;

एक UNION Result Order करना

आप एक UNION query में सिर्फ एक ORDER BY clause attach कर सकते हैं, और यह final SELECT के बाद आना ज़रूरी है, हर individual SELECT के बजाय पूरे combined result पर apply होते हुए।

UNION से पहले एक individual SELECT को order करना meaningful नहीं है, क्योंकि union का अपना row order तब तक guaranteed नहीं है जब तक final ORDER BY apply न हो। LIMIT भी उसी तरह काम करता है, सिर्फ combined output पर apply होते हुए।

उदाहरण: Ordering a UNION Result

sql
CREATE TABLE current_customers (name TEXT);
CREATE TABLE archived_customers (name TEXT);
INSERT INTO current_customers VALUES ('Priya');
INSERT INTO archived_customers VALUES ('Amit');
SELECT name FROM current_customers
UNION
SELECT name FROM archived_customers
ORDER BY name;
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. हर SELECT में अलग संख्या में columns select करना, जो एक error है।
  2. यह उम्मीद करना कि UNION duplicate rows रखेगा, जबकि यह उन्हें हटा देता है (UNION ALL इस्तेमाल करें)।
  3. पहले SELECT के अंदर ORDER BY रखना और पूरे result को sort होने की उम्मीद करना, जबकि इसे अंत में आना ज़रूरी है।
🔒

Chapter Quiz — Complete all 10 topics to unlock

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