The SQL ALIAS is used to temporarily rename the table names and column headings. In SQL Server, sometimes column names will not be user-friendly, for instance, we store [First Name] as either [FirstName] or [First_Name]. When we read the data it will annoying to see the column heading like that so, We use the Alias and rename them to more user-friendly (such as [First Name] or [FIRST NAME].
SQL Alias Syntax
The basic syntax to write Alias Names in SQL Server is as shown below:
-- 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]
TIP: In SQL Server Alias, AS Keyword is optional. It is up to you to include it or not.
We are going to use the below-shown data to explain the ALIAS Names in SQL Server with an example.
SQL Alias Example 1
In this example we are going to rename the column names 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]
SQL Alias Example 2
In this example, We added the [FirstName] and [LastName] column to create Full Name and we assigned [Full Name] as the Alias Name.
SELECT [FirstName] + ' ' + [LastName] AS [Full Name] ,[YearlyIncome] ,[Education] ,[Occupation] FROM [Customer]
SQL Alias Example 3
We can also apply the Alias names to calculated columns. 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.