SQL Alias Syntax

By | October 2, 2021
SQL Alias Syntax

The SQL alias is used to change the table or column name temporary, during a query. To use the Alias in SQL we use the AS clause followed by the temporary name. The main use of Aliases clauses to make a column and table name more readable.

SQL Alias Syntax

The life of the Alias temporary name change remains until the query execution. The name changes by the alias statement do not affect the real database or the real column name.

Vamware

Alias Column Syntax

SELECT column_name AS temp_name
FROM table_name;

Alias Table Syntax

SELECT column_name(s)
FROM table_name AS alias_name;

Example

For the following example consider these two tables:

Students

+------+--------+------+--------+-------+----------+
| id   | name   | age  | grades | marks | Trade    |
+------+--------+------+--------+-------+----------+
|    1 | Luffy  |   16 | A      |   970 | Science  |
|    2 | Naruto |   18 | A      |   960 | Humanity |
|    3 | Zoro   |   20 | A      |   940 | Commerce |
|    4 | Sanji  |   21 | B      |   899 | Humanity |
|    5 | Nami   |   17 | B      |   896 | Science  |
|    6 | Robin  | NULL | B      |   860 | Humanity |
|    7 | Goku   |   27 | B      |   860 | Humanity |
+------+--------+------+--------+-------+----------+

Library

+---------+-------------------+------------+
| Book_ID | Book_Name         | Student_ID |
+---------+-------------------+------------+
|    1124 | One Hundred years |          1 |
|    1104 | The Great Escape  |          2 |
|    1209 | Beloved           |          6 |
|    1111 | Hollow            |          4 |
|    2351 | Invisible Man     |          3 |
|    1034 | A Passage         |          2 |
|    1211 | Hero              |          6 |
|    1188 | Your Name         |          5 |
|    1211 | Hero              |          6 |
|    1000 | My Hope           |          8 |
|    1000 | Go Away           |         10 |
+---------+-------------------+------------+

Query: From table students show id as Student IDs and name as NAMES.

SELECT id AS StudentID , name as NAMES
FROM students;

Output

+-----------+--------+
| StudentID | NAMES  |
+-----------+--------+
|         1 | Luffy  |
|         2 | Naruto |
|         3 | Zoro   |
|         4 | Sanji  |
|         5 | Nami   |
|         6 | Robin  |
|         7 | Goku   |
+-----------+--------+

Query(Using AS with Table): Show student IDs, name from students table and the book they borrowed from the library.

SELECT S.ID, S.name, L.Book_Name
FROM students AS S, Library AS L
WHERE S.ID =L.Student_ID;

Output

+------+--------+-------------------+
| ID   | name   | Book_Name         |
+------+--------+-------------------+
|    1 | Luffy  | One Hundred years |
|    2 | Naruto | The Great Escape  |
|    6 | Robin  | Beloved           |
|    4 | Sanji  | Hollow            |
|    3 | Zoro   | Invisible Man     |
|    2 | Naruto | A Passage         |
|    6 | Robin  | Hero              |
|    5 | Nami   | Your Name         |
|    6 | Robin  | Hero              |
+------+--------+-------------------+

Summary

  • To use Alias in SQL we use the AS clause.
  • AS clause provide a temporary name to the Column and table.
  •  The life of the temporary name limit to the query execution.
  • Alias is more useful when there is more than one table involved in the query.

People are also reading:

Leave a Reply

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