SQL Interview: 16: UNION and Column Aliases

SQL Interview: 16: UNION and Column Aliases

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Intro

Question:

I execute the following query:

SELECT st.SalesTerritoryID AS [ID],
       st.SalesTerritoryName AS [Name],
       st.DateCreated
FROM dbo.SalesTerritories AS st

UNION

SELECT p.ProductID AS [Product],
       p.[Description],
       p.DateCreated AS [StartDate]
FROM dbo.Products AS p;

What alias will be returned for the each column in the returned results?

Answer:

The column aliases are derived from the first query in a UNION statement.

They will be ID, Name, DateCreated.

2025-02-04