Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL - Aliasing using "=" versus "as" [closed]

Is there any particular reason (performance or otherwise) to use AS ahead of = when aliasing a column?

My personal preference (for readability) is to use this:

select alias1     = somecolumn alias2     = anothercolumn from tables etc... 

instead of this:

select somecolumn as alias1 anothercolumn as alias2 from tables etc... 

Am I missing out on any reason why I shouldn't be doing this? What are other people's preferences when it comes to formatting their columns?

like image 354
mwjackson Avatar asked Oct 01 '09 11:10

mwjackson


People also ask

Can we use alias without AS?

Yes, you can alias without AS.

How does aliasing work in SQL?

SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.

How should an alias be defined if it has a space in it SQL?

If the alias_name contains spaces, you must enclose the alias_name in quotes. It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name.

What is the proper syntax for aliasing a field in SQL?

The basic syntax of a table alias is as follows. SELECT column1, column2.... FROM table_name AS alias_name WHERE [condition];


1 Answers

‘=’ isn't valid ANSI SQL, so you'll have difficulty should you wish to run your application on a different DBMS.

(It's when ANSI form is used but the optional ‘AS’ is omitted I find the results difficult to read, personally.)

like image 101
bobince Avatar answered Sep 23 '22 23:09

bobince