SQL aliases

An alias is an alternate name for a field or value. Aliases are assigned with the AS keyword and can be single words or complete strings. The syntax:

SELECT column_name1 AS alias_name1 FROM table_name;

In this lesson we will use the Customers table to demonstrate how to use aliases in SQL:

+-----+-----------+-------------+-------------------+-----------+--------+
| No  | lName     | fName       | CITY              | COUNTRY   | AMOUNT |
+-----+-----------+-------------+-------------------+-----------+--------+
| 168 | Franco    | Keith       | New Haven         | USA       |   1286 |
| 169 | de Castro | Isabel      | Lisboa            | Portugal  |   2000 |
| 171 | Rancé     | Martine     | Lille             | France    |   1370 |
| 172 | Bertrand  | Marie       | Paris             | France    |   1337 |
| 173 | Tseng     | Jerry       | Cambridge         | USA       |   1188 |
| 175 | King      | Julie       | Bridgewater       | USA       |   2200 |
| 177 | Kentary   | Mory        | Kita-ku           | Japan     |   3200 |
| 181 | Frick     | Michael     | NYC               | USA       |   1286 |
| 186 | Karttunen | Matti       | Helsinki          | Finland   |   1501 |
| 187 | Ashworth  | Rachel      | Manchester        | UK        |   1501 |
| 189 | Cassidy   | Dean        | Dublin            | Ireland   |   3400 |
| 198 | Taylor    | Leslie      | Brickhaven        | USA       |   1216 |
| 201 | Devon     | Elizabeth   | Liverpool         | UK        |   1501 |
| 202 | Tamuri    | Yoshi       | Vancouver         | Canada    |   3300 |
| 204 | Barajas   | Miguel      | Brickhaven        | USA       |   1188 |
| 205 | Young     | Julie       | Pasadena          | USA       |   1166 |
| 206 | Walker    | Brydey      | Singapore         | Singapore |   5200 |
| 209 | Citeaux   | Frédérique  | Strasbourg        | France    |   4100 |
| 211 | Gao       | Mike        | Central Hong Kong | Hong Kong |   1621 |
| 216 | Saavedra  | Eduardo     | Barcelona         | Spain     |   3100 |
| 219 | Young     | Mary        | Glendale          | USA       |   1166 |
| 223 | Kloss     | Horst       | Cunewalde         | Germany   |   2900 |
| 227 | Ibsen     | Palle       | ?rhus             | Denmark   |   1401 |
| 233 | Fresni?re | Jean        | Montréal          | Canada    |   1286 |
| 237 | Camino    | Alejandra   | Madrid            | Spain     |   NULL |
| 239 | Thompson  | Valarie     | San Diego         | USA       |   1166 |
| 240 | Bennett   | Helen       | Cowes             | UK        |   1501 |
+-----+-----------+-------------+-------------------+-----------+--------+

The following SQL statement specifies two aliases, one for the lName column and one for the fName column:

SELECT fName AS ‘First Name’, lName AS ‘Last Name’ FROM customers;

This would produce the following result:

+-------------+-----------+
| First Name  | Last Name |
+-------------+-----------+
| Keith       | Franco    |
| Isabel      | de Castro |
| Martine     | Rancé     |
| Marie       | Bertrand  |
| Jerry       | Tseng     |
| Julie       | King      |
| Mory        | Kentary   |
| Michael     | Frick     |
| Matti       | Karttunen |
| Rachel      | Ashworth  |
| Dean        | Cassidy   |
| Leslie      | Taylor    |
| Elizabeth   | Devon     |
| Yoshi       | Tamuri    |
| Miguel      | Barajas   |
| Julie       | Young     |
| Brydey      | Walker    |
| Frédérique  | Citeaux   |
| Mike        | Gao       |
| Eduardo     | Saavedra  |
| Mary        | Young     |
| Horst       | Kloss     |
| Palle       | Ibsen     |
| Jean        | Fresni?re |
| Alejandra   | Camino    |
| Valarie     | Thompson  |
| Helen       | Bennett   |
+-------------+-----------+
Geek University 2022