SQL Column Alias

Defining a Column Alias

A column alias:

• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (There can also be the optional AS keyword between the            column name and alias.)                                          
• Requires double quotation marks if it contains spaces or special characters, or if it is case-sensitive

When displaying the result of a query, SQL Developer normally uses the name of the selected
column as the column heading. This heading may not be descriptive and, therefore, may be difficult
to understand. You can change a column heading by using a column alias.

Specify the alias after the column in the SELECT list using blank space as a separator. By default,
alias headings appear in uppercase. If the alias contains spaces or special characters (such as # or $),
or if it is case-sensitive, enclose the alias in double quotation marks (" ").


Using Column Aliases

                                        
                       SELECT last_name AS name, commission_pct comm FROM employees;



The first example displays the names and the commission percentages of all the employees. Note that
the optional AS keyword has been used before the column alias name. The result of the query is the
same whether the AS keyword is used or not. Also, note that the SQL statement has the column
aliases, name and comm, in lowercase, whereas the result of the query displays the column headings
in uppercase. As mentioned earlier column headings appear in uppercase by default.

                     SELECT last_name "Name" , salary*12 "Annual Salary" FROM employees;



The second example displays the last names and annual salaries of all the employees. Because
Annual Salary contains a space, it has been enclosed in double quotation marks. Note that the
column heading in the output is exactly the same as the column alias.



No comments:

Post a Comment