MySQL

How to Use Column Alias in Select Clause – MySQL

Aliases in MySQL are used to give a temporary name to a table or a column in a table. Aliases are often used to make column names more readable, and they only exist for a limited time.
 

Syntax:
SELECT my_column AS my_alias FROM my_table;

 

 

Example:

Let’s take an example to see how aliases works. For this we will use “Persons” table.

> SELECT * FROM Persons;

+----------+-----------+--------+-------------------------------+
| PersonID |    Name   |  age   |             Address           |
+----------+-----------+--------+-------------------------------+
|    101   |    Alex   |   25   | 819 Saint Francis Way         |
|    102   |   Emily   |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean    |   35   | 188 Clay Street Indiana       |
|    104   |    Bob    |   40   | 285 Java Lane Missouri        |
+----------+-----------+--------+-------------------------------+

The following query creates two aliases, one for “PersonID” column and one for “Name” column:

SELECT PersonID AS 'ID', Name AS 'First Name' FROM Persons;

Output:

+----------+------------+--------+-------------------------------+
|    ID    | First Name |  age   |             Address           |
+----------+------------+--------+-------------------------------+
|    101   |    Alex    |   25   | 819 Saint Francis Way         |
|    102   |   Emily    |   15   | 171 Jarvisville Road Michigan |
|    103   |   Jean     |   35   | 188 Clay Street Indiana       |
|    104   |    Bob     |   40   | 285 Java Lane Missouri        |
+----------+------------+--------+-------------------------------+
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

One thought on “How to Use Column Alias in Select Clause – MySQL

  • The results from the aliased query (SELECT PersonID AS 'ID', Name AS 'First Name' FROM Persons;) will only have TWO tables, not all four.

    Reply

Leave a Reply

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