SQL Alias

SQL Alias are the temporary names given to table or column for the purpose of a specific SQL query.

SQL Alias name specifies to make table or column name are more readable. Alias assigning is optional not a compulsory.

You can temporary assign another name to a table or a column name for a duration of SELECT query using as alias name.

When you joining one or more table that time assign alias name of a table or a column name to make easier to read.

Alias Useful When...

  • column name is big and complex assign alias name.
  • use aggregate function assign alias name.
  • JOIN one or more table query structure make more readable assign alias name.
  • More than one columns are combined together to assign alias name.

Syntax

Look this syntax column_name AS alias_column_name and table_name alias_table_name no need AS keyword. Considering following syntax that help you to understanding Alias,

SELECT
    column_name [AS alias_column_name], aggregate_function(column_name) [AS alias_name], ...
    FROM table_name [ alias_table_name ];

Example Table

We have following employee_hour table that store weekday hours for each employee:

SQL> SELECT * FROM employee_hour;

NAME                 DAY             HOURS
-------------------- ---------- ----------
Opal Kole            Monday              8
Max Miller           Monday              8
Beccaa Moss          Monday              8
Paul Singh           Monday              9
Opal Kole            Tuesday             9
Max Miller           Tuesday             6
Beccaa Moss          Tuesday            10
Paul Singh           Tuesday             8
Opal Kole            Wednesday           7
Max Miller           Wednesday           9
Beccaa Moss          Wednesday          11
Paul Singh           Wednesday          12

12 rows selected.

Example

SQL> SELECT 
    name AS Employee_Name, SUM(hours) AS Total_Hours 
    FROM employee_hour emp_hours
    GROUP BY name;

EMPLOYEE_NAME        TOTAL_HOURS
-------------------- -----------
Opal Kole                     24
Beccaa Moss                   29
Paul Singh                    29
Max Miller                    23

Run it...   »