Search This Blog

Using Aliases in Select Queries

Aliases are useful for:
  • Making your query results easier to read
  • Can make your query easier to read especially when you have multiple joins
  • Giving meaningful names to columns
  • Giving names to aggregated columns
There is some debate over how aliases should be created.  Some prefer using = and other AS.  My preference is to use AS because this is consistent with how other aliases work in SQL Server such as common table expressions.  The methods are:

  • AliasName = ColumnName 
  • ColumnName AS AliasName

Here are some examples of how to use aliases in your queries:

First using an alias to give a column another name:



This query will give you the following results, notice the salesorderid has the label OrderNo and the TotalDue column has the label OrderValue:




The following query uses aliases for the table names; this makes it easy to see which table each column is coming from as well as saving on typing:


 










Finally here's a simple example of using an alias to name an aggregated column otherwise it wouldn't have a column heading:






 

No comments:

Post a Comment