The AS command is utilized to assign an alias to a column or table, providing a temporary identifier for use within the query.
The given SQL statement establishes two aliases, assigning one to the CustomerID column and another to the CustomerName column.
SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers; |
The provided SQL statement creates two aliases, highlighting that if the alias includes spaces, double quotation marks or square brackets are necessary.
SELECT CustomerName AS Customer, ContactName AS [Contact Person] FROM Customers; |
The given SQL statement establishes an alias named “Address” by combining four columns: Address, PostalCode, City, and Country.
SELECT CustomerName, Address + ‘, ‘ + PostalCode + ‘ ‘ + City + ‘, ‘ + Country AS Address FROM Customers; |
Note: For the SQL statement above to function in MySQL, utilize the following adjustment:
SELECT CustomerName, CONCAT(Address,‘, ‘,PostalCode,‘, ‘,City,‘, ‘,Country) AS Address FROM Customers; |
The following SQL statement retrieves all orders from the customer with CustomerID=4 (Around the Horn). It employs the “Customers” and “Orders” tables, assigning them the aliases “c” and “o” respectively to streamline the SQL query.
SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName=“Around the Horn” AND c.CustomerID=o.CustomerID; |