32

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

My current approach (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...

Is there a performance or maintainability reason to use one over the other?

3
  • 3
    I should elaborate on my motivations. I find searching for aliased columns in long & nested queries as the most annoying and possibly error prone part of maintaining queries. IMHO other possible consequences like distinguishing between an = in the select versus and = in the where clause and others are far less of a nuisance to me and I wondered what others opinions are?
    – mwjackson
    Oct 1, 2009 at 11:24
  • 3
    You have my vote... but it seems we're outnumbered <g> Oct 1, 2009 at 11:26
  • 1
    It would appear so. I suppose the important thing is that its readable and nicely idented, whatever people's preferences are...
    – mwjackson
    Oct 1, 2009 at 12:20

16 Answers 16

34

‘=’ 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.)

9
  • A valid point, but I would argue the improvement in readability in 95% of cases outweighs the 5% of cases where you would need to execute a query against a different DBMS
    – mwjackson
    Oct 1, 2009 at 12:23
  • 2
    The readability judgement is rather subjective! :-) How often you need a different DBMS is dependent on the type of project; if it's an in-house tool for an MS-oriented company relying on T-SQL extensions might be appropriate; for, say, an open-source project it wouldn't really fit.
    – bobince
    Oct 1, 2009 at 12:37
  • 1
    +1 This would be the only reason why I would consider changing my SQL writing style but I will postpone until the event occurs (wich may well be never). Oct 1, 2009 at 12:39
  • I suppose ANSI compliance is a good enough reason to use AS over "=", and with some of the formatting suggestions below, it can still be very readable. Considering I work on SqlServer 99% of the time however, its not enough for me to change my style :)
    – mwjackson
    Oct 7, 2009 at 13:20
  • 2
    Heh. Good luck parsing SQL with regex... ;-)
    – bobince
    May 9, 2018 at 11:23
17

To put in some counterweight, I prefer using =.

If I am the consumer of the query results in some way, I find it more convenient to see what columns I as a consumer can use.

I prefer this

SELECT
      [ElementObligationID] = @MaxElementObligationID + eo.ElementObligationID
      , [ElementID] = eo.ElementID
      , [IsotopeID] = eo.IsotopeID
      , [ObligationID] = eo.ObligationID
      , [ElementWeight] = eo.ElementWeight * -1
      , [FissileWeight] = eo.FissileWeight * -1
      , [Items] = eo.Items * -1
      , [Comment] = eo.Comment
      , [AdditionalComment] = eo.AdditionalComment
      , [Aanmaak_userid] = @UserID
      , [Aanmaak_tijdstip] = GetDate()
      , [Laatste_wijziging_userid] = @UserID
      , [Laatste_wijziging_tijdstip] = GetDate()
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

over this

SELECT
      @MaxElementObligationID + eo.ElementObligationID AS [ElementObligationID]
      , eo.ElementID AS [ElementID]
      , eo.IsotopeID AS [IsotopeID]
      , eo.ObligationID AS [ObligationID]
      , eo.ElementWeight * -1 AS [ElementWeight]
      , eo.FissileWeight * -1 AS [FissileWeight]
      , eo.Items * -1 AS [Items]
      , eo.Comment AS [Comment]
      , eo.AdditionalComment AS [AdditionalComment]
      , @UserID AS [Aanmaak_userid]
      , GetDate() AS [Aanmaak_tijdstip]
      , @UserID AS [Laatste_wijziging_userid]
      , GetDate() AS [Laatste_wijziging_tijdstip]
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

just my 2c.

1
  • 1
    +1: I worked on a couple of projects where the use of "alias = ..." and "... as alias" was mixed between different stored procedures -- and occasionally the same stored procedure. I find "alias = ..." much much easier to read and write. In particular, I find that right-aligning aliases using "... {tab} {tab} {tab} as alias" slightly more readable no alignment, but maintenance is a nightmare.
    – Juliet
    Oct 1, 2009 at 15:02
11

I wouldn't use it simply as it looks far too much like equality operation. 'AS' is clear inasmuch that it's not ambiguous to me.

Its the same as not using upper case in sql, I find it harder to read.

8

I'm not as lucky as others who have posted here. The code I work with is usually written by someone else and it is rare that there are not CASE statements or other calculations, concatenations or logic that cause a single entry to span over several rows of T_SQL script.

Using a equal sign instead of 'AS' is by far easier to read. With an equal sign you know the alias name you are looking for is in the first position of the row. When 'AS' is used and the T_SQL spans multiple lines, the alias name could literally be anywhere.

It is far, far, far easier to find the 'Items' alias when equals is used than when 'AS' is used.

    SELECT
        ElementObligationID = @MaxElementObligationID + eo.ElementObligationID
      , ElementID = eo.ElementID
      , IsotopeID = eo.IsotopeID
      , ObligationID = eo.ObligationID
      , ElementWeight = eo.ElementWeight * -1
      , FissileWeight = eo.FissileWeight * -1
      , Items = CASE WHEN eo.Items < 0 THEN eo.Items * -1
                     WHEN eo.Items > 0 THEN eo.Items
                     ELSE 0 END
      , Comment = eo.Comment
      , AdditionalComment = eo.AdditionalComment
      , Aanmaak_userid = @UserID
      , Aanmaak_tijdstip = GetDate()
      , Laatste_wijziging_userid = @UserID
      , Laatste_wijziging_tijdstip = GetDate()
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

Now imagine having more than 5 times the amount of code that is here and needing to find the 'Items' alias.

SELECT
      @MaxElementObligationID + eo.ElementObligationID AS ElementObligationID
      , eo.ElementID AS ElementID
      , eo.IsotopeID AS IsotopeID
      , eo.ObligationID AS ObligationID
      , eo.ElementWeight * -1 AS ElementWeight
      , eo.FissileWeight * -1 AS FissileWeight
      , CASE WHEN eo.Items < 0 THEN eo.Items * -1
             WHEN eo.Items > 0 THEN eo.Items
             ELSE 0 END AS Items
      , eo.Comment AS Comment
      , eo.AdditionalComment AS AdditionalComment
      , @UserID AS Aanmaak_userid
      , GetDate() AS Aanmaak_tijdstip
      , @UserID AS Laatste_wijziging_userid
      , GetDate() AS Laatste_wijziging_tijdstip
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

'AS' vs '=' is not a capricious and arbitrary preference. I am not exaggerating when I say there have been times when it would take several minutes to find the alias name I am looking for because the author of the script I am now in charge of maintaining did not use the equals sign with their alias. I cannot think of a bigger waste of time, money and resources than paying an IT professional to look for alias names in code!! There is a right and wrong answer if you care about maintainability, readability, and efficiency. Your job is to provide business value, not to spend your day looking for Waldo!

2
  • 1
    Except as many others have pointed out, if you DO care about maintainability and efficiency, then you should keep it in an ANSI-compliant syntax, NOT a specific DBMS syntax. Your maintainability score plummets to near 0 if they would ever need to move to a new DBMS. If your queries are so complicated already, imagine refactoring them all to be compliant!
    – D.R.
    Jan 16, 2019 at 20:23
  • 1
    Also, table aliases always use the AS form (with or without the optional AS), so AS is much more consistent in the overall query and SHOULD be familiar with the user
    – D.R.
    Jan 16, 2019 at 20:26
7

"=" is just plain ambiguous.

If you indent to break out each select clause...

select
    alias1     = somecolumn,
    alias2     = anothercolumn,
    result     = column1 * column2
from
    table
....


select
    somecolumn as          alias1,
    anothercolumn as       alias2,
    column1 * column2 as   result
from
    tables
     ...
2
  • 4
    While I disagree that = is ambiguous, this is a nice alternative...
    – mwjackson
    Oct 1, 2009 at 12:28
  • 2
    Good indentation really helps readability. But I always align the "AS" on the right just before the alias. It's more readable. Oct 1, 2009 at 18:36
5

= can be confused with assignment and equality; actually, the form I really don't like is when it looks like a string (usually when spaces are involved):

somecolumn as 'alias 1'

or

'alias 1' = somecolumn

I far prefer the alternative notation:

somecolumn as [alias 1]
3
  • There is a potential for confusion but only if you are doing nested selects inside your select clause, or not formatting your query nicely, both of which are far greater concerns in my opinion
    – mwjackson
    Oct 1, 2009 at 12:26
  • 1
    Another notation: ´[alias 1] = somecolum´
    – Jota Pardo
    Aug 14, 2017 at 22:01
  • 1
    Note that 'alias 1' = somecolumn is deprecated, while [alias 1] = somecolumn is not.
    – GSerg
    Jan 10, 2019 at 13:02
5

The postfix alias form (with or without the "AS") is consistent between column and table aliases. Personally, I'd like an option to enforce the use of "AS", and then you wouldn't have the situation:

select
    columnA,
    columnB
    columnC
from
    table

producing a result set with two columns instead of the expected 3.

I'd also say that with the prefix "=" form, it can make it more difficult to read if you're mixing obtaining a result set and variable assignment:

select
    cA = columnA,
    @cB = columnB,
    cC = columnC
from
    table
0
4

The three ways I know of to alias:

  1. TableColumn AS MyAlias
  2. TableColumn MyAlias
  3. MyAlias = TableColumn

Re: 1), I prefer this as it is the most self-documenting code (IMO), and it lets me search for AS if I need to find aliases..

Re: 2), This is my second choice, but without the AS, I am never sure whether this is a cut-and-paste error or not, especially in long, badly-formatted queries.

Re: 3), I don't like this because a) it looks like an assignment, and b) it blends in too much with ON clauses and CASE statements

So, my vote is to use the AS keyword for your aliases.

2
  • Is the = commutative in SQL with respect to aliases? I've never seen it used as you're describing instead of MyAlias = TableColumn. Oct 1, 2009 at 14:23
  • You're right, I transposed it, fixed now. Oct 1, 2009 at 14:24
2

I prefer using AS since = is used in the where statement, and can be confusing in a long query.

2
  • 1
    Only if you are doing nested selects inside your select clause, or not formatting your query nicely, both of which are far greater concerns in my opinion
    – mwjackson
    Oct 1, 2009 at 12:22
  • I formatted queries in different ways using '=' and 'AS'. After a long time working with SQL Server, I prefer to use the '='. In my case, because some columns require extensive logics and being able to locate the 'AS' can be complicated in some cases, while the '=' will always be after the ALIAS. As @mwjackson says, it all depends on how you format the code, and as far as my personal opinion is concerned. I have come to this question just because I want to determine the performance between one and the other, but readability remains subjective.
    – Jota Pardo
    Aug 14, 2017 at 21:58
2

I prefer using neither of those. I just give the name of the column without any keyword in between

SELECT MAX(price_column) maximumprice FROM prices
1
  • I do this as well, due to painful personal history behind use of "as" within queries. I always make sure those alias names stand out, through use of white space and (when there's more than one) column alignment. Oct 1, 2009 at 13:54
1

Column aliases declared by "=" syntax are deprecated in SQL Server 2008 and not supported in the next version. See MSDN article.

4
  • 8
    That only mentions the deprecation of 'string_alias' = expression, not column_alias = expression wich is still available Oct 1, 2009 at 11:58
  • This is a very important answer, covered by a pile of useless "i like it this or that way" answers...
    – Wouter
    May 2, 2019 at 14:14
  • Sidenote: [column_alias] = expression is also supported, yet doesn't appear in the documentation. Yet I guess this is not deprecated ...
    – Wouter
    May 2, 2019 at 14:23
  • Another sidenote: This piece of documentation seems to imply that "=" vs "as" is interchangeable, and doesn't come with any functional or performance differences.
    – Wouter
    May 2, 2019 at 14:25
1

While I have a preference for using AS, the really key thing here is to have a corporate standard and to follow it. If more of your people use AS than = then everyone should use it. Coding standards are what makes it easier to maintain code not the particular standard you pick. If everyone uses the same thing, then your eye gets used to picking it out.

1

I like the

SELECT
 column1 = table.column1
 ,column2 = table.colum2
FROM table

I find AS not as easily noticable compared to a = sign (I can spot = quicker than AS)

Also when one just does SELECT column alias, sometimes it's confusing to know which one is which :)

1

Since I write SQL for several different relational database management systems, I prefer to use a syntax which works on all of them, which normally means writing ANSI compatible SQL. My normal formatting preference is:

SELECT
    S.name AS SchemaName,
    O.name AS ObjectName,
    C.column_id AS ColumnId,
    C.name AS ColumnName
FROM
    sys.schemas AS S
    INNER JOIN sys.objects AS O ON S.schema_id = O.schema_id
    INNER JOIN sys.columns AS C ON O.object_id = C.object_id
ORDER BY
    S.name ASC,
    O.name ASC,
    C.column_id ASC;

As an alternative formatting of the above, the following makes it easier to see the column alias names:

SELECT
    S.name
        AS SchemaName,
    O.name
        AS ObjectName,
    C.column_id
        AS ColumnId,
    C.name
        AS ColumnName
FROM
    sys.schemas AS S
    INNER JOIN sys.objects AS O ON S.schema_id = O.schema_id
    INNER JOIN sys.columns AS C ON O.object_id = C.object_id
ORDER BY
    S.name ASC,
    O.name ASC,
    C.column_id ASC;
-1

You don't have to use either

Drop the AS and use

SELECT originalname alias
FROM
   tablename
-2

**even i prefer using 'as' instead of '=' . '=' makes confusion in code.

e.g :

 column as alias1
2
  • 7
    ...and this means a lot to mankind? ;) Oct 1, 2009 at 11:28
  • I prefer to use the '='. In my case, because some columns require extensive logics and being able to locate the 'AS' can be complicated in some cases, while the '=' will always be after the ALIAS. It all depends on how you format the code, and as far as my personal opinion is concerned. I have come to this question just because I want to determine the performance between one and the other, but readability remains subjective.
    – Jota Pardo
    Aug 14, 2017 at 22:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.