Loading…
When you use aggregate functions, the column name in results can be confusing (like AVG(price)). The AS keyword lets you give columns friendly names (aliases).
SELECT COUNT(*), AVG(price) FROM products;
-- Column names: COUNT(*), AVG(price) — not very clear!
SELECT COUNT(*) AS total_products, AVG(price) AS average_price
FROM products;
-- Column names: total_products, average_price — much better!
| Rule | Example |
|---|---|
| Simple name (no spaces) | <code>AS total_products</code> |
| Name with spaces (use quotes) | <code>AS "Total Products"</code> |
| Works on any column | <code>SELECT name AS product_name</code> |
| Works with expressions | <code>SELECT price * 1.1 AS price_with_tax</code> |
Count the total number of products and give the column the alias total_products.
Downloading SQL engine… (one-time)
This runs entirely in your browser and is cached for next time.