Mike Pala Mike Pala - 2 months ago 15
SQL Question

How to build "empty columns" for UNION?

from what I understand "each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order." Well what if the first SELECT has more columns than the 2nd one can generate. Here's what I mean: let's say I want to

SELECT "City", "Country", "Continent" from table1
UNION
SELECT "City", "Country" from table2


...let's say table 2 does not contain a column called "Continent" but for my needs it's fine for the records that come from table2 to have a blank or NULL in that column. I am using dashDB.

Answer

You can always add "virtual" columns:

SELECT "City", "Country", "Continent" from table1  
UNION  
SELECT "City", "Country", NULL AS "Continent" from table2 
Comments