i

SQL Aliases

Aliases are giving a temporary name to a column in a table or table itself. But it exists only while the query is executing.

Consider the below EMPLOYEE table

EMPLOYEE_ID

FIRST_NAME

LAST_NAME

EMAIL

SALARY

100

Steven

King

SKING

24000

101

Neena

Kochhar

NKOCHHAR

17000

102

Lex

De Haan

LDEHAAN

17000

103

Alexander

Hunold

AHUNOLD

9000

104

Bruce

Ernst

BERNST

6000

105

David

Austin

DAUSTIN

4800

106

Valli

Pataballa

VPATABAL

4800

107

Diana

Lorentz

DLORENTZ

4200

108

Nancy

Greenberg

NGREENBE

12008

109

Daniel

Faviet

DFAVIET

9000

In the below example, the select clause uses an arithmetic expression like salary/2 as an attribute,

Select first_name, salary/2

From employees

Where salary = 17000;

It displays the result-set as follows,

FIRST_NAME

SALARY/2

Neena

8500

Lex

8500

Here replace the salary/2 attribute name to Half_Salary by using aliases.

Example,

Select first_name, salary/2 as Half_Salary

From employees

Where salary = 17000;

FIRST_NAME

HALF_SALARY

Neena

8500

Lex

8500

Here notice the change in column name from salary/2 to Half_salary after using aliases. We can also give a temporary name to the table, which helps to reduce the coding error.