डाउनलोड पी.डी.एफ. ई-बुक्स
SQL - UNION Clause

UNION Clause दो या दो से ज्यादा SELECT Statements के result-sets को जोड़ता है |

UNION Clause में जब SELECT Statements दिए जाते है तब उन SELECT Statements पर एक जैसे columns की संख्या होती है |

UNION Clause में सिर्फ unique values को return किया जाता है |

Syntax for UNION Clause

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

Sample Tables

+----+----------+-----------------+-------+------+
| id | Dealer   | Appliances      | Cost  | GST  |
+----+----------+-----------------+-------+------+
|  1 | Balaji   | T.V.            | 19000 | 3420 |
|  2 | Ravikant | Washing Machine | 15000 | 4200 |
|  3 | Electra  | Refrigerator    | 10000 | 2800 |
|  4 | K.K.     | Cooler          | 18000 | 5040 |
|  5 | Ganesh   | A.C.            | 35000 | 9800 |
|  6 | Rajaram  | Iron            |  1500 |  420 |
|  7 | Ravi     | Laptop          | 30000 | 8400 |
|  8 | Ravikant | Washing Machine | 15000 | 4200 |
+----+----------+-----------------+-------+------+

+--------+-----------------+------------+
| emp_id | emp_name        | emp_salary |
+--------+-----------------+------------+
|      1 | Shweta Pandit   |      30000 |
|      3 | Rupal Patil     |      35000 |
|      4 | Shankar Mane    |      15000 |
|     15 | Ramesh Mohane   |      10000 |
|     20 | Rahul Kapoor    |      27000 |
|     21 | Shankar Mane    |      20000 |
|     22 | Maria Shaikh    |      30000 |
|     23 | Vikram Kank     |      40000 |
|     24 | Vaibhav Vichare |      10000 |
|     25 | Shweta Pandit   |      25000 |
+--------+-----------------+------------+

Example for UNION Clause in SQL

Example पर Employee table का emp_salary और Appliances table का Cost इन columns का union बनाके result-set return किया गया है |

Source Code :
SELECT emp_salary 
FROM Employee 
UNION 
SELECT Cost FROM Appliances;
Output :
+------------+
| emp_salary |
+------------+
|      30000 |
|      35000 |
|      15000 |
|      10000 |
|      27000 |
|      20000 |
|      40000 |
|      25000 |
|      19000 |
|      18000 |
|       1500 |
+------------+