Yogesh
Yogesh

Reputation: 3482

UNION with dissimilar columns

TABLE A

ID    Name    Age
1     John       22

TABLE B

ID   Name
5    Erik

I want result like

ID    Name    Age
1     John    22
5     Erik    

When I am performing UNION giving error

"All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists."

How to get desired result?

Upvotes: 9

Views: 8440

Answers (1)

Martin Smith
Martin Smith

Reputation: 452978

You could supply a dummy column in lieu of the missing one that returns NULL as below.

SELECT ID,
       Name,
       Age
FROM   TABLE_A
UNION ALL
SELECT ID,
       Name,
       NULL
FROM   TABLE_B  

Upvotes: 16

Related Questions