Logo

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

When you need to combine the results of two or more SELECT statements in SQL, you’ll often reach for either UNION or UNION ALL. At first glance, they may seem interchangeable, but there are key differences in terms of duplicate handling and performance.

UNION

  • Removes Duplicate Rows: By default, UNION filters out any duplicates from the combined result set.
  • Sorting Operation: It effectively performs a DISTINCT on the result, which requires additional sorting/processing under the hood.
  • Use Case: Ideal when you explicitly need to eliminate duplicates and have relatively small result sets (or are willing to pay the cost of deduplication).

Example:

SELECT name FROM TableA UNION SELECT name FROM TableB;
  • If name exists in both tables, you’ll only see one occurrence of that name in the result.

UNION ALL

  • Keeps All Rows: It does not remove duplicates; every row from each query is returned.
  • Faster: Because it doesn’t have to check for duplicates, UNION ALL typically outperforms UNION.
  • Use Case: Best when you don’t care about duplicates or you know there won’t be any. Useful for large datasets where speed is paramount.

Example:

SELECT name FROM TableA UNION ALL SELECT name FROM TableB;
  • If name exists in both tables, you’ll see both occurrences in the combined result.

Performance Implications

  • UNION: The database must perform a distinct operation to remove duplicates, which can involve sorting or hashing. This step can be expensive for large result sets.
  • UNION ALL: No deduplication is done, making this approach more efficient for large data volumes or when duplicates are acceptable (or even needed).

Choosing the Right One

  1. Need Unique Rows? Use UNION.
  2. Handling Large Datasets? If duplicates are acceptable (or absent), opt for UNION ALL to improve performance.
  3. Know Your Data: If your queries naturally produce unique rows, UNION may waste resources filtering duplicates that don’t exist.

Further Mastery of SQL

To deepen your SQL and database knowledge, including advanced querying, indexing, and performance tuning, consider these courses from DesignGurus.io:

Whether you’re optimizing queries or preparing for a technical interview, a solid understanding of UNION vs. UNION ALL will help you write accurate, high-performance SQL. By picking the right operation for your specific use case, you’ll ensure both correctness and efficiency in your queries.

CONTRIBUTOR
TechGrind