SQL SQL MOC


The AS command assigns an alias to a table or field to temporarily rename it for the query. These can help make long SQL statements more readable or descriptive.

SELECT field AS aliasname FROM tablename;

Example

customers table

customerIdfirstNamelastNameaddresscitycountry
1UrsaVasquezP.O. Box 878, 8416 Nullam St.WorcesterUnited States
2QuynMeyerP.O. Box 670, 7155 Tincidunt St.PriceCanada
3OrliKlein4981 Gravida St.Barrow-in-FurnessUnited Kingdom
4TallulahHines6279 Pellentesque StreetOmahaUnited States
5JoelRossP.O. Box 842, 4634 Egestas AvenueClovenfordsUnited Kingdom
6CharlotteRamos794-1654 A Rd.AkronUnited States
7DennisAveryP.O. Box 506, 4804 Molestie AvenueMatlockUnited Kingdom
8IgorMalone6627 Porttitor Rd.IrvineUnited Kingdom
9ConnorWitt5979 Vel St.TainUnited Kingdom
10KarenMarquezAp 524-1173 Metus. RoadAnnapolis RoyalCanada
SELECT customerId, CONCAT_WS(' ', firstName, lastName) AS fullName FROM customers;
customerIdfullName
1Ursa Vasquez
2Quyn Meyer
3Orli Klein
4Tallulah Hines
5Joel Ross
6Charlotte Ramos
7Dennis Avery
8Igor Malone
9Connor Witt
10Karen Marquez

Table Names

Aliases can shorten queries as well.

SELECT orders.orderId, customers.firstName, customers.lastName, orders.currency, orders.total
FROM orders, customers
WHERE orders.customerId = customers.customerId;

With AS, this query becomes the following.

SELECT o.orderId, c.firstName, c.lastName, o.currency, o.total
FROM orders AS o, customers AS c
WHERE o.customerId = c.customerId;