Aliases

An alias is just an alternate name for a field or value that improves the readability of the queries. They are assigned with the AS keyword and can be single words or complete strings.

Consider the following example to understand the usefulness of aliases. Let’s say that we want to return the current date. We can do it with the following command:

mysql> SELECT CURDATE();
 +------------+
 | CURDATE() |
 +------------+
 | 2016-03-10 |
 +------------+
 1 row in set (0.00 sec)

Notice how the column returned was named CURDATE(). We can define more user friendly name for the column using aliases. Here is how we would do that:

mysql> SELECT CURDATE() AS CurrentDate;
 +-------------+
 | CurrentDate |
 +-------------+
 | 2016-03-10 |
 +-------------+
 1 row in set (0.00 sec)

The column now has a more descriptive name – CurrentDate.

Geek University 2021