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 2014 Standard Edition Server w/20 CAL’s

Price: $641.99
Buy It Now
SQL Server 2014 Standard Edition, Media & License | Full Retail | 10CAL Edition

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

Price: $995
Buy It Now
Microsoft SQL Server 2008 R2 Workgroup + 5 CAL

Price: $249.99
Buy It Now
Microsoft SQL 2008 R2 Server Standard 25 CAL 32/64-Bit NEW SEALED

Price: $350
Buy It Now
Microsoft SQL Server 2008 R2 Standard 10CAL Server License key FactorySealedPack

Price: $699
Buy It Now







Copyright 2017 - TSQLTutorials.com