Unioning two tables with different number of columns
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A SQL UNION works only when both queries return the same number of columns in the same order, with compatible data types. If two tables have different shapes, the solution is not a special kind of union. The solution is to reshape both SELECT statements so they project one shared output schema.
Why the Direct UNION Fails
Suppose you have:
- '
employees(id, name, age)' - '
customers(user_id, first_name, last_name, email)'
This direct union fails:
The database rejects it because one side returns three columns and the other side returns four.
Build One Shared Result Shape
The normal fix is to decide what the final result should look like and then make both queries match that shape.
Now both SELECT statements return four columns in the same order, so the union works.
The important change is not the keyword. It is the explicit output design.
Use NULL, Literals, and Expressions Deliberately
When one source lacks a column that the other source has, use NULL or a fixed value to fill the gap. When the two sources store equivalent information differently, transform one side into a comparable form with an expression.
That is why CONCAT(first_name, ' ', last_name) is useful in the example above. It reshapes the customer row into the same conceptual field as the employee display name.
Prefer UNION ALL Unless You Need Deduplication
UNION removes duplicates. UNION ALL keeps every row. If your real goal is simply to append two differently shaped sources into one result, UNION ALL is often better because it avoids unnecessary deduplication work.
Use plain UNION only when duplicate removal is part of the business requirement.
Make Data Types Compatible
Matching column count is only one rule. Each column position should also have compatible types. If one query returns an integer and the other returns text in the same position, the database may cast automatically or it may fail depending on the engine and types involved.
When needed, cast explicitly:
Explicit casts make the result easier to reason about and reduce engine-dependent surprises.
Add a Source Column When It Helps
When rows from several sources are mixed together, it is often useful to keep track of where each row came from.
That small addition can make downstream reporting and debugging much easier.
Common Pitfalls
Assuming SQL can union tables with different column counts without reshaping the queries first is the root misunderstanding.
Padding columns mechanically with NULL values without deciding what the shared result actually means usually creates confusing output.
Using UNION when UNION ALL would be faster and more faithful to the source data can hide duplicate rows unexpectedly.
Ignoring data-type compatibility can make the query brittle across database engines.
Thinking in terms of "merging tables" instead of "projecting one shared result schema" leads to poor query design.
Summary
- '
UNIONrequires the same number of columns on both sides.' - Reshape each
SELECTso both queries return one shared output structure. - Use
NULL, literals, expressions, and casts to align missing or different columns. - Prefer
UNION ALLunless duplicate removal is required. - Design the result around a meaningful shared schema, not just around satisfying SQL syntax.

