SQL UNION ALL: Combining Results with Duplicates

SQL UNION ALL is a powerful tool for combining the results of multiple SELECT statements into a single result set. Unlike the UNION operator, which removes duplicate records, UNION ALL includes all duplicates. This makes UNION ALL a more efficient choice when duplicate records are acceptable or necessary.

Understanding SQL UNION ALL

The SQL UNION ALL command merges the results of two or more SELECT statements. It’s essential that all SELECT statements involved have the same number of columns, and the columns must be of compatible data types. If the number of columns or data types don’t match, an error will occur.

Syntax:

SELECT columns FROM table1
UNION ALL
SELECT columns FROM table2;

SQL UNION ALL vs UNION

Here’s a comparison between UNION ALL and UNION:

FeatureUNION ALLUNION
Duplicate RecordsIncludes all duplicatesRemoves duplicate records
PerformanceFaster, as it doesn’t check for duplicatesSlower, due to duplicate elimination
Use CaseWhen duplicates are acceptable or neededWhen duplicates need to be removed
SyntaxSELECT columns FROM table1 UNION ALL SELECT columns FROM table2;SELECT columns FROM table1 UNION SELECT columns FROM table2;
Memory UsageGenerally lower, due to no extra processing for duplicatesHigher, due to additional steps for duplicate removal
Result SetCombined rows from all SELECT statements, including duplicatesCombined rows from all SELECT statements, without duplicates
ApplicabilityUseful for large datasets where performance is critical and duplicates are acceptableUseful when data integrity requires unique records in the result set

Examples of SQL UNION ALL

Let’s look at some practical examples of the UNION ALL command.

Example 1: Single Field with Same Name

Suppose we want to combine the names from both the STUDENTS and TRIP_DETAIL tables, including all names, even if there are duplicates.

Tables:

STUDENTS Table:

ROLL_NONAMEDOBAGE
1DEV SHARMA2001-08-1617
2AMAN VERMA2002-01-0416
3KRISH VATSA2000-11-2918

TRIP_DETAIL Table:

ROLL_NONAMEDOBAGE
1DEV SHARMA2001-08-1617
2AMAN VERMA2002-01-0416
3KRISH VATSA2000-11-2918
4VARUN GOYAL2003-09-2115

Query:

SELECT NAME FROM STUDENTS
UNION ALL
SELECT NAME FROM TRIP_DETAIL;

Output:

NAME
DEV SHARMA
AMAN VERMA
KRISH VATSA
DEV SHARMA
AMAN VERMA
KRISH VATSA
VARUN GOYAL

Explanation:
The UNION ALL operator combines all rows from the NAME column in both tables. It includes all names, including duplicates. Names like “DEV SHARMA,” “AMAN VERMA,” and “KRISH VATSA” appear twice because they exist in both tables.

Example 2: Different Field Names

Suppose we want to combine the ROLL_NO from both tables and align the column names for consistency.

Query:

SELECT ROLL_NO AS Identifier FROM STUDENTS
UNION ALL
SELECT ROLL_NO AS Identifier FROM TRIP_DETAIL;

Output:

Identifier
1
2
3
1
2
3
4

Explanation:
Here, ROLL_NO from both tables is selected and aliased as Identifier. The UNION ALL operator combines all roll numbers from both tables, including duplicates.

Important Points About SQL UNION ALL

  • The UNION ALL command combines results from two or more SELECT statements from different tables.
  • Unlike UNION, UNION ALL includes duplicate records from the SELECT statements.
  • Both SELECT statements must have an equal number of columns, and these columns must be of compatible data types.

Conclusion

The UNION ALL operator is a valuable tool for combining results from multiple queries while preserving all rows, including duplicates. It is particularly useful when aggregating data from similar sources or when duplicates are acceptable. The examples provided illustrate how UNION ALL operates with columns of the same and different names.

FAQs

Can UNION ALL be used with different data types?
No, the columns being combined with UNION ALL must have compatible data types. If they differ, you may need to cast or convert the data types to make them compatible.

How does UNION ALL handle NULL values?
UNION ALL will include NULL values in the result set. If NULL values exist in the result sets of the queries being combined, they will appear in the final result set.

Can UNION ALL be used with more than two tables?
Yes, UNION ALL can combine results from more than two SELECT queries. You can chain multiple SELECT queries with UNION ALL to aggregate data from several sources.