SQL SELECT AS statement (Aliases name)
The SQL SELECT AS statement is used to assign an alias to a column or expression in a SELECT statement. The alias acts as a temporary name for the column or expression, and it can be used to make the result set easier to read or to provide a more meaningful name for the column or expression.
Syntax for a SELECT AS statement
1SELECT column1 AS alias1, column2 AS alias2, ... columnN AS aliasN 2FROM table_name;
In this syntax, column1, column2, ... columnN
are the names of the columns that you want to retrieve, alias1, alias2, ... aliasN
are the aliases that you want to assign to the columns, and table_name
is the name of the table from which you want to retrieve the data.
For example, consider a table named customers
that contains the following data:
1+----+-------+--------+ 2| id | name | city | 3+----+-------+--------+ 4| 1 | William | London | 5| 2 | Jane | Paris | 6| 3 | William | Berlin | 7| 4 | Alice | Paris | 8+----+-------+--------+
If you want to retrieve the name
and city
columns, but with more meaningful column names, you can use the following SELECT AS statement:
1SELECT name AS customer_name, city AS customer_city 2FROM customers;
The result set of this statement will be:
1+------------+--------------+ 2| customer_name | customer_city | 3+------------+--------------+ 4| William | London | 5| Jane | Paris | 6| William | Berlin | 7| Alice | Paris | 8+------------+--------------+
As you can see, the columns have been given more meaningful names using the SELECT AS statement.