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.
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 Alias names concept and rename them to more user-friendly (such as [First Name] or [FIRST NAME]. The syntax to writing SQL Alias Column Names or synonym is
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
In this SQL ALIAS Column names or synonyms example, we are going to rename the headers 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]
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]
We can also apply the Alias names to calculated columns (mathematical calculations). In this example, we will show you how to apply synonym to calculated field.
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 it as the Income.