Alias Clause MySQL Essential Training

Alias Clause MySQL Essential Training

MySQL

Alias Clause MySQL Essential Training




In this Alias Clause MySQL Essential Training lesson we will learn one of the important function called Alias. We everyone knows what it means, it assigns a name temporarily instead of the real name. Because that particular function wants to renamed at the certain time.
We can did this in two different ways like column alias and the table alias. You can understand the difference between both of them using the name itself.
Alias Clause MySQL Essential Training – Alias for columns
While we extract a particular set of data from the table it ‘ll be difficult to understand because of they are so technical. To give a column descriptive name we use the alias column name. The following query will gives the structure of the column alias;

We can give column alias name, should use the AS keyword before the descriptive_name. If the column name has any white spaces then we’ve to give it inside the single quotes. Like the following query,

The AS keyword is not a compulsion, so we can omit it in the statement. And additionally we gives expressions as column names. Lets consider the employees table in the sample database.
Consider the employees table for example and select the first and last_name and combines them to gives the full name. So here we use the CONCAT_WS as combine function.

Alias Clause MySQL Essential Training




The above shown the column heading is not comfortable to read. So we can give the name AS FULL NAME as alias to the combined column like the following.

Alias Clause MySQL Essential Training
In Alias Clause MySQL Essential Training lesson, it has the capability to handle the alias column name to sort and some other clause functions. The following is uses the ORDER BY Clause to sort the columns based on the ‘FULL NAME’. The MySQL is consider the alias name as separate column until the next query is executes.

The following statement selects the orders whose total amount are greater than 10000. It uses column aliases in GROUP BY and HAVING clauses.




Alias Clause MySQL Essential Training
We can not use the WHERE clause in the column alias. Because when the MySQL executes the WHERE the columns in the SELECT clause are may not be determined yet.
Alias Clause MySQL Essential Training – Table Alias
The another method is the table alias to give a different name to a table. The AS keyword is optional, so you can omit in the statement.

We can use this alias among with INNER JOIN, LEFT JOIN, SELF JOIN and in SubQueries also. Consider the tables products and employees.
Both tables have the same column name: id. Without using the table alias to qualify the id column, you will get an error message like:

To avoid the above error we can use the following query,

The query above selects id and the number of employees from the products and employees tables. It uses c as a table alias for the products table and o as a table alias for the employees table. The columns in the products and employees tables are referred to via the table aliases.

The above lesson will gives you the clear explanation about the Alias clause and its functionalities.




Leave a Reply