SQL Aliases



SQL Aliases :

  1. SQL Aliases are used to rename the table or column temporarily.
  2. SQL Aliases make table name more readable
  3. SQL Aliases are created using the AS Keyword.

Syntax :

Below is the Syntax for creating Alias for Columns –

SELECT column_name AS alias_name
FROM table_name;

Below is the Syntax for creating Alias for Table –

SELECT column_name(s)
FROM table_name AS alias_name;

Demo Database :

Consider the following demo table ‘EMPLOYEE’-

+----+----------+-----+-----------+--------+---------+
| Id | EmpNAME  | Age | EmpAdd    | ZIP    | EmpSal  |
+----+----------+-----+-----------+--------+---------+
|  1 | Ramesh   |  32 | Kota      | 411028 | 2000.00 |
|  2 | Raj      |  23 | Delhi     | 411013 | 1500.00 |
|  3 | Ram      |  21 | Karachi   | 455345 | 2000.00 |
|  4 | Saurabh  |  25 | Mumbai    | 223456 | 6500.00 |
|  5 | Poonam   |  29 | Bhopal    | 657894 | 8500.00 |
|  6 | Komal    |  23 | Pune      | 334455 | 4500.00 |
|  7 | Arnav    |  24 | Indore    | 778866 | 1000.00 |
+----+----------+-----+-----------+--------+---------+

SQL Alias Example : Rename Table Columns

Consider below query on demo database –

SQL> SELECT  Id, EmpNAME AS ENAME, Age, 
             EmpAdd AS ADDRESS, ZIP, EmpSal
FROM EMPLOYEE;

will result into

+----+----------+-----+-----------+--------+---------+
| Id | ENAME    | Age | ADDRESS   | ZIP    | EmpSal  |
+----+----------+-----+-----------+--------+---------+
|  1 | Ramesh   |  32 | Kota      | 411028 | 2000.00 |
|  2 | Raj      |  23 | Delhi     | 411013 | 1500.00 |
|  3 | Ram      |  21 | Karachi   | 455345 | 2000.00 |
|  4 | Saurabh  |  25 | Mumbai    | 223456 | 6500.00 |
|  5 | Poonam   |  29 | Bhopal    | 657894 | 8500.00 |
|  6 | Komal    |  23 | Pune      | 334455 | 4500.00 |
|  7 | Arnav    |  24 | Indore    | 778866 | 1000.00 |
+----+----------+-----+-----------+--------+---------+

Above SQL statement will change the database table column name temporarily while displaying the table. In the DB the name of Column is still EmpNAME and EmpAdd

SQL Alias Example : Rename Table

If we need to select all the employee having the salary greater than 4000 then we can use the aliase for the table using below query –

Consider below query on demo database –

SQL> SELECT  * from FROM EMPLOYEE e 
     WHERE e.EmpSal > 4000 ;

will result into

+----+----------+-----+-----------+--------+---------+
| Id | EmpNAME  | Age | EmpAdd    | ZIP    | EmpSal  |
+----+----------+-----+-----------+--------+---------+
|  4 | Saurabh  |  25 | Mumbai    | 223456 | 6500.00 |
|  5 | Poonam   |  29 | Bhopal    | 657894 | 8500.00 |
|  6 | Komal    |  23 | Pune      | 334455 | 4500.00 |
+----+----------+-----+-----------+--------+---------+

SQL Alias Example : Merge two columns under single name

Consider below query on demo database –

SQL> SELECT ID,ENAME, EmpAdd + ',' + ZIP AS Address,EmpSal
FROM EMPLOYEE;

will result into

+----+----------+-----------------+---------+
| Id | EmpNAME  | Address         | EmpSal  |
+----+----------+-----------------+---------+
|  1 | Ramesh   | Kota,411028     | 2000.00 |
|  2 | Raj      | Delhi,411013    | 1500.00 |
|  3 | Ram      | Karachi,455345  | 2000.00 |
|  4 | Saurabh  | Mumbai,223456   | 6500.00 |
|  5 | Poonam   | Bhopal,657894   | 8500.00 |
|  6 | Komal    | Pune,334455     | 4500.00 |
|  7 | Arnav    | Indore,778866   | 1000.00 |
+----+----------+-----+-----------+---------+