Why would you use “AS” when aliasing a SQL table?

蹲街弑〆低调 提交于 2019-11-27 23:33:59

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.

It's generally preferred. Consider what happens if you're using old 'comma notation' for joins, and you miss a comma.

Instead of:

select *
from Orders, Customers;

You end up with:

select *
from Orders Customers; --Customers is now the alias for Orders.

And whilst this isn't fixed by introducing 'as', you can more easily tell if it's intended (since I may have actually wanted to alias Orders as Customers, depending on what else I was doing to it during my query).

Not all databases support the AS statement as far as I know. (Oracle?) But for some reason it looks more readable.

Edit: Oracle doesn't support the 'AS' keyword over here;

ORA-00933: SQL command not properly ended

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

Field aliases are for readability of the output. Table aliases are for readability of the query structure. Especially when your're dealing with long table names and possibly even cross-database references.

If you have duplicate table references in your query, you should always use table aliases to distinguish one table from the other. For instance, a parent-child join could look somewhat like this:

SELECT parent.Name AS ParentName,
child.Name AS ChildName
FROM MyTable AS parent
INNER JOIN MyTable as child
ON parent.ID = child.ParentID

Probably by using AS you can quickly see which are the tables getting used as Alias.

If you have a huge SQL statement with various joins, aliases make it easier to read/understand where the columns are coming from

One of our applications can't handle hyphens in column names (don't ask me why), so aliases are a perfect method for converting COLUMN-NAME to COLUMN_NAME

As far as when AS should be explicit specified, it depends on the syntax supported by the particular engine and personal preference (or even policy).

In SQL Server (which is all I deal with), AS is optional in the case of after a relation name in a FROM or JOIN. In such cases I skip the keyword due to my preference and that I find that it does not "decrease readability" when used with a consistent line-oriented join form.

However, when using derived queries, SQL Server requires the AS keyword and so I diligently include it in such cases.

Once again, in output clauses due to preference, I include AS. I believe my choice of the keyword here is due to the fact that, unlike with my join formatting, it is often the case that multiple identifiers appear on the same line.

So, for me in SQL Server:

SELECT a.all AS my, a.stuff  -- AS    (preference)
FROM someTableName a         -- no AS (preference)
INNER JOIN (
    SELECT ..
    ) AS b                   -- AS (required)
  ON a.id = b.id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!