SQL Aliases

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
By Srikanth Varma
Free
star5
Enrolled: 1000
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
Srikanth Varma
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

As the name suggests, alias is a keyword in SQL which is used as a temporary name for a table or column, while writing a query. Giving aliases makes the table or column name more readable and is also preferred when there are a lot of queries written for a table.

What is an Alias in SQL Statements?

Alias in SQL is basically a temporary name that is given to a table or a column while writing a query. This is usually done when the column or the table names are long, so in order to render more readability, alias is given. The alias in SQL is a temporary change and only exists till the duration of that query.

In SQL alias, when we rename a table name or a column name, the actual table name or column name does not get changed. Aliasing is preferred when there are a lot of queries written for a table.

Column alias is used to make the column heading more readable while the table alias is used to make the table name more readable.

Syntax for Column Alias

In the above syntax, we can see that we have given an alias for the column name.

Syntax for Table Alias

In the above syntax, we can see that we have given an alias for the table name.

For using an alias in SQL, we use the keyword "as" or "AS".

Examples

Consider the following table named "India_Offices".

Office_id_IndiaLocationTarget_AreaEmployeesGlobal_Rank
81BangaloreSales74889
37PuneSoftware34934
67HyderabadCloud2999898
78NoidaMarketing2876
19ChennaiHardware82477

Now we will understand the alias clause by using queries on the above table.

How to Alias a Column Name?

So, we will write a query that will fetch us the Office_id_India column.

Query

Output

o_id
81
37
67
78
19

How to Alias a Table Name?

Consider another table named "Managers". Now we are joining two tables using a common attribute and writing a query for it.

Office_idEmp_idManager_name
8188Prince Chandila
37679Adhya Rathi
671089Dipti Kadam
78999Deepak Rai
191893Jay Bhagat

Query

Output

Office_idTarget_AreaManager_name
81SalesPrince Chandila
37SoftwareAdhya Rathi
67CloudDipti Kadam
78MarketingDeepak Rai
19HardwareJay Bhagat

We can see that the table names have been aliased and used here.

Conclusion

  • Alias in SQL is basically a temporary name that is given to a table or a column while writing a query.
  • Giving aliases makes the table or column name more readable.
  • It is preferred when there are a lot of queries written for a table.
  • In SQL alias, when we rename a table name or a column name, the actual table name or column name does not get changed. It is a temporary change and only exists till the duration of that query.
  • We can give an alias for a table name or a column name.