tags:

views:

385

answers:

9

I have seen many different syntax styles for aliases in SQL. Are there any advantages/disadvantages to any of these syntax styles? What is your preference?

Alias only:

SELECT patient.nm_first   patient_firstname, 
       patient.nm_last    patient_lastname,
       physician.nm_first physician_firstname,
       physician.nm_last  physician_lastname

Use AS:

SELECT patient.nm_first   AS patient_firstname, 
       patient.nm_last    AS patient_lastname,
       physician.nm_first AS physician_firstname,
       physician.nm_last  AS physician_lastname

Using equal:

SELECT patient_firstname   = patient.nm_first,   
       patient_lastname    = patient.nm_last, 
       physician_firstname = physician.nm_first,
       physician_lastname  = physician.nm_last
+25  A: 

Mine is to use the

column AS alias

convention. It's less ambiguous than the other methods. Using the "=" can easily cause confusion with other expressions when scanning the code. With no keyword at all, it's unclear if you're looking at multiple columns or a column and an alias.

Of the three, using "AS" is the most clear when reading the code.

Jay Stevens
@Warpraptor: "using "AS" is the most clear when reading the code" is quite subjective. I personally prefer "=".
Sung Meister
I think, at best, its a "little" subjective (especially given the majority of answers below). The primary issue is that using "=" or nothing at all is open to be interpreted as something other than an alias. "AS" isn't open to the same interpretations.
Jay Stevens
@Sung Meister: I instinctively read = as variable assignment (programming does that to you). In this case, it's not. I reserve the "=" only for @variables. Ergo, AS is easier to read, for me. +1 to Warpraptor.
Eric
@Warpraptor: I understand that some people prefer reading SQL like Fluent English. Even with a programming background, I find "=" to be easier for me to read since I can tell part which (vertical) code of TSQL is for alias and what part is for domain name. I see it that way.
Sung Meister
+1  A: 

I just alias with the space. The less keystrokes I have to make during my day, the better.

On occasion (working in Oracle usually), I will use the AS statement just because some lengthy procedures lend themselves to readability that way.

Jonathan
being able to read (and decipher) the SQL is A MUCH MORE IMPORTANT consideration than saving three keystrokes for an alias. (Don't use an alias if it's not needed, I'm fine with saving keystrokes...
spencer7593
*shrug* Again, it goes back to individual preference and readability. I'm fine with reading aliases without an AS clause primarily due to my tabbing/line entry methods in my specific SQL editor.
Jonathan
I agree with Jonathan. If I'm aliassing columns, I'll have one column per row, then space the aliases such that they *appear* as a column in the script -- much as his first example above. Throw in some extra spaces (at least 2), and it's pretty clear what's going on. And at that point, adding an "AS" just seems like repetitive and non-essential data to me, so I "normalize it out of my text.
Philip Kelley
+2  A: 

I like using "AS", simply for better SQL readability.

Ryan Oberoi
+1  A: 

The most important thing is to be consistent. Talk it over with your co-workers and see if your company uses one more frequently than the others. I can't think of any strong arguments for one over any other.

StriplingWarrior
+3  A: 

I believe that AS is the clearest to read. There is no implication of computed columns with AS, whereas '=' does imply a computed column and simple whitespace can be confusing (it appears there are more columns if you don't notice the missing ',').

Other than that, it is preference and convention.

Godeke
+1  A: 

I like the "AS", it separates and distinguishes the new column name best to my eye.

At first glance, the "=" method looks like an assignment or condition, and the space style looks like a syntax error to me.

KM
how can you down vote a complete opinion???
KM
+3  A: 

AS keyword is a SQL-92 standard while = is not.

Use AS if you need to be standards compliant.

My preference is to use =, personally, for quick selects. For work, I am for AS

Sung Meister
A: 

I use just space as it requires less typing. As is also good because it is clear. = is not standard and might be confusing so I would not prefer it.

And please do not use capitalized keywords in sql, nowadays editors can color keywords.

Calmar
You'd fail our coding standards then: SQL keywords all uppercase. It's no different to inane (IMHO) case sensitivity in c#: Frog frog = new Frog(). WTF?
gbn
Not all RDBMS are Case Sensitive outside of explicit strings.I find it really quick to type out a sproc in all lowercase then press Compile and watch Oracle convert everything to uppercase for me. It's again, a matter of preference.
Jonathan
A: 

Seems I'm the odd man out here, but I strongly prefer alias = expression.

Aliases tend to be shorter than expressions, and of more consistent length, so in any complex SELECT clause it keeps things more readable. eg.

select
  groupby1
, groupby2
, ratio1   = convert(float,sum(case <big long thing here> end))
           / nullif(sum(case <big long thing here> end),0)
, total2   = <some other long expression>
from table1 etc....

In this format, you can visually parse the SELECT list in one pass. If you need more detail you can quickly find and focus on a particular expression, as it maps to a column in the result set.

Peter