Aliases

Column Headings

Aliases can be used to change the column headings in the results of SELECT queries.

Aliases containing spaces must be enclosed in square brackets or quotation marks.

SELECT productName AS [Product Name], price as Price
FROM Product

Calculated Values can also be given meaningful column headings:

SELECT productName AS [Product Name], price, quantity,
    price*quantity AS [Product Cost]
FROM Product, Order
WHERE Product.productID = Order.productID
   AND order# = 123456;
SELECT MIN(pricePersonNight) AS "Cheapest Price per Night",
    MAX(pricePersonNight) AS "Dearest Price per Night"
FROM Hotel;

Table Names

Multi-table queries will include the table names several times:

  • in the FROM clause
  • in the WHERE clause (linking the tables’ keys)
  • in other clauses
SELECT productCategory, MAX(price)
FROM Product, Supplier
WHERE supplier.location = "Greece"
    AND supplier.supplierID = product.supplierD
GROUP BY productCategory
ORDER BY MAX(price) ASC;

A shorter alias can be created in the FROM clause, and then used elsewhere:

SELECT productCategory, MAX(price)
FROM Product p, Supplier s
WHERE s.location = "Greece"
    AND s.supplierID = p.supplierD
GROUP BY productCategory
ORDER BY MAX(price) ASC;