SQL - SPLessons

SQL Aliases

Chapter 29

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

SQL Aliases

SQL Aliases

shape Description

SQL Aliases are used to give a temporary and customize and more readable name of a database column while retrieving value with a select query. Alias can also be given to a table. It will help to shorten the table name while performing join on a table.

shape Syntax

The Alias syntax for a column is as follows:

Select column_name AS alias_name from table_name;

Column name => The operations that can be performed on a column in the table.

shape Examples

The below example describes the alias commands with the help of join in sql database.

sql> select * from employee;
+--------+--------+---------+
| emp_id | ename  | dept_no |
+--------+--------+---------+
|   1001 | jack   |      10 |
|   1002 | maddi  |      20 |
|   1003 | maddie |      10 |
|   1004 | max    |      20 |
|   1004 | capi   |      30 |
+--------+--------+---------+
5 rows in set (0.00 sec)

sql> select * from department;
+---------+--------------+----------+
| dept_no | dept_name    | city     |
+---------+--------------+----------+
|      10 | finance      | texas    |
|      20 | capital      | new york |
|      30 | manager      | ausralia |
|      40 | applications | usa      |
|      50 | technology   | canada   |
+---------+--------------+----------+
5 rows in set (0.00 sec)

sql> select employee.emp_id,employee.ename,department.dept_name,department.city from department right join employee on department.dept_no=employee.dept_no;
+--------+--------+-----------+----------+
| emp_id | ename  | dept_name |  city    |
+--------+--------+-----------+----------+
| 1001   | jack   | finance   | texas    |
| 1003   | maddie | finance   | texas    |
| 1002   | maddi  | capital   | new york |
| 1004   | max    | capital   | new york |
| 1004   | capi   | manager   | ausralia |
+--------+--------+-----------+----------+
5 rows in set (0.00 sec)

Here in the above example the table alias is used to in the query and performed right join on both employee and department table.

Summary

shape Key Points

  • SQL Aliases – Used to give a temporary and customize and more readable name of a database column.