Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use column aliases with or without the keyword AS?

Which is best?

Select TestColumn TC

or

Select TestColumn AS TC

I prefer the first one but I am not sure if this is a modern way of declaring the alias.

like image 929
connersz Avatar asked Jan 25 '12 16:01

connersz


2 Answers

The two are equivalent for most purposes. I prefer the explicit AS notation.

One reason why I use the AS form is because, in the DBMS I use mainly, it provides a measure of protection against new keywords appearing on system upgrades. That is:

SELECT TestColumn KeyWord

where 'KeyWord' becomes a keyword in a future release can cause problems, whereas in the particular system, using:

SELECT TestColumn AS KeyWord

remains valid.

I recently learned (was told) that Oracle does not support AS in table aliases.

FROM TableName T1     -- OK in Oracle
FROM TableName AS T1  -- Not OK in Oracle

If that is accurate (I've not validated it), then I regard that AS a bug.

It's your choice, though; the two notations are equivalent.

like image 61
Jonathan Leffler Avatar answered Sep 19 '22 18:09

Jonathan Leffler


I prefer the second one (using "AS"). If it works either way, just use what you prefer and use it consistently.

like image 22
Bernard Avatar answered Sep 17 '22 18:09

Bernard