Unioning two tables with different number of columns

Jack Kada picture Jack Kada · Feb 22, 2010 · Viewed 184.8k times · Source

I have two tables (Table A and Table B).

These have different number of columns - Say Table A has more columns.

How can I union these two table and get null for the columns that Table B does not have?

Answer

Kangkan picture Kangkan · Feb 22, 2010

Add extra columns as null for the table having less columns like

Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2