Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would you use "AS" when aliasing a SQL table?

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.

like image 798
froadie Avatar asked Mar 16 '10 14:03

froadie


People also ask

Do you need to use AS in SQL for alias?

Should You Use the SQL AS Keyword with Table Aliases? The AS keyword is optional when using it with a table alias.

Do you need to use AS for alias?

According to ANSI/ISO SQL the AS keyword is optional. But some dbms products want it, while others don't want it... There's also the alias=columnValue syntax; i.e. select x=1, 2 as y, 3 z used in some DBMS systems.

Why do we use AS in SQL?

SQL AS keyword is used to give an alias to table or column names in the queries. In this way, we can increase the readability and understandability of the query and column headings in the result set.

Which is the AS clause used for?

The AS clause allows you to give an alias name to EQL attributes and results. The alias name can be given to an attribute, attribute list, expression result, or query result set. The aliased name is temporary, as it does not persist across different EQL queries.


1 Answers

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.

like image 95
FrustratedWithFormsDesigner Avatar answered Oct 11 '22 22:10

FrustratedWithFormsDesigner