SQL aliases (AS)

An alias is a temporary name. Column aliases make results readable (total_revenue instead of sum). Table aliases make joins shorter (o instead of orders).

The keyword AS is optional for table aliases and recommended for column aliases.

Syntax

SELECT column AS new_name
FROM table_name AS t;

Try it yourself

Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.

aliases.sql · PostgreSQL
Loading editor…

Examples

1. Column alias

SELECT first_name, country AS customer_country FROM customers;

The result column is called customer_country.

2. Table alias in a join

SELECT o.id, c.first_name FROM orders o JOIN customers c ON c.id = o.customer_id;

o and c are short names for the tables.

Common mistakes

  • Using a column alias in WHERE. WHERE runs before SELECT, so the alias doesn't exist yet. Repeat the expression or use a subquery.

Practice: test yourself

This challenge is from our interactive course. Write the query and press Check answer.

Challenge · 10 pts

Return each customer's first_name and country, but label the country column customer_country.

challenge.sql · PostgreSQL
Loading editor…

FAQ

Can an alias contain spaces?

Yes, if you wrap it in double quotes: AS "Total Revenue". Snake_case is easier to work with.

Learn SQL step by step

65 hands-on exercises, streaks, a leaderboard and a certificate.

Start free →