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

MySQL UNION ALL

UNION ALL कई SELECT statements के results को एक result set में combine करता है, हर row duplicates सहित रखते हुए, UNION के उलट जो उन्हें हटाता है।
Syntax
sql
SELECT column1, column2 FROM table1
UNION ALL
SELECT column1, column2 FROM table2;

UNION बनाम UNION ALL

UNION और UNION ALL दोनों दो या ज़्यादा SELECT statements के results को एक single result set में combine करते हैं, लेकिन UNION automatically उस combined result से duplicate rows हटा देता है, जबकि UNION ALL हर row को exactly return हुई तरह रखता है, duplicates सहित।

उदाहरण: UNION vs UNION ALL

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

Basic UNION ALL Syntax

UNION ALL UNION जैसा ही syntax इस्तेमाल करता है -- UNION ALL keyword से separated दो या ज़्यादा SELECT statements -- और हर SELECT को same order में compatible data types वाले same संख्या के columns return करना ज़रूरी है।

उदाहरण: Basic UNION ALL Syntax

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

Duplicate Rows रखी जाती हैं

क्योंकि UNION ALL deduplication step पूरी तरह skip करता है, अगर same row किसी एक से ज़्यादा SELECT statement के results में दिख जाए, वह row final combined output में कई बार दिखेगी।

उदाहरण: Duplicate Rows are Kept

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

Performance Differences

UNION ALL आमतौर पर UNION से तेज़ है क्योंकि duplicates हटाने के लिए MySQL को combined result में हर row sort और compare करना पड़ता है, वह काम जो UNION ALL result sets को बस साथ append करके पूरी तरह skip कर देता है।

उदाहरण: Performance Differences

sql
-- UNION: must sort and compare every row to remove duplicates
SELECT name FROM t1 UNION SELECT name FROM t2;
-- UNION ALL: skips deduplication entirely, so it's faster
SELECT name FROM t1 UNION ALL SELECT name FROM t2;

UNION और UNION ALL में चुनना

UNION ALL बेहतर choice है जब duplicate rows या तो impossible, expected, या रखने लायक meaningful हों, या जब performance deduplication से ज़्यादा मायने रखे, जबकि UNION तब अपनी extra cost worth है जब actually एक clean, duplicate-free result चाहिए।

उदाहरण: Choosing Between UNION and UNION ALL

sql
CREATE TABLE web_logs (event TEXT);
CREATE TABLE app_logs (event TEXT);
INSERT INTO web_logs VALUES ('click');
INSERT INTO app_logs VALUES ('click');
-- Duplicates are meaningful here -- count every event
SELECT event FROM web_logs UNION ALL SELECT event FROM app_logs;
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. duplicates न चाहते हुए UNION ALL इस्तेमाल करना और repeated rows पाना।
  2. यह मान लेना कि column names दूसरे SELECT से आते हैं, जबकि result पहली query के names इस्तेमाल करता है।
  3. columns को अलग order में combine करना, ताकि अलग meanings की values same column में पहुँच जाएँ।
चैप्टर सारांश
  • Joins कई tables से rows combine करते हैं, INNER JOIN, LEFT JOIN, और RIGHT JOIN सहित।
  • Self joins, cross joins, और multiple table joins ज़्यादा advanced relationships handle करते हैं।
  • UNION और UNION ALL कई queries के results को एक में combine करते हैं।
🔒

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.