SQL Alias Syntax

Let’s see, SQL Alias Syntax. In SQL, Alias is used to provide the temporary name to the Database table or column of any table while writing the SQL query.

 

Basically, aliases are created to make column names more readable. The concept of aliasing is quite useful in case of large or complex queries.

SQL Syntax :

SELECT column-name from table-name as alias-name;

We use “as” keyword to provide the alias to any table or column name.

 

SQL Alias Example for Table

Suppose we have the table named “Employees” in our database. We will use the alias for the table name and call the columns on the basis of this Alias.

SQL> select emp.First_Name , emp.Salary from Employees emp;

Alias in SQL

 

You can see, We are using Alias for the table named Employees . We  alias the table to name ’emp’ and using the alias to call the Table’s column name using the dot (.) operator. Using this way you can Alias the table name in SQL.

 

SQL Alias Example for Table Columns

We can provide the Alias for column names too. To provide the alias to column name we need to use ‘AS’ keyword.

SQL> select First_Name ||’ ‘|| Last_Name as Name , Salary as Wage from Employees;

Alias in SQL

 

You can see, In the above example. We alias concatenate the first and last name columns and alias them as ‘Name’ and alias the Salary as ‘Wage’. In the result-set we are getting the alias name instead of column name.

 

If you need to provide the space in Alias then you need to use double quotation marks or square brackets with the Alias.

Example –

SQL> select First_Name ||’ ‘|| Last_Name as “Employee Name” , Salary as Wage from Employees;

Alias in SQL

 

You can see, We are using double quotation marks to print the sapce in Alias name. If you will not use quatation marks then SQL will throw an syntax error.

0 Comment

Leave a Reply