![]() |
SQL UNION and UNION ALL operators are used to concatenate results of multiple SELECT statements. However, they are different from each other. One key difference between UNION and UNION ALL in SQL is that the UNION command removes duplicates from the final results set, whereas the UNION ALL command allows duplicates in the results set. Here we will explore the difference between UNION and UNION ALL in SQL. Difference Between UNION and UNION ALLUNION and UNION ALL, both commands are used to combine the result of two or more SELECT Statements, but there are many difference in UNION and UNION ALL.
SQL UNION OperatorSQL UNION Operator is used to combine the set of one or more SELECT statements as the resulting. The UNION operator removes duplicates from the combined result from the set of SELECT statements. The conditions for a UNION statement are that the columns in the SELECT statement need to be in the same order, and the data types should be compatible. It is important to match the number of columns and their data types across different SELECT statements. Syntax
Explanation: In the Above query, It combines the results of two SELECT statements using the UNION operator. It retrieves data from columns “column1,” “column2,” and “column3” from both “table1” and “table2,” eliminating duplicate rows from the result set. UNION ALL OperatorSQL UNION ALL operator is also used to combine the set of one or more select statements as the result. The difference between UNION and UNION ALL is that in the UNION ALL operator there are duplicates in the result sets of SELECT statements whereas in the UNION operator, there are no duplicate values. The UNION ALL is faster than the UNION statement because in UNION ALL there is no additional step of eliminating duplicates. Syntax
Explanation: In the Above query, It combines the results of two SELECT statements using the UNION ALL operator. It retrieves columns “column1,” “column2,” and “column3” from “table1” and appends the same columns from “table2” to the result set. UNION and UNION ALL Operator ExamplesLet’s look at some examples of the UNION and UNION ALL commands in SQL, and understand how they differ from each other. We will be using the customer table, shown below: ![]() Customer Table SQL UNION ExampleIn this example, we will retrieve and combine rows with CustomerID between 1 and 5 from the “customer” table using the UNION operator. Query: SELECT ∗ FROM customer Output: ![]() Output of Union Example 1 Explanation:
SQL UNION ALL ExampleIn this example, we will retrieves rows with CustomerID between 1 and 5 from the “customer” table and combines them with duplicate rows using the UNION ALL operator Query: SELECT ∗ FROM customer Output: ![]() Output of UNION ALL Example 1 Explanation:
ConclusionThis article covered the differences between UNION and UNION ALL Operator along with their examples. The UNION removes duplicate rows from the result set while the UNION ALL remains all rows, including duplicates, without any elimination. Also Both UNION and UNION ALL are widely supported across various relational database management systems (RDBMS). |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |