The SQL ALIAS is used to rename the table names and column headings temporarily. In SQL Server, sometimes column names will not be user-friendly. So, we have to change them using SQL ALIAS.
For instance, we store [First Name] as either [FirstName] or [First_Name]. When we read that first name data, it was annoying to see the column heading like that. So, We use this SQL Server Alias names concept and rename them to more user-friendly (such as [First Name] or [FIRST NAME]. The syntax to writing Alias Column Names is
-- SQL Server ALIAS Syntax SELECT [Column_Name] AS [Name] FROM [Table_Name] --OR You can Simply Write without using AS Keyword SELECT [Column_Name] [Name] FROM [Table_Name]
In a query, Alias, AS Keyword is optional. It is up to you to include it or not. We use this data to explain the SQL ALIAS Column Names.
SQL Alias Example 1
In this example, we are going to rename the column names in the above-specified table to read them easily. We are going to rename [FirstName] to [First Name], [LastName] to [Last Name], [YearlyIncome] to [Yearly Income] and [Occupation] to [Profession]
SELECT [FirstName] AS [First Name] ,[LastName] AS [Last Name] ,[YearlyIncome] AS [Yearly Income] ,[Education] ,[Occupation] AS [Profession] FROM [Customer]
Alias Column Example 2
We added the [FirstName] and [LastName] SQL column to create Full Name. Next, we assigned [Full Name] as the Alias Name.
SELECT [FirstName] + ' ' + [LastName] AS [Full Name] ,[YearlyIncome] ,[Education] ,[Occupation] FROM [Customer]
Alias Columns Example 3
We can also apply the Alias names to calculated columns (mathematical calculations). In this example, we will show you how to apply Alias names to calculated columns.
SELECT [FirstName] ,[LastName] ,([YearlyIncome] + 15000) AS Income ,[Education] ,[Occupation] FROM [Customer]
We added 15000 to each and every record of the [YearlyIncome] column and renamed the column name as Income.