5 votes

In SQL, does not using AS is a bad practice? Why? Is it documented somewhere?

I read (if I remember correctly in a post or a comment to a question on Stackoverflow in English) in SQL does not use AS for aliases was a bad practice.

I usually use my query without a AS when you are alias, because it seems to me more elegant, for example:

SELECT COUNT(*) total FROM tabla t1 INNER JOIN tabla2 t2 ON t1.id=t2.id;
SELECT MAX(id)  mayor FROM tabla ... 

Instead of:

SELECT COUNT(*) AS total FROM tabla AS t1 INNER JOIN tabla2 AS t2 ON t1.id=t2.id;
SELECT MAX(id)  AS mayor FROM tabla ... 

I was looking for example in the MySQL Reference Manual and found nothing that says explicitly not to use AS it's a bad practice.

In summary, I want to know if you don't use AS is a bad practice. If it is, why it is and where it is documented?

The question refers to SQL in general, not to specific handlers.

6voto

zerocool Points 821

The standard ISO/IEC or 9075 in the specification of the query indicates that:


7.12 < query specification >

This Subclause is modified by Subclause 7.1,"", in ISO/IEC 9075-4.

Function

Specify a table derived from the result of to .

Format

< query specification > ::=
  SELECT [ < set quantifier > ] < select list >< table expression >

< select list > ::= < asterisk > | < select sublist > [ { < comma >< select sublist > }... ]

< select sublist > ::=< derived column > | < qualified asterisk >

< qualified asterisk > ::= < asterisked identifier chain >< period >< asterisk > | < all fields reference >

< asterisked identifier chain > ::= < asterisked identifier> [ { < period >< asterisked identifier > }... ]

< asterisked identifier > ::=< identifier >

< derived column > ::= < value expression > [ < as clause > ] 

< as clause > ::= [ AS ] < column name >

< all fields reference > ::= < value expression primary >< period >< asterisk > [ AS < left paren >< all fields column name list >< right paren > ]

< all fields column name list > ::= < column name list >

We are interested in:

 < as clause > ::= [ AS ] < column name >

If one interprets the documentation as usual, the square brackets indicate that it is optional what that remark by which the word AS to indicate the alias of the column is optional under the specification.

I know that in the question we talk about tables but I don't think they differ in this concept.

I don't think that's a bad practice to leave it without the AS, I would bet that it has been by issues of compatibility.

I leave the link to the draft of the standard that I found on the web (Page 377): http://jtc1sc32.org/doc/N1801-1850/32N1822T-text_for_ballot-CD_9075-2.pdf

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X