SQL

Naming Columns with AS

Column Aliases with AS

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).

Without AS

SELECT COUNT(*), AVG(price) FROM products;
-- Column names: COUNT(*), AVG(price) — not very clear!

With AS

SELECT COUNT(*) AS total_products, AVG(price) AS average_price
FROM products;
-- Column names: total_products, average_price — much better!

Rules for Aliases

RuleExample
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>

💡 Why Aliases Matter

  • Readability: Results make sense to non-technical people
  • Reports: Column headers in exports look professional
  • Reuse: Other parts of SQL can reference the alias
  • Documentation: Self-documenting queries

Your Task

Count the total number of products and give the column the alias total_products.

query.sql
Loading SQL engine...
Downloading SQL engine… (one-time)

Downloading SQL engine… (one-time)

This runs entirely in your browser and is cached for next time.