Advertisement
Google Ad Slot: content-top
MySQL Aliases
MySQL Aliases
In MySQL, an alias is a temporary name for a table or column. It is often used to make the output more readable or to simplify queries.
- Aliases are created using the
ASkeyword (thoughASis optional in MySQL).
- They only exist for the duration of the query and do not affect the actual table or column names.
Alias Column Syntax
SELECT column_name AS alias_name FROM table_name;
You can also write it without AS:
SELECT column_name alias_name FROM table_name;
Demo Database
Below is a selection from the "Students" table in the school_db database:
student_id |
name |
gender |
city |
age |
score |
course_id |
phone_number |
|
|---|---|---|---|---|---|---|---|---|
1 |
Alice |
Female |
Delhi |
20 |
85 |
101 |
NULL |
1234567890 |
2 |
Bob |
Male |
Mumbai |
22 |
75 |
NULL |
bob@email.com |
NULL |
3 |
Charlie |
Male |
Delhi |
21 |
95 |
102 |
NULL |
NULL |
4 |
David |
Male |
Bangalore |
23 |
65 |
101 |
david@email.com |
NULL |
5 |
Eve |
Female |
Mumbai |
20 |
80 |
103 |
NULL |
NULL |
6 |
Frank |
Male |
Delhi |
22 |
90 |
103 |
NULL |
NULL |
7 |
Alice |
Female |
Mumbai |
19 |
60 |
102 |
NULL |
NULL |
Alias for Columns Examples
The following SQL statement creates two aliases, one for the student_id column and one for the name column:
Note
Single quote marks are required if the alias name contains spaces:
Alias for Tables Example
We use the "Students" and "Course" tables, and give them the table aliases of "s" and "c" respectively (Here we use aliases to make the SQL shorter):
The following SQL statement is the same as above, but without aliases: