Aliases

Updated · 9 min read
easyPostgreSQL 18sqlaliasesasselect

An alias is a temporary name you give a column or a table inside one query. Column aliases make results readable: a calculated column called discounted instead of ?column?, or a heading such as Fee (USD) for a report. Table aliases make long queries shorter, which matters once queries use several tables. Neither changes the database; the name lasts only for that query.

In One Minute

  • expression AS name names a result column: fee_usd * 0.9 AS discounted.
  • Put the alias in double quotes to keep capitals or use spaces: AS "Fee (USD)".
  • FROM students AS s gives a table a short name, so you can write s.first_name.
  • An alias works in ORDER BY but not in WHERE, because WHERE runs before the SELECT list is worked out.
  • In PostgreSQL, AS is optional for both column and table aliases, but writing it makes queries clearer.

The Basics

SQL
SELECT expression AS column_alias, ...
FROM table_name AS table_alias;
AliasGives a name toExample
Column aliasOne column of the resultfee_usd * 0.9 AS discounted
Table aliasA table, for the rest of the queryFROM students AS s

Without an alias, a calculated column gets a name made up by the database. PostgreSQL calls it ?column?, which isn't much use in a report:

SQL▶ Try it
SELECT title, fee_usd * 0.9
FROM courses
ORDER BY course_id
LIMIT 2;
Output
    title    | ?column?
-------------+----------
 Java Basics |  405.000
 Java OOP    |  450.000
(2 rows)

With an alias, the heading says what the column means:

SQL▶ Try it
SELECT title AS course, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY course_id
LIMIT 2;
Output
   course    | discounted
-------------+------------
 Java Basics |    405.000
 Java OOP    |    450.000
(2 rows)
An alias renames a column in the result, never in the table

Aliases with Spaces and Capitals

An alias follows the same rules as any other name. Without quotes it's turned into lowercase and can't contain spaces, so AS Course comes out as course. To keep capitals, spaces or symbols, put the alias in double quotes, exactly as you learned in SQL Syntax:

SQL▶ Try it
SELECT title AS "Course", fee_usd AS "Fee (USD)"
FROM courses
ORDER BY fee_usd DESC, title
LIMIT 3;
Output
      Course      | Fee (USD)
------------------+-----------
 Java Collections |    550.00
 Java OOP         |    500.00
 Java Basics      |    450.00
(3 rows)

Table Aliases

A table alias is a short name for a table, used to say which table each column comes from. Write it after the table name, then put it in front of each column with a dot:

SQL▶ Try it
SELECT s.first_name, s.city
FROM students AS s
WHERE s.state = 'CA'
ORDER BY s.first_name;
Output
 first_name |     city
------------+---------------
 Advitha    | San Francisco
 Srihitha   | San Francisco
(2 rows)

With one table, the s. prefix is optional; the database knows every column is from students.

Once you give a table an alias, use the alias everywhere in that query. The alias replaces the table's name, so mixing the two fails, and PostgreSQL's hint points at the alias:

SQL▶ Try it
SELECT students.first_name
FROM students AS s;
Output
ERROR:  invalid reference to FROM-clause entry for table "students"
HINT:  Perhaps you meant to reference the table alias "s".

Why Table Aliases Matter: Two Tables

Table aliases become essential when a query combines tables, because two tables often have a column with the same name. This is a preview of How Joins Work, so don't worry about how JOIN works yet; just look at the names. Both students and enrollments have a student_id column, so asking for student_id alone is ambiguous:

SQL▶ Try it
SELECT student_id, status
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id;
Output
ERROR:  column reference "student_id" is ambiguous

With short aliases, every column says which table it comes from, and the query reads cleanly:

SQL▶ Try it
SELECT s.first_name, e.course_id, e.status
FROM students AS s
JOIN enrollments AS e ON s.student_id = e.student_id
ORDER BY s.first_name, e.course_id;
Output
 first_name | course_id |  status
------------+-----------+-----------
 Advitha    |         1 | active
 Advitha    |         4 | active
 Navika     |         5 | active
 Srihitha   |         1 | completed
 Srihitha   |         2 | completed
 Srihitha   |         3 | active
 Srihitha   |         4 | active
(7 rows)
Two tables share student_id; s. and e. say which one you mean

Where You Can Use an Alias

A column alias works in ORDER BY, but not in WHERE. Sorting by an alias is fine:

SQL▶ Try it
SELECT title, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY discounted DESC, title;
Output
       title       | discounted
-------------------+------------
 Java Collections  |    495.000
 Java OOP          |    450.000
 Java Basics       |    405.000
 JavaScript Basics |    405.000
 SQL Basics        |    360.000
 Git Basics        |    225.000
(6 rows)

Filtering by it fails:

SQL▶ Try it
SELECT title, fee_usd * 0.9 AS discounted
FROM courses
WHERE discounted > 400;
Output
ERROR:  column "discounted" does not exist

The reason is the order the database works in, which isn't the order you write. For the basic queries in this lesson, think of the logical order as: find the rows (FROM, then WHERE), work out the SELECT list, where the alias is born, and sort last (ORDER BY). More clauses join this picture as you learn them. When WHERE runs, discounted doesn't exist yet. The fix is to repeat the expression: WHERE fee_usd * 0.9 > 400.

WHERE runs before SELECT, so it can't see an alias; ORDER BY runs after

In Other Databases

In PostgreSQL, AS is optional for both column aliases (SELECT title course) and table aliases (FROM students s); writing it makes queries easier to read, and a missing comma can't quietly turn into an alias. Other databases differ in the details:

DatabaseTable aliasQuoting an alias with spaces
PostgreSQLstudents AS s or students s"Fee (USD)"
Oraclestudents s only: AS isn't allowed before a table alias (Oracle only)"Fee (USD)"
MySQLstudents AS s or students sBackticks, `Fee (USD)`, or double quotes with the ANSI_QUOTES setting (MySQL only)
SQL Serverstudents AS s or students sSquare brackets, [Fee (USD)], or double quotes with QUOTED_IDENTIFIER on, its default (SQL Server only)

Tabs showing each database's version are coming to this course.

Common Mistakes

Using a column alias in WHERE. WHERE discounted > 400 fails with column "discounted" does not exist. Repeat the expression instead.

Single quotes around an alias. AS 'Fee (USD)' is a syntax error in PostgreSQL, because single quotes make text values, not names. Use double quotes: AS "Fee (USD)".

Expecting capitals to survive without quotes. AS Course gives a column called course. Quote it to keep the capital.

A missing comma that turns into an alias. SELECT title fee_usd FROM courses returns one column, title, renamed to fee_usd. Without AS, a forgotten comma doesn't cause an error; it quietly creates an alias.

Practice

Challenge: Show the three most expensive courses, with the title in a column called Course and the fee in a column called Fee (USD), most expensive first. Break any tie by title.

Show solution
SQL▶ Try it
SELECT title AS "Course", fee_usd AS "Fee (USD)"
FROM courses
ORDER BY fee_usd DESC, title
FETCH FIRST 3 ROWS ONLY;
Output
      Course      | Fee (USD)
------------------+-----------
 Java Collections |    550.00
 Java OOP         |    500.00
 Java Basics      |    450.00
(3 rows)

The quoted aliases keep the capitals, the space and the parentheses. Java Basics and JavaScript Basics tie at $450.00, and sorting by title as well picks Java Basics for third place every time.

Quick check: Aliases

3 questions
1What does AS discounted do in SELECT fee_usd * 0.9 AS discounted FROM courses?
2Why does WHERE discounted > 400 fail?
3Which alias keeps its capitals and space?

Key Takeaways

You want toWrite
Name a calculated columnfee_usd * 0.9 AS discounted
A heading with spacesfee_usd AS "Fee (USD)"
A short table nameFROM students AS s
Sort by an aliasORDER BY discounted DESC
Filter by a calculationWHERE fee_usd * 0.9 > 400

Was this tutorial helpful?

Lesson 14 of 115