Page 163 - SQL
P. 163

SELECT
             Customers.PhoneNumber,
             Customers.Email,
             Customers.PreferredContact,
             Orders.Id AS OrderId
         FROM
             Customers
         LEFT JOIN
             Orders ON Orders.CustomerId = Customers.Id


        *AS OrderId means that the Id field of Orders table will be returned as a column named OrderId. See
        selecting with column alias for further information.


        To avoid using long table names, you can use table aliases. This mitigates the pain of writing long
        table names for each field that you select in the joins. If you are performing a self join (a join
        between two instances of the same table), then you must use table aliases to distinguish your
        tables. We can write a table alias like Customers c or Customers AS c. Here c works as an alias for
        Customers and we can select let's say Email like this: c.Email.


         SELECT
             c.PhoneNumber,
             c.Email,
             c.PreferredContact,
             o.Id AS OrderId
         FROM
             Customers c
         LEFT JOIN
             Orders o ON o.CustomerId = c.Id


        SELECT Using Column Aliases


        Column aliases are used mainly to shorten code and make column names more readable.


        Code becomes shorter as long table names and unnecessary identification of columns (e.g., there
        may be 2 IDs in the table, but only one is used in the statement) can be avoided. Along with table
        aliases this allows you to use longer descriptive names in your database structure while keeping
        queries upon that structure concise.

        Furthermore they are sometimes required, for instance in views, in order to name computed
        outputs.


        All versions of SQL



        Aliases can be created in all versions of SQL using double quotes (").


         SELECT
             FName AS "First Name",
             MName AS "Middle Name",
             LName AS "Last Name"
         FROM Employees






        https://riptutorial.com/                                                                             145
   158   159   160   161   162   163   164   165   166   167   168