SQL ALIAS is used to rename the table names and column headings temporarily. In Tables, 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 for writing SQL Server Alias Column Names or synonyms 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, SQL ALIAS column names, AS Keyword is optional. It is up to you to include it or not. We use this data to explain the concept.

Customer Table with meaningful names

SQL Server Alias Example

In this 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]
      ,[Occupation] AS [Profession]
  FROM [Customer]
SQL ALIAS Names Example 1

We added the [FirstName] and [LastName] SQL columns to create Full Name. Next, we assigned [Full Name] as the Alias Name.

SELECT [FirstName] + ' ' + [LastName] AS [Full Name]
FROM [Customer]
Synonym Example 2

We can also apply the Alias names to calculated columns (mathematical calculations). In this example, we will show you how to apply synonyms to the calculated field.

SELECT [FirstName] 
      ,([YearlyIncome]  + 15000) AS Income
  FROM [Customer]

We added 15000 to each and every record of the [YearlyIncome] column and renamed it as the Income.

Categories SQL