SQL Aliases

Sometimes you want to change the column names to make them more readable, SQL aliases are used to change the names of tables or columns headings, it is used as follows:

Syntax

SELECT column_name AS new_name

FROM TableName ;

OR

SELECT column_name

FROM TableName AS new_name ;

Note: The use of the AS keyword is optional but its use is recommended.

Example 1:

SELECT
     [CardType] AS [Card Type]
    ,[CardNumber] AS [Card Number]
FROM [Sales].[CreditCard];

01_Alias

Example 2:

The columns after a concatenation or computing are not named, it is recommended to rename these columns:

  • Without Alias
SELECT
      [Title]
     ,[FirstName]
     ,[LastName]
     ,[FirstName] +' '+ [LastName] 
FROM [Person].[Person] ;

02_Alias

  • With Alias

Here now with the application of the alias:

SELECT [Title]
      ,[FirstName]
      ,[LastName]
      ,[FirstName] +' '+ [LastName] AS [Full Name]
FROM [Person].[Person];

03_Alias

Aliases on a Table

This can help to have shorter names, simpler. This is particularly useful when you use Joins between tables and sometimes it is required in Self-Joins.

You may also like...

Leave a Reply

Your email address will not be published.