An alias is a temporary name you give to a column or a table inside a single query. Aliases make results easier to read and long queries easier to write. They do not rename anything in the database itself.
Column aliases
Use AS after a column or expression to name it in the result:
Aliases are most valuable for calculated columns, which otherwise get headings like price * stock:
SELECT name,
price * stock AS stock_value
FROM products
ORDER BY stock_value DESC;Try it Yourself »Notice that the alias stock_value can be used in ORDER BY. The database works out the SELECT list before sorting, so the new name is already known.
Aliases with spaces
If you want a heading with spaces or capital letters, put the alias in double quotes:
SELECT name AS "Customer",
contact AS "Contact Person",
city || ', ' || COALESCE(country, '?') AS "Location"
FROM customers;Try it Yourself » Contact Person ) and SQL Server accepts square brackets ([Contact Person]`). Single quotes are for text values, not names, so avoid them for aliases.AS is optional
The keyword AS can be left out: SELECT name product FROM products works. But it is easy to misread, especially if you forget a comma (SELECT name price silently renames name to price!). Writing AS makes your intention clear.
AS for column aliases. It costs two characters and prevents confusing mistakes.Table aliases
You can also give a table a short alias in the FROM clause, then use it to qualify column names:
SELECT p.name, p.category, p.price
FROM products AS p
WHERE p.price > 10000;Try it Yourself »With a single table this is just shorter. With several tables it becomes essential, because two tables can have columns with the same name. Both customers and products have a column called name, and both orders and customers have customer_id. Writing c.name or o.customer_id makes clear which one you mean:
SELECT o.order_id, o.order_date, c.name AS customer
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
ORDER BY o.order_date;Try it Yourself »Joins are covered properly in INNER JOIN. For now, notice how much shorter o. and c. are than repeating orders. and customers. everywhere.
AS before a table alias; write FROM orders o. Leaving AS out for table aliases works in every database, so you will often see it written that way.Where can you use an alias?
A column alias belongs to the result, so it is available in ORDER BY, but in standard SQL it is not available in WHERE. The WHERE clause is applied before the SELECT list is calculated, so the alias doesn't exist yet:
-- Not portable: many databases reject this
SELECT name, price * stock AS stock_value
FROM products
WHERE stock_value > 200000;
Repeat the expression instead:
SELECT name, price * stock AS stock_value
FROM products
WHERE price * stock > 200000;Try it Yourself »WHERE, but MySQL, PostgreSQL and SQL Server do not. Write the portable version.Show each product's price including 16% VAT, in a column named with_vat.
SELECT name, price * 1.16 with_vat FROM products;Give the customers table the short alias c.
SELECT c.name FROM customers AS ;
SKOPE