Why we use aliases in SQL?

Query By: DINESH RAI

A

Alex


Use of SQL Alias

For better readability and understandability SQL aliases are used to provide temporary names to the table or table's column. SQL aliases are used with only select queries. In general SQL aliases are used to provide a meaningful and more readable name to a column.


  • SQL alias is created with the 'as' keyword.
  • SQL alias is a temporary name, that exits till that particular query.
  • SQL alias is often used to provide a meaningful name to a column.
  • SQL alias can also be used to provide meaningful names to a table.
  • SQL aliases are used only with select queries.


Example:- 

Let's suppose there is a table (tblEmployee) for employee records, in tblEmployee, there is a column for storing employee salary, the column name is 'employee_s'. Now the column name 'employee_s' is a bit confusing so anyone can think what is the meaning of 'employee_s' so in this SQL alias is the best solution for this problem. We can use any meaningful name at the place of 'employee_s' in the select query, see the below query.


select employee_s as employee_salary from tblEmployee


Now on running the above query, in the result, you will see the column name employee_salary at the place of employee_s. I hope now you can understand why we use SQL aliases.


Rules for Using SQL Alias

  • You can use more than one alias in a SQL query.
  • If SQL alias name contains more than one word then always keep the alias name in double quotation marks or in square brackets, see below for example

In the above example let's suppose you want the alias name 'employee salary' instead of 'employee_salary', the query will be like


select employee_s as [employee salary] from tblEmployee


I hope now you will be able to understand why we use SQL aliases, If still you have any queries regarding this then you can comment below.

0 votes

Your Answer

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

LifeStyle & Fun

© 2024 GDATAMART.COM (All Rights Reserved)