🎯 Few days left this month Support SQLTest.online and help us keep SQL practice free for everyone. Donate today →
SQL code copied to buffer

SQL Alias: Renaming Columns with AS

Lesson 2.5 · Reading time: ~5 min · Practice tasks ↓

A SQL column alias is a temporary name that you give to a column in the query result with the AS keyword. In this lesson you'll learn the alias syntax, how to write aliases with spaces, why calculated columns need aliases, and where in a query an alias can be used.

In the previous lessons you selected columns exactly as they are named in the table. Table column names are often technical, like rental_rate or replacement_cost, and a calculated value such as length / 60 has no proper name at all. Aliases let you control the headers of the result.

SQL column alias: the AS keyword renames a column in the result, quoted aliases with spaces, and an alias for a calculated column

What Is a Column Alias?

A column alias assigns a temporary name to a column in the result of a SELECT query. It does not change the column in the table: the alias exists only in the output of this one query.

Basic Syntax

SELECT column_name AS alias_name
FROM table_name;

The AS keyword is optional, so SELECT column_name alias_name works too. Writing AS explicitly makes the query easier to read, because the alias doesn't look like a second column.


Renaming a Column

SELECT title AS film_title,
       rental_rate AS price
FROM film;

Result: all films with two columns named film_title and price instead of title and rental_rate.


Aliases with Spaces

If an alias contains spaces or special characters, enclose it in double quotes:

SELECT first_name AS "Given Name",
       last_name AS "Surname"
FROM actor;

Result: the list of actors with the column headers "Given Name" and "Surname".

Double quotes are the SQL standard. MySQL and MariaDB also accept backticks (`Given Name`), and SQL Server accepts square brackets ([Given Name]). In practice, short names without spaces, such as given_name, are easier to use later in the query.


Aliases for Calculated Columns

When a column is calculated from an expression, the database generates a header from the expression text, for example length / 60. An alias gives it a proper name:

SELECT title,
       length AS length_minutes,
       length / 60 AS length_hours,
       rental_rate * rental_duration AS full_rental_cost
FROM film;

Result: every film with its length in minutes and hours, and the cost of renting it for the whole rental period.

Calculated columns are the most common reason to use aliases: without them the result is hard to read, and applications that read the result by column name can't find the value.


Where You Can Use an Alias

An alias is created in the SELECT list, so it is available only in the parts of the query that are processed after SELECT:

ClauseCan use a column alias?
ORDER BYYes, in all major databases
WHERENo: WHERE is processed before SELECT
GROUP BY, HAVINGDepends on the database (MySQL and PostgreSQL allow it in GROUP BY)
SELECT title, rental_rate * rental_duration AS full_rental_cost
FROM film
ORDER BY full_rental_cost DESC;

Result: films sorted from the most to the least expensive full rental.

If a query has to work in several databases, repeat the expression instead of the alias in WHERE, GROUP BY and HAVING. You'll learn more about the processing order in Lesson 2.7.


Key Takeaways:

  • A column alias is a temporary name for a column in the query result; the table doesn't change.
  • Write aliases with AS: SELECT title AS film_title. The keyword is optional but improves readability.
  • Enclose aliases with spaces in double quotes: AS "Given Name".
  • Give every calculated column an alias.
  • An alias can be used in ORDER BY, but not in WHERE.

Frequently Asked Questions

Is the AS keyword required for an alias in SQL?

No. SELECT title film_title works the same as SELECT title AS film_title. Using AS is recommended because it shows clearly where the column ends and the alias begins.

Why can't I use a column alias in WHERE?

WHERE is processed before SELECT, so the alias doesn't exist yet when rows are filtered. Repeat the expression in WHERE, or use the alias in ORDER BY, which is processed after SELECT.

Does an alias rename the column in the table?

No. An alias affects only the result of the current query. To rename a column in the table itself, use ALTER TABLE ... RENAME COLUMN.

Try solving the following tasks to reinforce what you learned in this lesson.

  1. What Is an Alias in SQL?
  2. Aliases That Break a Query