I just came across a SQL statement that uses AS to alias tables, like this:
SELECT all, my, stuff
FROM someTableName AS a
INNER JOIN someOtherTableName AS b
ON a.id = b.id
What I'm used to seeing is:
SELECT all, my, stuff
FROM someTableName a
INNER JOIN someOtherTableName b
ON a.id = b.id
I'm assuming there's no difference and it's just syntactic sugar, but which of these is more prevalent/wide-spread? Is there any reason to prefer one over the other?
Edited to clarify:
I appreciate all the answers and all the points made, but the question was not why or why not to use table aliases. The question was purely about using the "AS" keyword for table aliasing or leaving it out.
It's syntactic sugar and it takes a little bit longer to type but some people find it more readable and clear. The reason I use it is that when reading a large query, it is easier to pick out the aliases by looking for the
AS
's.Another reason, sometimes the full table name is long and cumbersome to type. Aliasing to something shorter can sometimes make things easier for when you don't have fancy features like autocomplete - or for when you're just feeling lazy. ;)
...And as some others have pointed out before me, it can be useful when doing self-joins.
First of all, aliasing with "a" is actually often considered a Bad Thing (we officially prohibit it in our coding standard). The reason is that in a long complicated multi-table query people lose track of which alias stands for which table.
Saving 2 seconds on typing - especially in this era of intellisenseish IDEs - is kind of idiotic when weighed against readability/maintainability.
The main legit use of aliasing is to do self-joins