Alias Syntax

You can rename a table or a column in a database temporarily by giving them another pseudo name. This pseudo name is known as Alias. The use of aliases is to address a specific table or a column in an SQL statement without changing their original name in the database. Aliases are created with the AS keyword.

Aliases can be especially useful when working with complex queries involving multiple tables or columns with similar names. By assigning temporary names to these tables or columns, you can make your SQL query more readable and easier to understand.

The SQL Aliasing

Aliases are used to address database tables with a shorter or more meaningful name within an SQL query. The basic syntax of a table alias is as follows.

SELECT column1, column2....FROM table_name AS alias_name;

Example

Assume we have created a table with name CUSTOMERS in MySQL database using CREATE TABLE statement as shown below −

CREATETABLE CUSTOMERS (
   ID INTNOTNULL,
   NAME VARCHAR(20)NOTNULL,
   AGE INTNOTNULL,
   ADDRESS CHAR(25),
   SALARY DECIMAL(18,2),PRIMARYKEY(ID));

Following query inserts values into this table using the INSERT statement −

INSERTINTO CUSTOMERS VALUES(1,'Ramesh',32,'Ahmedabad',2000.00),(2,'Khilan',25,'Delhi',1500.00),(3,'Kaushik',23,'Kota',2000.00),(4,'Chaitali',25,'Mumbai',6500.00),(5,'Hardik',27,'Bhopal',8500.00),(6,'Komal',22,'Hyderabad',4500.00),(7,'Muffy',24,'Indore',10000.00);

The CUSTOMERS table obtained is as follows −

IDNAMEAGEADDRESSSALARY
1Ramesh32Ahmedabad2000.00
2Khilan25Delhi1500.00
3Kaushik23Kota2000.00
4Chaitali25Mumbai6500.00
5Hardik27Bhopal8500.00
6Komal22Hyderabad4500.00
7Muffy24Indore10000.00

Now, we are creating the second table ORDERS using CREATE TABLE statement as shown below −

CREATETABLE ORDERS (
   OID INTNOTNULL,
   DATES DATETIMENOTNULL,
   CUSTOMER_ID INTNOTNULL,
   AMOUNT INTNOTNULL,PRIMARYKEY(OID));

Following query inserts values into this table using the INSERT statement −

INSERTINTO ORDERS VALUES(102,'2009-10-08 00:00:00',3,3000),(100,'2009-10-08 00:00:00',3,1500),(101,'2009-11-20 00:00:00',2,1560),(103,'2008-05-20 00:00:00',4,2060);

The ORDERS table obtained is as shown below −

OIDDATECUSTOMER_IDAMOUNT
1022009-10-08 00:00:0033000.00
1002009-10-08 00:00:0031500.00
1012009-11-20 00:00:0021560.00
1032008-05-20 00:00:0042060.00

Now, the following query shows the usage of a table alias. The CUSTOMERS table is aliased as ‘C’ and the ORDERS table is aliased as ‘O’ −

SELECT C.ID, C.NAME, C.AGE, O.AMOUNT 
FROM CUSTOMERS AS C, ORDERS AS O
WHERE  C.ID = O.CUSTOMER_ID;

Output

This would produce the following result −

IDNAMEAGEAMOUNT
3Kaushik233000.00
3Kaushik231500.00
2Khilan251560.00
4Chaitali252060.00

Aliasing Column Names

We can also use an alias for a column name in SQL to give it a different name in the result set of a query. The basic syntax of a column alias is as follows −

SELECT column_name AS alias_name
FROM table_name;

Example

Following is the usage of a column alias. Here, the NAME column is aliased as ‘CUSTOMER_NAME’ −

SELECT ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
FROM CUSTOMERS;

Output

This would produce the following result −

CUSTOMER_IDCUSTOMER_NAME
1Ramesh
2Khilan
3Kaushik
4Chaitali
5Hardik
6Komal
7Muffy

Aliasing with Self Join

The SQL Self Join is used to join a table to itself as if the table were two tables. During this process, we need to use alias for one of the tables with a temporary name to avoid misunderstandings. This renaming is done using aliases.

Syntax

Following is the syntax for performing a self-join with aliases −

SELECT column_name(s)FROM my_table a, my_table b
ON a.join_column = b.join_column;

Example

Now, let us join the CUSTOMERS table to itself using the following Self Join query. Our aim is to establish a relationship among customers on the basis of their earnings. In here, we are using aliases with column names as well as with the table names −

SELECT 
   a.ID, b.NAME as EARNS_HIGHER, 
   a.NAME as EARNS_LESS, 
   a.SALARY as LOWER_SALARY
FROM CUSTOMERS a, CUSTOMERS b
WHERE a.SALARY 
Output
Output of the above query isas follows −


ID
EARNS_HIGHER
EARNS_LESS
LOWER_SALARY


2
Ramesh
Khilan
1500.002
Kaushik
Khilan
1500.006
Chaitali
Komal
4500.003
Chaitali
Kaushik
2000.002
Chaitali
Khilan
1500.001
Chaitali
Ramesh
2000.006
Hardik
Komal
4500.004
Hardik
Chaitali
6500.003
Hardik
Kaushik
2000.002
Hardik
Khilan
1500.001
Hardik
Ramesh
2000.003
Komal
Kaushik
2000.002
Komal
Khilan
1500.001
Komal
Ramesh
2000.006
Muffy
Komal
4500.005
Muffy
Hardik
8500.004
Muffy
Chaitali
6500.003
Muffy
Kaushik
2000.002
Muffy
Khilan
1500.001
Muffy
Ramesh
2000.00

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *