MySQL As Keyword

In this tutorial, we will look at how to use the MySQL AS keyword. The MySQL AS keyword is used to specify an alternate name (or alias) to use when referring to either a table or a column (field) in a table. Using an alias may be important for readability when reading from the database or constructing SQL queries to be performed on the database.

Using the MySQL AS keyword to provide an alias may also be useful when a table name or column name has changed and you want to make the database compatible with existing program code. Whatever your need is, you will see the usefulness of the MySQL AS keyword below.

Using the MySQL AS Keyword to alias column names

Suppose we had the following table called "employees" in our database:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo
Bill May

We could actually alter the column names of our table in our query result by using the AS keyword. If we executed the following select query:

SELECT first_name AS given_name, last_name AS surname FROM employees;

We would get the following result:

given_name surname
Paul Pitterson
Francine Beecham
Raul DiNozzo
Bill May

As you can see, the column names in the result set have different names than those in the database. So if we were writing code to process the result of our SQL query, we would have to refer to the result using the aliased column names. Our program would have no way of knowing what the original column names were.

This is how the compatibility that we mentioned above comes in. If our database schema (in this case column names) changed, if we wanted to, we could only alter our existing queries and not necessarily the code that performs calculations on our result. So we would basically alias the new names to the old names and our code would not need to change.

Using the MySQL AS Keyword to alias table names

Suppose we had the same "employees" table in our database. If we ran the following query:

SELECT employees.first_name, employees.last_name FROM employees;

We would get:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo
Bill May

If we wanted to, we could shorten our query by aliasing the name of our table like so:

SELECT e.first_name, e.last_name FROM employees AS e;

And our result would be the same:

first_name last_name
Paul Pitterson
Francine Beecham
Raul DiNozzo
Bill May

Omitting the MySQL AS Keyword

The last thing to note is that the MySQL AS keyword is so special that we can actually omit it if the readability our our query is not a concern.

For example, the following queries would both do the same thing:

SELECT first_name AS given_name, last_name AS surname FROM employees;
SELECT first_name given_name, last_name surname FROM employees;

And these as well:

SELECT e.first_name, e.last_name FROM employees AS e;
SELECT e.first_name, e.last_name FROM employees e;

That is pretty much all you need to know about the MySQL AS keyword. We hope you learnt a lot in this MySQL tutorial.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...