SQL ALIAS Tutorial
ALIAS are used in SQL query for alternate names for actual table names and column names.Using an ALIAS in any SQL Query is completely optional and the only purpose of the ALIAS is to make SQL query looks more simple and easy to understand. ALIAS is used to assign simple and short names for complex or long tables and columns.
In a query you may have to fetch columns from various tables. If the column is present in multiple tables then you need to specify the tablename along with the columnname while retrieving rows. Hence making SQL query long and complex in appearance. The other use of ALIAS can be while using derived columns or derived tables. Sometime we perform various operations on column, in that case to give simple name to columns we can use ALIAS. Let us see an example below:
Without using ALIAS:
SELECT CUSTOMERS.CUST_CITY, CUSTOMERS.CUST_OPERATOR, CUSTOMERS.CUST_NAME, ‘1000’||CUSTOMERS.CUST_ID FROM CUSTOMERS;
SELECT C.CUST_CITY, C.CUST_OPERATOR, C.CUST_NAME, ‘1000’||C.CUST_ID as NEW_CUST_ID FROM CUSTOMERS C;
Using Alias in any SQL Query is considered as good practice. However Alias name should be relevant and easy to read and understand. Also, once the ALIAS is created for a table, then every where the ALIAS name should be used instead of tablename for selecting columns. We will see later what problem we may face if we are not using ALIAS name everywhere after creating it. The keyword ‘AS’ is used to create ALIAS for tables and columns however it is completely optional to use.