views:

55

answers:

6

Simple question: Office debate about whether the keyword AS is necessary in our T-SQL statements. I've always used it in cases such as

SELECT mycol AS Something
FROM MYTABLE;

I know you don't NEED it but is there any benefit of using it (or not using it)? I think it makes statements easier to read but people seem to disagree with me.

Thanks!

+1  A: 

I agree with you that including the AS keyword makes queries easier to read. It's optional, but not including it is lazy. It doesn't make a significant difference to the performance of the query - the query plan will be the same. I would always prefer to include it for clarity.

Mark Byers
+2  A: 

Generally yes, as it makes it easier to see what is aliased to what.

thecoop
A: 

I don't think it makes much difference. It certainly makes no difference to the performance. I think the important thing is to be consistent.

Similarly with table aliases:

SELECT mycol AS Something
FROM MYTABLE AS m;

Personally, I prefer to omit the AS, because it is faster to write and fewer characters to read.

Paul
A: 

I have almost always aliased my table names, and sometimes aliased my column names.

For production queries, I suggest that you go with uniformity - if you do it, do it at all times, and use the same convention. If you do not, then just leave things as they are.

Raj More
+1  A: 

I think it depends upon how readable your schema is to start with. If the field names are cryptic, then yes, using an alias can make it easier to understand the output of the SQL statement. However, there can be a cost associated with this when debugging. In a complex schema it can be difficult to track down the source of a column unless you look at the SQL statement itself to understand what field the alias is referring to.

Tim Lentine
A: 

I think you should always keep using AS when aliasing columns as it is obligatory for some other DBRM engines such as Oracle, for instance. So, if you are used to use this syntax, you won't get bothered for something as simple as that.

Will Marcouiller