blog post

An Alias is a shorthand for a table or column name.
Aliases reduce the amount of typing required to enter a query.
Complex queries with aliases are generally easier to read.
Aliases are useful with JOINs and aggregates: SUM, COUNT, etc.
An Alias only exists for the duration of the query.

SQL Alias Examples

Problem: List total customers in each country.
Display results with easy to understand column headers.
SELECT COUNT(C.Id) AS TotalCustomers, C.Country AS Nation
  FROM Customer C
 GROUP BY C.Country

TotalCustomers and Nation are column aliases.
The table alias (C) in this example is not particularly useful.
Problem: List the total amount ordered by customer with easy to read column headers

SELECT C.Id AS Identifier, C.LastName + ', ' + C.FirstName AS CustomerName, 
       SUM(O.TotalAmount) AS TotalSpent
  FROM [Order] O JOIN Customer C ON O.CustomerId = C.Id
 GROUP BY C.Id, C.LastName + ', ' + C.FirstName
 ORDER BY TotalSpent DESC

The aliases significantly simplify writing the JOIN and ORDER BY clauses.
The C alias in C.Id helps identify the Customer Id rather then the Order Id.

  • Share This Story


Pleas give your valuable comments to improve the contents.

about author

Atul kumar shukla


I'm Atul Kumar Shukla, working as a web developer.
Welcome to my Blog. This is my personal blog where i post my own questions and answers , articles about ASP.Net., SQL Server, HTML, CSS, Jquery and many more..

Visit Profile

Leave a comment

Name
E-mail
Url
Message

Get More Stuff
Subscribing to the TheOneblogs newsletter and get intersting stuff and updates to your email inbox.
Your Email

 

Style Switcher

Change Color

×! We use cookies to improve your user experience. More information