What is An Alias in SQL?
Alias in SQL is used to create a temporary name for a table or a column of a table without affecting the original name. To define an alias in SQL, we use the keyword AS. However, this keyword is not mandatory. So, it is also possible to write the alias name immediately without using the AS keyword. As mentioned before, this name is temporary and will be valid only for the duration of that query.
Alias is used when we are dealing with a complex query that has tables or columns with similar names. Alias is also commonly used when we are using aggregate functions such as SUM(), AVG(), COUNT(), etc. Moreover, it is beneficial to use aliases when the names of tables or columns are lengthy or meaningless. By using the alias, the query that we have can be more efficient, readable, and meaningful. Therefore, it will be easier to understand.
To learn more about SQL ALIAS, click here.
Syntax:
SELECT column_name AS alias_name
FROM table_name;
or
SELECT column_name alias_name
FROM table_name;
Example
SELECT w3wiki AS GFG
FROM GFG_Table;
or
SELECT w3wiki GFG
FROM GFG_Table;
Difference Between Alias and Synonym in SQL
In SQL, two keywords are used to create an alternative name which are alias and synonym. Although both of these have similar functions, each of these keywords has its purpose. This means that the Alias has different usage from Synonym and vice versa.
This article aims to clarify the differences between the alias and the synonym and guide on when and how to use each effectively.