Saturday, 29 March 2014

SQL: ALIASES

Learn how to use SQL ALIASES (temporary names for columns or tables) .

Description

ALIASES are used to create a temporary name for columns or tables.
  • COLUMN ALIASES used to make column headings for result se, so it become easy to read.
  • TABLE ALIASES used to shorten SQL Query and it make easier to read query or when you are performing  joins.
Syntax

1.      Syntax for ALIAS A COLUMN in SQL is:
column_name AS alias_name

For example:

Aliases used to make the column headings in your result set easier to read. For example, when using the COUNT function,
SELECT designation, COUNT(*) AS TOTAL
FROM employees
GROUP BY designation;

2.      Syntax for ALIAS A TABLE in SQL is:

table_name alias_name

When creating table aliases, it is not necessary to create aliases for all of the tables listed in the FROM clause. You can choose to create aliases on any or all of the tables.
Let's look at an example of how to alias a table name.

For example:

SELECT e.employee_id, e. employee_name, order_details.order_date
FROM employee e
INNER JOIN orders
ON e.employee_id = order.employee_id
WHERE e.employee_id > 5000;

Parameters or Arguments
column_name is the original name of the column that you wish to alias.
table_name is the original name of the table that you wish to alias.
alias_name is the temporary name to assign.


Note

  • It is acceptable to use spaces when you are aliasing a column name. However, it is not generally good practice to use spaces when you are aliasing a table name.
  • If the alias_name contains spaces, you must enclose the alias_name in quotes.
  • The alias_name is only valid within the scope of the SQL statement.

No comments:

Post a Comment

Others

You will like to read it