AS Keyword Denotes an ALIAS

The word "AS" found in a statement is used to denote an "alias". An alias is used to shorten the name of a table or a column. This can be helpful when the names of tables or columns are long or complex, and it can make it easier to make calls to multiple tables in the same statement.

Here is an example from a table called "changed_pages" that is tracking pages which have been moved to a new url. We want to find out the:

SELECT cp.cp_id, cp.old_url, cp.new_url, cp.date_time
FROM changed_pages AS cp
WHERE cp.old_url = "http://www.example.com/example.php";

Note the dots in the selected table columns. Their use, coupling the table with the column, clarifies any ambiguity about which table contains the column (especially if column names within the tables happen to be the same). This practice would be vital in a statement that contained more than one table.

It should be noted that an alias established in the SELECT clause (for a column for example) will not work in the WHERE clause. This is because the FROM clause is evaluated first, the WHERE clause next and only then the SELECT clause.

Note that in PHP mySQL when the resulting array is accessed, the alias is included in the key. For example:

$resulting_r = mysql_fetch_assoc($query);
$new_url = $resulting_r[cp.new_url];

< SQL Table Structure and Syntax | SQL Alter Table Statement >


Send notes in disappearing ink!

Interesting Pages