19

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.

froadie
  • 79,995
  • 75
  • 166
  • 235

8 Answers8

25

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.

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
  • It's definitely more readable if your database supports it. Good syntactic sugar :) – Daniel Bingham Mar 16 '10 at 14:13
  • Thanks for the answer. Just wanted to point out, though - The second paragraph is more of a reason why to alias than a reason why or why not to use "AS". – froadie Mar 16 '10 at 14:22
  • @froadie: Yes, I guess I got carried away with "aliasing fever"! I think you're right, the first paragraph answers the question most directly. – FrustratedWithFormsDesigner Mar 16 '10 at 14:30
  • +1 from me exactly for making both points, why you use the 'as' and why you use the alias in the first point. Both points need to be remembered together! – lexu Mar 16 '10 at 16:16
  • Just wanted to comment that I think MS Access requires you to use "As". I'm sure your questions wasn't about MS Access. Just saying. – HK1 Oct 24 '11 at 21:59
  • This is an old thread, but I'd like to add that if you us 'AS' it makes it easier to do a find and immediately locate the place where the alias was defined. Yes, it's possible to make use of the spaces to do this too. – Mint Mar 21 '19 at 22:38
7

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).

Rob Farley
  • 15,625
  • 5
  • 44
  • 58
  • I should mention I'm actually quite lazy about using 'as', for example this query I just wrote over at http://ask.sqlservercentral.com/questions/4908/permutations-in-t-sql – Rob Farley Mar 16 '10 at 14:21
3

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
Rhapsody
  • 6,017
  • 2
  • 31
  • 49
3

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

DVK
  • 126,886
  • 32
  • 213
  • 327
  • You meant "aliasing with "as" is actually " – Ashish Gupta Mar 16 '10 at 14:14
  • 2
    @Ashish Gupta - Don't think so, sounds like he/she meant using "a" as an alias isn't very understandable. I agree, but if you alias it as something shorter *and* still recognizable, I think there's a place for it. I've often seen aliases when table names are annoyingly long. – froadie Mar 16 '10 at 14:16
  • 1
    @froadie - it's a balance. But as I said, if it is a serious 2-page-long query with 7 tables 4 of which start with "a" and a somewhat similarly named to boot, aliasing is a net loss for readability. – DVK Mar 16 '10 at 14:20
1

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
Prutswonder
  • 9,894
  • 3
  • 27
  • 39
0

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
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • [SQL Server doesn't require `AS` when using derived queries.](http://www.sqlfiddle.com/#!6/d41d8/17859) –  May 23 '14 at 19:11
0

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

Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
0

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

Jimmy
  • 16,123
  • 39
  • 133
  • 213