
UNION and UNION ALL in SQL are used to retrieve data from two or more tables. UNION returns distinct records from both the table, while UNION ALL returns all the records from both the tables. In this article, we will explore the difference between UNION and UNION ALL.
UNION and UNION ALL in SQL are set operators that combine the result of two SELECT queries. Both share standard features, with one significant difference: UNION only returns a unique record, while UNION ALL returns all the records (including duplicates). This article will explore the difference between UNION and UNION ALL.
Must Check: Free Database and SQL Online Course and Certificates
Must Check: SQL tutorial
Table of Content
UNION vs. UNION ALL
Parameter | UNION | UNION ALL |
Definition | Combines the result from the multiple table and return the distinct records into a single result set. | Combines the result from the multiple table and return all the record into a single result set. |
Removal of dulicates | It has a default feature to eliminate duplicate rows from the table. | It can’t eliminate the duplicate rows from the table. |
Performance | Slow performance as it removes duplicate rows from the output. | Fast performance as compare to UNION. |
Syntax | SELECT column_names FROM table_1 WHERE conditions UNION SELECT column_names FROM table_2 WHERE conditions |
SELECT column_names FROM table_1 WHERE conditions UNION ALL SELECT column_names FROM table_2 WHERE conditions |
Read Also: Update Query in SQL
What is UNION?
UNION in SQL combines data from the result set of two SELECT queries into a single distinct result set. The result set to produce from UNION doesn’t contain any duplicate value.
Syntax
SELECT column_namesFROM table_1WHERE conditions
UNION
SELECT column_namesFROM table_2WHERE conditions;
Must Read: SQL Logical Operator
What is UNION ALL?
UNION ALL is an extension of UNION in SQL. As the result set of UNION of two tables doesn’t contains all the records from both the table, so UNION ALL comes into the picture that returns all the records from both the table (including duplicate rows also).
Syntax
SELECT column_namesFROM table_1WHERE conditions
UNION ALL
SELECT column_namesFROM table_2WHERE conditions;
Also Read: How to use UNION in SQL
Rules
- Both the SELECT statement must have the same number of columns and the same data type.
- Columns in both the SELECT statement must be in the same order.
- The names of the columns can be different, but the final result set will show the column name of the first select query.
Let’s understand the difference between UNION and UNION ALL by an example.
Must Read: SQL LIMITS
Example
Let us have two tables of employee names and manager names. Combine both the table using UNION and UNION ALL.
Employee
Employee_id | Name |
1 | Ram |
2 | Shyam |
3 | Joya |
4 | Vidit |
Manager
Employee_id | Name |
1 | Ram |
4 | Vidit |
UNION
SELECT Employee_id, nameFROM EmployeeUNIONSELECT *FROM Manager;
output
Employee_id | Name |
1 | Ram |
2 | Shyam |
3 | Joya |
4 | Vidit |
UNION ALL
SELECT Employee_id, nameFROM EmployeeUNIONSELECT *FROM Manager;
output
Employee_id | Name |
1 | Ram |
2 | Shyam |
3 | Joya |
4 | Vidit |
1 | Ram |
4 | Vidit |
Must Read: All about DML Command in SQL
Must Read: SQL ACID Properties
Key differences
- Both UNION and UNION ALL combine the result of two or more tables.
- The result set of UNION does not contain duplicate rows, while the result set of UNION ALL returns all the rows from both tables.
- The execution time of UNION ALL is less than the execution time of UNION as it does not removes the duplicate rows.
Conclusion
UNION and UNION ALL in SQL are used to retrieve data from two or more tables. UNION returns distinct records from both the table, while UNION ALL returns all the records from both the tables.
In the article, we have briefly discussed UNION and UNION ALL, the differences between them, and examples.
Hope, you will like the article.
Keep Learning!!
Keep Sharing!!
Related Reads:
FAQs
What is UNION in SQL?
UNION in SQL combines data from the result set of two SELECT queries into a single distinct result set. The result set to produce from UNION does not contain any duplicate value.
What is UNION ALL in SQL?
UNION ALL is an extension of UNION in SQL. As the result set of UNION of two tables doesnu2019t contains all the records from both the table, so UNION ALL comes into the picture that returns all the records from both the table (including duplicate rows also).
What is the difference between UNION and UNION ALL?
The result set of UNION does not contain duplicate rows, while the result set of UNION ALL returns all the rows from both tables. The execution time of UNION ALL is less than the execution time of UNION as it does not removes the duplicate rows.
Download this article as PDF to read offline
Download as PDF
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio
Comments