Sql Excel : Subqueries for UNION ALL

The UNION ALL operator almost always demands subqueries, because it requires
that the columns be the same for all tables involved in the union. Consider
extracting the location names from ZipCensus into a single column along with the
type:
SELECT u.location, u.locationtype
FROM ((SELECT DISTINCT stab as location, ‘state’ as locationtype
FROM ZipCensus zc
) UNION ALL
(SELECT DISTINCT county, ‘county’ FROM ZipCensus zc
) UNION ALL
(SELECT DISTINCT zipname, ‘zipname’ FROM ZipCensus zc
)
) u
This example uses subqueries to ensure that each part of the UNION ALL has the
same columns. Also, note that the column names are taken from the first
subquery, so they are not needed in the subsequent subqueries.