🚀 Try Zilliz Cloud, the fully managed Milvus, for free—experience 10x faster performance! Try Now>>

What is the difference between UNION and UNION ALL in SQL?

In SQL, UNION and UNION ALL are used to combine results from multiple SELECT statements, but they differ in how they handle duplicates and performance. UNION merges results from two or more queries and automatically removes duplicate rows, ensuring each row in the final output is unique. In contrast, UNION ALL simply appends all rows from the queries, including duplicates. Both require the SELECT statements to have the same number of columns with compatible data types. The key distinction is that UNION adds a step to eliminate duplicates, which impacts performance, while UNION ALL skips this step, making it faster for large datasets.

For example, consider two tables: Employees_DepartmentA with rows (1, ‘Alice’), (2, ‘Bob’), and Employees_DepartmentB with rows (2, ‘Bob’), (3, ‘Charlie’). Using SELECT * FROM Employees_DepartmentA UNION SELECT * FROM Employees_DepartmentB returns three rows: (1, ‘Alice’), (2, ‘Bob’), (3, ‘Charlie’), with the duplicate (2, ‘Bob’) removed. If you use UNION ALL instead, the result includes all four rows: (1, ‘Alice’), (2, ‘Bob’), (2, ‘Bob’), (3, ‘Charlie’). This illustrates how UNION filters duplicates, whereas UNION ALL preserves all data, even if rows are identical.

When deciding which to use, prioritize UNION ALL if duplicates are irrelevant or impossible (e.g., when combining disjoint datasets) to avoid the unnecessary overhead of deduplication. Use UNION only when unique rows are required, such as aggregating user emails from multiple tables where duplicates must be excluded. Keep in mind that UNION’s deduplication process can slow down queries, especially with large datasets, as it involves sorting and comparing rows. Always validate that the column structures match between SELECT statements, as mismatches will cause errors regardless of the operator chosen.

Like the article? Spread the word

How we use cookies

This website stores cookies on your computer. By continuing to browse or by clicking ‘Accept’, you agree to the storing of cookies on your device to enhance your site experience and for analytical purposes.