SQL Alias
- An Alias is a shorthand for a table or column name.
- Aliases reduce the amount of typing required to enter a query.
- Complex queries with aliases are generally easier to read.
- Aliases are useful with JOINs and aggregates: SUM, COUNT, etc.
- An Alias only exists for the duration of the query.
Alias Column Syntax
SELECT column_name AS alias_name FROM table_name;
Alias Table Syntax
SELECT column_name(s) FROM table_name AS alias_name;
SQL Alias Examples
1) Example of Alias column
mysql> select name as alias_name from person;
+-----------------+
| alias_name |
+-----------------+
| subham ball |
| jit |
| sajal |
| sourav biswas |
| sajal |
| chayan das |
| shantanu biswas |
| sudip saha |
| debo |
| NULL |
| NULL |
| mono |
| monojit |
+-----------------+
13 rows in set (0.00 sec)
+-----------------+
| alias_name |
+-----------------+
| subham ball |
| jit |
| sajal |
| sourav biswas |
| sajal |
| chayan das |
| shantanu biswas |
| sudip saha |
| debo |
| NULL |
| NULL |
| mono |
| monojit |
+-----------------+
13 rows in set (0.00 sec)
2) Example of Alias Table
mysql> select name,age,sex from person as alias_name;
+-----------------+------+------+
| name | age | sex |
+-----------------+------+------+
| subham ball | 25 | M |
| jit | 18 | M |
| sajal | 21 | M |
| sourav biswas | 23 | M |
| sajal | 21 | M |
| chayan das | 24 | M |
| shantanu biswas | 26 | M |
| sudip saha | 26 | F |
| debo | NULL | M |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| mono | 20 | F |
| monojit | 22 | F |
+-----------------+------+------+
13 rows in set (0.00 sec)
+-----------------+------+------+
| name | age | sex |
+-----------------+------+------+
| subham ball | 25 | M |
| jit | 18 | M |
| sajal | 21 | M |
| sourav biswas | 23 | M |
| sajal | 21 | M |
| chayan das | 24 | M |
| shantanu biswas | 26 | M |
| sudip saha | 26 | F |
| debo | NULL | M |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
| mono | 20 | F |
| monojit | 22 | F |
+-----------------+------+------+
13 rows in set (0.00 sec)
Comments
Post a Comment