Friday , 14 May 2021
Stackode
Home > MySql > Alias Syntax – SQL

Alias Syntax – SQL

Alias Syntax – SQL — You can rename a table or a column temporarily by giving another name known as alias.

The use of table aliases means to rename a table in a particular SQL statement. The renaming is a temporary change and the actual table name does not change in the database.

The column aliases are used to rename a table’s columns for the purpose of a particular SQL query.

Syntax:

The basic syntax of table alias is as follows:

SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];

The basic syntax of column alias is as follows:

SELECT column_name AS alias_name
FROM table_name
WHERE [condition];

Example:

Consider the following two tables, (a) CUSTOMERS table is as follows:

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
|  2 | Khilan   |  25 | Delhi     |  1500.00 |
|  3 | kaushik  |  23 | Kota      |  2000.00 |
|  4 | Chaitali |  25 | Mumbai    |  6500.00 |
|  5 | Hardik   |  27 | Bhopal    |  8500.00 |
|  6 | Komal    |  22 | MP        |  4500.00 |
|  7 | Muffy    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

(b) Another table is ORDERS as follows:

+-----+---------------------+-------------+--------+
|OID  | DATE                | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 |           3 |   3000 |
| 100 | 2009-10-08 00:00:00 |           3 |   1500 |
| 101 | 2009-11-20 00:00:00 |           2 |   1560 |
| 103 | 2008-05-20 00:00:00 |           4 |   2060 |
+-----+---------------------+-------------+--------+

Now, following is the usage of table alias:

SQL> SELECT C.ID, C.NAME, C.AGE, O.AMOUNT 
        FROM CUSTOMERS AS C, ORDERS AS O
        WHERE  C.ID = O.CUSTOMER_ID;

This would produce the following result:

+----+----------+-----+--------+
| ID | NAME     | AGE | AMOUNT |
+----+----------+-----+--------+
|  3 | kaushik  |  23 |   3000 |
|  3 | kaushik  |  23 |   1500 |
|  2 | Khilan   |  25 |   1560 |
|  4 | Chaitali |  25 |   2060 |
+----+----------+-----+--------+

Following is the usage of column alias:

SQL> SELECT  ID AS CUSTOMER_ID, NAME AS CUSTOMER_NAME
     FROM CUSTOMERS
     WHERE SALARY IS NOT NULL;

This would produce the following result:

+-------------+---------------+
| CUSTOMER_ID | CUSTOMER_NAME |
+-------------+---------------+
|           1 | Ramesh        |
|           2 | Khilan        |
|           3 | kaushik       |
|           4 | Chaitali      |
|           5 | Hardik        |
|           6 | Komal         |
|           7 | Muffy         |
+-------------+---------------+

About Whendy

Whendy Blog mengumpulkan beberapa Tutorial Website, PHP, AngularJS, JQuery, Laravel, Codeigniter dan lain-lain. ( jika sempat ) :)

Check Also

Overview – SQL

Overview – SQL tutorial gives unique learning on Structured Query Language and it helps to ...

9 comments

  1. Outstanding post however I was wanting to know if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!

  2. Useful info. Fortunate me I discovered your web site by accident, and I am surprised why this coincidence did not took place earlier! I bookmarked it.

  3. Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and aid others like you aided me.

  4. We’re a group of volunteers and opening a new scheme in our community. Your website offered us with valuable info to work on. You have done a formidable job and our entire community will be grateful to you.

  5. I all the time emailed this weblog post page to all my contacts, for the reason that if like to read it after that my contacts will too.

  6. Very nice post. I just stumbled upon your weblog and wished to say that I’ve truly enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and I hope you write again soon!

  7. Good way of telling, and fastidious piece of writing to get facts on the topic of my presentation focus, which i am going to deliver in institution of higher education.

  8. Thank you, I have just been looking for information approximately this topic for ages and yours is the greatest I have came upon till now. But, what in regards to the conclusion? Are you certain concerning the source?

  9. Informative article, exactly what I wanted to find.

Leave a Reply

Your email address will not be published. Required fields are marked *