Column & Table Aliases (AS)

Reviewed & published by Brayan K

Raw column names like total_spent are fine for the database but awkward in a report, and a calculated column has no name at all until you give it one. AS lets you rename columns and tables on the fly — purely for the result of one query.

Part of the free SQL course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll learn column aliases for readable headings, table aliases that make joins compact, and the one rule that trips everyone up: where you can and can't use an alias.

What You'll Learn

Our Sample Table: customers

Plus a related orders table for the join example. Note the database-style column names — exactly the kind we'll dress up with aliases.

1. Column Aliases — Friendly Headings

A column alias renames a column in the result set. The table keeps its real column names; only this query's output changes. Use AS (or just a space) followed by the new name.

An alias is like a name tag at a conference. Your legal name (the column name) doesn't change — but for today, your tag says "Sam from Marketing" so everyone can read it easily.

-- The two tables this lesson uses. Run this block first;
-- every later block queries them.
CREATE TABLE customers (
    id          INTEGER PRIMARY KEY,
    full_name   TEXT,
    total_spent NUMERIC(10,2)
);
INSERT INTO customers VALUES
    (1, 'Amélie Dubois', 1240.55),
    (2, 'Lucas Martin',   980.25),
    (3, 'Mia Chen',      2310.75);

CREATE TABLE orders (
    id          INTEGER PRIMARY KEY,
    customer_id INTEGER,
    amount      NUMERIC(10,2)
);
INSERT INTO orders VALUES
    (100, 1, 420.50),
    (101, 3, 899.99),
    (102, 1, 820.00);

-- AS renames a column in the OUTPUT only
SELECT
    full_name   AS customer,
    total_spent AS "Lifetime Value"
FROM customers;

-- ✅ Expected result:
-- customer | Lifetime Value
-- Amélie Dubois | 1240.55
-- Lucas Martin | 980.25
-- Mia Chen | 2310.75

2. Naming Calculated Columns

This is where aliases stop being a nicety and become essential. A computed column like total_spent * 0.05 has no natural name — without an alias, its heading is the raw expression, and you can't reference it cleanly anywhere else.

-- Uses the customers and orders tables from the first example.
-- Calculated columns have NO name until you give them one
SELECT
    full_name,
    total_spent,
    ROUND(total_spent * 0.05, 2) AS reward_points
FROM customers;

-- ✅ Expected result:
-- full_name | total_spent | reward_points
-- Amélie Dubois | 1240.55 | 62.03
-- Lucas Martin | 980.25 | 49.01
-- Mia Chen | 2310.75 | 115.54

Your Turn: name the columns

Add the alias keyword in both blanks to produce the headings "Customer" and "Spent (USD)".

-- Uses the customers and orders tables from the first example.
-- 🎯 YOUR TURN — add two aliases, then "Try it Yourself"
-- Goal: show the name as "Customer" and the spend as "Spent (USD)".

SELECT
    full_name   ___ "Customer",      -- 👉 replace ___ with AS
    total_spent ___ "Spent (USD)"    -- 👉 replace ___ with AS
FROM customers;

-- ✅ Expected result:
-- Customer | Spent (USD)
-- Amélie Dubois | 1240.55
-- Lucas Martin | 980.25
-- Mia Chen | 2310.75

3. Table Aliases — Short Nicknames

A table alias is a short nickname you use to refer to a table throughout the query. They keep queries compact and are practically mandatory in joins, where two tables may share column names like id.

-- Uses the customers and orders tables from the first example.
-- A table alias is a short nickname for a table
SELECT o.id, o.amount, c.full_name
FROM orders   AS o
JOIN customers AS c ON c.id = o.customer_id;

-- Without aliases this would read:
--   orders.id, orders.amount, customers.full_name
--   FROM orders JOIN customers ON customers.id = orders.customer_id
-- The aliases o and c make it far shorter and clearer.

-- ✅ Expected result:
-- id | amount | full_name
-- 100 | 420.5 | Amélie Dubois
-- 101 | 899.99 | Mia Chen
-- 102 | 820 | Amélie Dubois

4. Where Aliases Work (and Don't)

This catches almost every beginner. Because of the order SQL evaluates a query, a column alias is created after WHERE runs — so you can use it in ORDER BY but not in WHERE.

-- Uses the customers and orders tables from the first example.
-- You CAN use a column alias in ORDER BY...
SELECT full_name, ROUND(total_spent * 0.05, 2) AS reward_points
FROM customers
ORDER BY reward_points DESC;   -- ✅ works in most databases

-- ...but NOT in WHERE — this errors:
--   WHERE reward_points > 50   ❌ alias unknown here
-- Use the full expression instead:
--   WHERE total_spent * 0.05 > 50   ✅

-- ✅ Expected result:
-- full_name | reward_points
-- Mia Chen | 115.54
-- Amélie Dubois | 62.03
-- Lucas Martin | 49.01

🧩 Reorder Challenge

Unscramble these lines into a query that lists each customer's name and a "tax" column (20% of spend), sorted by tax, highest first.

-- SCRAMBLED — arrange into a query that shows each
-- customer's name and a "Tax" column (total_spent * 0.2),
-- sorted by that tax, highest first.

ORDER BY tax DESC
SELECT full_name AS name,
FROM customers
total_spent * 0.2 AS tax

Why: the two SELECT expressions come first (they define name and tax), then FROM, then ORDER BY. The alias tax is usable in ORDER BY because sorting happens after SELECT assigns the alias — which is exactly why you couldn't use tax in a WHERE clause here.

🧠 Quick Recall

Predict the outcome, then reveal.

revenue — the alias replaces the column name in the result. The stored column is still total_spent.

No — WHERE runs before the alias exists. You'd get an "unknown column revenue" error. Use the full expression WHERE total_spent > 1000.

Yes — AS is optional. The column heading becomes customer. Many style guides keep AS for clarity, but it's not required.

Common Errors (and the fix)

Mini-Challenge: VIP Report

Build a report with a "Name" column, a calculated "Tier Bonus" column, and a table alias. Expected headings are in the comments.

-- 🎯 MINI-CHALLENGE — uses only this lesson's ideas
-- Build a "VIP report":
--   1. Column "Name"  = full_name
--   2. Column "Tier Bonus" = total_spent * 0.10  (a calculated column)
--   3. Use a table alias 'c' for customers
--
-- ✅ Expected headings: Name | Tier Bonus

-- your query here

🎉 Lesson Complete

Practice quiz

What does a column alias created with AS change?

  • The stored column name in the table
  • The data type
  • The column's heading in this query's result only
  • Every query that reads the table

Answer: The column's heading in this query's result only. An alias renames a column only in the output of that one query; the table schema is untouched.

Is the AS keyword required to create an alias?

  • No — a space alone works, e.g. SELECT full_name customer
  • Yes, always
  • Only for table aliases
  • Only in joins

Answer: No — a space alone works, e.g. SELECT full_name customer. AS is optional; SELECT full_name customer and SELECT full_name AS customer are identical.

Why does a calculated column like total_spent * 0.05 usually need an alias?

  • The query errors without one
  • It changes the math
  • Calculations are forbidden without aliases
  • Otherwise its heading is the raw expression and it is hard to reference

Answer: Otherwise its heading is the raw expression and it is hard to reference. A computed column has no natural name, so without an alias the heading is the raw expression.

In which clause can you reference a column alias defined in SELECT?

  • WHERE
  • ORDER BY
  • FROM
  • JOIN

Answer: ORDER BY. ORDER BY runs after SELECT assigns aliases, so it can use them; WHERE cannot.

Why can't you use a SELECT column alias in the WHERE clause?

  • WHERE is evaluated before SELECT assigns the alias, so it doesn't exist yet
  • WHERE only allows numbers
  • Aliases are case-sensitive
  • It is just a style rule

Answer: WHERE is evaluated before SELECT assigns the alias, so it doesn't exist yet. WHERE runs before the alias is created, so you must repeat the full expression there.

When do you need double quotes around an alias?

  • Always
  • Only for numbers
  • When it contains spaces or special characters, e.g. AS "Total Price"
  • Never

Answer: When it contains spaces or special characters, e.g. AS "Total Price". A one-word alias needs no quotes; quote it only for spaces, special chars, or exact casing.

Why are table aliases practically mandatory in joins?

  • They speed up the join
  • They disambiguate columns like id that exist in both tables
  • Joins require AS by syntax
  • They sort the result

Answer: They disambiguate columns like id that exist in both tables. When both tables have an id column, a short alias (o.id vs c.id) tells the database which you mean.

After writing FROM orders AS o, how must you reference the amount column?

  • orders.amount
  • either works
  • amount AS o
  • o.amount

Answer: o.amount. Once a table is aliased, the original name is hidden — you must use o.amount, not orders.amount.

What heading does SELECT total_spent AS revenue produce?

  • total_spent
  • revenue
  • AS revenue
  • an error

Answer: revenue. The alias revenue replaces the column name in the result; the stored column stays total_spent.

Why might AS 'name' (single quotes) be problematic for an alias?

  • It is faster
  • It always errors
  • Single quotes may be read as a string literal; use double quotes or none for identifiers
  • It changes casing

Answer: Single quotes may be read as a string literal; use double quotes or none for identifiers. Single quotes denote string literals in strict modes; identifiers use double quotes or no quotes.

Continue this course

Frequently asked questions

Is the AS keyword required for aliases?

No. AS is optional for both column and table aliases — SELECT price total and SELECT price AS total are identical. Most teams keep AS for column aliases (it reads clearly) and drop it for table aliases (SELECT * FROM orders o).

Can I use a column alias in the WHERE clause?

Usually not. WHERE is evaluated before SELECT assigns aliases, so the alias doesn't exist yet. You can reference column aliases in ORDER BY (and in GROUP BY/HAVING in some databases), but in WHERE you must repeat the full expression.

When do I need double quotes around an alias?

Only when the alias contains spaces, special characters, or you want to preserve exact casing — e.g. AS "Total Price". A simple one-word alias like total_price needs no quotes.

Why are table aliases almost mandatory in joins?

When two tables both have an 'id' column, the database can't tell which you mean. A short table alias (o.id vs c.id) disambiguates every column and makes long join queries far shorter and easier to read.

Do aliases change the actual table?

Never. An alias only renames a column or table for the duration of one query's result. The stored schema — real column and table names — is completely untouched.

Related lessons