4 Replies Latest reply: Oct 9, 2013 4:22 AM by Billy~Verreynne RSS

    Aliasing

    8e2e82ee-c8d1-495d-83a3-391e1c86327d

      Just wondering ... I've seen aliasing written in four different ways.

       

      EX:

       

      1) ps_case.column1

      2) c.column1

      3) column1 c

      4) column1 AS c

       

      Why so many variations and are these all equally usable or are there pros and cons to each?

        • 1. Re: Aliasing
          Karthick_Arp

          There is no alias in 1 and 2 they are the original column name. Only 3 and 4 has alias in your example. The AS keyword is optional.

          • 2. Re: Aliasing
            Gurjeet

            Alias we can differentiate as below:::

            1. alias used with column_name in (select clause) written as "alias.column_name"

            2. alias used to shorten the lengthy table names/column_names written as " table_name/column_name alias"

            3. AS is optional

            • 3. Re: Aliasing
              BluShadow

              8e2e82ee-c8d1-495d-83a3-391e1c86327d wrote:

               

              Just wondering ... I've seen aliasing written in four different ways.

               

              EX:

               

              1) ps_case.column1

              2) c.column1

              3) column1 c

              4) column1 AS c

               

              Why so many variations and are these all equally usable or are there pros and cons to each?

               

              1) this is either a table name or table alias prefixing the column name.  It is not an alias of the column name.

              2) this is the same as 1)

              3) this is an alias of the column name

              4) this is the same as 3 but including the optional "AS" keyword.

               

              So there are only 2 types of alias... table aliases and column aliases.

               

              A table alias can be defined after the table (or subquery) name in the query, and is then used as a prefix to the columns selected where there could possibly be ambiguity as to which column is required (where more than one table has the same named column).  When defining a table alias it cannot use the "AS" keyword, that is only for column aliases.

               

              Column aliases provide an alternative name for the column, and are defined in the SQL projection after each column as required.  They may include the optional "AS" keyword.  This is useful when you are querying more than one column of the same name and need to differentiate between them.

              • 4. Re: Aliasing
                Billy~Verreynne

                1 and 2 are examples of explicit scope. Instead of only providing a column reference, a reference to the object (via the object's actual name, or an alias for the object) is given.

                 

                Explicit scope in the SQL language is often a good thing - but can also be a problem when abstraction is done at SQL level. So treat it as a two edge sword and make a concious decision as to when it should be used, and when not.

                 

                3 and 4 are examples of naming a SQL projection column.