Aliasing with AS
Column names are built for typing, not for reading: dob, sal, percent. An alias gives a column a friendlier heading in the resultset, and leaves the table exactly as it is.
1Renaming a column
The headings read as English. Nothing in the table changed — desc students would still show name and percent. An alias lives for the length of one query.
percent + 5 is not a column, it is a calculation, so it has no name of its own. Without an alias MySQL heads the column with the expression itself — a heading reading percent + 5. Any time you calculate something, name it.Look at Bhim's row while you are here. His percentage is NULL, and NULL + 5 is NULL, exactly as the concepts chapter promised. Aliasing a calculation does not change that — it only gives the column a heading.
2When the alias needs quotes
When the alias contains a space or a symbol.
When it is a single plain word.
3AS itself is optional
Leave the word out and put the alias straight after the column. MySQL accepts it, and you will meet it in exam papers written this way:
select name Student and select name, Student differ by one comma and mean completely different things — the second asks for a column called Student that does not exist. Writing as makes the intention unmistakable to a reader, and to you.4Recap
Renames the column in the resultset only.
An alias lasts for one query. desc shows the real names.
percent + 5 has no name until you give it one.
select name Student works, but as is clearer.
After select name as 'Student Name' from students; what is the column called in the table?
Why does select percent + 5 from students; usually want an alias?
Which alias must be written in quotes?