Can you use different column names for tables merged using UNION ALL? What will be the column names in the result?

Experience Level: Medior
Tags: DatabasesSQL

Answer

Explanation

You can use different column names. The 2 datasets are merged by a column index. The data types of the columns on the same column index must match, the names can be however different.

The result merged dataset will use the column names of the first dataset.

Example

SELECT 1 ClientId, 'Peter' ClientName
UNION ALL
SELECT 2 SomeClientId, 'Jill' Client

-- The output will be the following:

ClientId ClientName
1 Peter
2 Jill


 

Comments

No Comments Yet.
Be the first to tell us what you think.