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 |
Related Databases job interview questions
How would you store 1 million phone numbers?
Data StructuresDatabases JuniorDescribe and tell the difference amongst table, view and materialized view.
Databases MediorWhat is the difference between UNION and UNION ALL?
DatabasesSQL MediorWhat is the processing order of logical parts of the following query?
DatabasesSQL MediorWhen you use SQL Server, what is a difference between local and global temporary table?
DatabasesMS SQL ServerSQL Senior