T-SQL Tutorials - TSQLTutorials.com

Aliases - naming and renaming


Introduction

Aliases are used in TSQL to rename things. There are two main types of aliases. Aliases can be used to rename columns within a query, or name columns. Aliases can also be used to name datasets, or rename tables within a query. The keyword AS can be used for created aliases, however it is optional.

Column Aliases

Often in a result set you may want to rename a column, so that it makes more sense in a particular context. For example you may want to refer to the column 'EMPID' as 'Employee_ID'. The following two examples will rename the column within the dataset, both are the same, with the first using the optional AS keyword.
Using AS keyword SELECT EMPID AS Employee_ID
FROM Employees
Without using AS keyword SELECT EMPID Employee_ID
FROM Employees
Another very common use Column Aliases, is the give names to computed expressions. For example the expression '[Income] - [Expenses]' can be named 'Profit'. The following two examples will name the column 'Profit' within the dataset, both are the same, with the first using the optional AS keyword.
Using AS keyword SELECT (Income - Expenses) AS Profit
FROM Sales
Without using AS keyword SELECT (Income - Expenses) Profit
FROM Sales




Table Aliases

Table Aliases are very helpful in cases when you have to rename a table, or want to rename a table, in a query for convenience sake. First of all some table names can be very long and inconvenient to type out. An example of a long table name could be: '2006_NorthAmerican_Sales'. Now this table name isn't to long if you have to type it out once, but if you have to reference it many times in a query, it could become tedious. This example will show how to create a table alias and use it: Using AS keyword SELECT
  sales.Item,
  sales.Price,
  sales.Quantity,
  sales.Date,
  sales.City,
  sales.State,
  sales.Store
FROM 2006_NorthAmerican_Sales AS sales
Without using AS keyword SELECT
  sales.Item,
  sales.Price,
  sales.Quantity,
  sales.Date,
  sales.City,
  sales.State,
  sales.Store
FROM 2006_NorthAmerican_Sales sales
An additional advantage of using Table Aliases is that you can substitute another table in a query with minimal effort (change 2006_NorthAmerican_Sales to 2007_SouthAmerican_Sales: SELECT
  sales.Item,
  sales.Price,
  sales.Quantity,
  sales.Date,
  sales.City,
  sales.State,
  sales.Store
FROM 2007_SouthAmerican_Sales sales


Microsoft SQL Server 2008 R2 Standard Server License key +5CAL FactorySealedPack

Price: $495
Buy It Now
Microsoft SQL Server 2012 Standard 10CAL +Server License Key Factory Sealed Pack

Price: $949
Buy It Now
Add-On Microsoft SQL Server 2008 R2 5CAL Standard Enterprise Datacenter Sealed!

Price: $199
Buy It Now
Brand New - Microsoft SQL Server 2014 Standard Edition SP2 and 25 CAL License

Price: $899
Buy It Now
Factory Sealed- 228-10602 Microsoft SQL Server 2016 Standard 10 CAL NEW!!

Price: $2169.99
Buy It Now
Microsoft SQL Server 2005 Standard IA64 5 CAL, Itanium System, New, PN 228-04025

Price: $899.95
Buy It Now







Copyright 2017 - TSQLTutorials.com