Column & Table Aliases (AS)

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.

Learn Column & Table Aliases (AS) in our free SQL course — a beginner-friendly interactive lesson with runnable queries, a practice exercise and a quick recall.

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.

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

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.

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.

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

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 .

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 .

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

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.

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.

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

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.