Lesson Beginner

Aliases

An alias is a second name.
You use aliases to rename columns in the result, shorten table names inside a query, or name values you compute on the fly.
The query you read should tell you what each piece means, and aliases are how you make that happen.

Column Aliases #

A column alias renames a column in the output only. The underlying table is untouched.

sql
SELECT
  id      AS order_id,
  total   AS order_total
FROM orders;

The result now has columns order_id and order_total. The AS keyword is optional in most SQL dialects (id order_id works the same), but writing AS is clearer, especially when you skim a query a month from now.

Table Aliases #

A table alias gives the table a short nickname you can use for the rest of the query. This is mostly about readability when tables get longer or when you reference the same table twice.

sql
SELECT
  o.id,
  o.total
FROM orders AS o;

Table aliases become essential once you join tables. They tell SQL (and the reader) which table each column comes from. You will use them constantly in the Joins lessons later.

Alias an Expression #

You can also alias values you compute. Without a name, the result column looks like total * 1.1, which is noise. With an alias, it says what it means.

sql
SELECT
  id,
  total,
  total * 1.1 AS total_with_tax
FROM orders;
Warning

On most engines, column aliases defined in SELECT cannot be used in WHERE.
SQL evaluates WHERE before SELECT, so the alias does not exist yet when the filter runs. Reuse aliases only in ORDER BY (which runs after SELECT). If you need the derived value in a filter, repeat the expression or wrap the query in a subquery.

DuckDB, the engine running this site's editors, is a notable exception: its "friendly SQL" lets you reference SELECT aliases in WHERE, GROUP BY, and HAVING. Convenient here, but an error on Postgres, SQL Server, and (for WHERE) MySQL, so don't build the habit if you write portable SQL.

Try It #

The query below uses a column alias, a table alias, and an expression alias together.

Loading SQL editor...

Mistakes to Watch For #

  • Using a SELECT alias inside WHERE or GROUP BY. It will not resolve on most engines (DuckDB allows it, so it works in this site's editors but breaks elsewhere).
  • Quoting aliases with double quotes only when you need case-sensitivity or special characters. Otherwise leave them unquoted.
  • Giving two different columns the same alias: the second one wins, and the first disappears from your result.
  • Over-aliasing trivial names (SELECT id AS id FROM orders adds noise, not clarity).

Practice #

Write a query that returns three columns from orders: the order's id aliased as order_id, the total column as-is, and a computed column discounted_total equal to total * 0.9. Alias the orders table as o and sort the result by discounted_total descending.

Loading SQL editor...

Knowledge check #

3 questions

0 / 3 answered
  1. What does AS do in SELECT total AS revenue FROM orders?

  2. You wrote SELECT total * 1.10 AS price_with_tax FROM orders WHERE price_with_tax > 100. What happens?

  3. Aliases can be referenced in:

Next Step #

Continue to the Filtering section and start with WHERE to return only the rows you care about.