Aliases
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 namenames 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 sgives a table a short name, so you can writes.first_name.- An alias works in
ORDER BYbut not inWHERE, becauseWHEREruns before theSELECTlist is worked out. - In PostgreSQL,
ASis optional for both column and table aliases, but writing it makes queries clearer.
The Basics
SELECT expression AS column_alias, ...
FROM table_name AS table_alias;| Alias | Gives a name to | Example |
|---|---|---|
| Column alias | One column of the result | fee_usd * 0.9 AS discounted |
| Table alias | A table, for the rest of the query | FROM 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:
SELECT title, fee_usd * 0.9
FROM courses
ORDER BY course_id
LIMIT 2; title | ?column?
-------------+----------
Java Basics | 405.000
Java OOP | 450.000
(2 rows)With an alias, the heading says what the column means:
SELECT title AS course, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY course_id
LIMIT 2; course | discounted
-------------+------------
Java Basics | 405.000
Java OOP | 450.000
(2 rows)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:
SELECT title AS "Course", fee_usd AS "Fee (USD)"
FROM courses
ORDER BY fee_usd DESC, title
LIMIT 3; 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:
SELECT s.first_name, s.city
FROM students AS s
WHERE s.state = 'CA'
ORDER BY s.first_name; 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:
SELECT students.first_name
FROM students AS s;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:
SELECT student_id, status
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id;ERROR: column reference "student_id" is ambiguousWith short aliases, every column says which table it comes from, and the query reads cleanly:
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; 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)Where You Can Use an Alias
A column alias works in ORDER BY, but not in WHERE. Sorting by an alias is fine:
SELECT title, fee_usd * 0.9 AS discounted
FROM courses
ORDER BY discounted DESC, title; 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:
SELECT title, fee_usd * 0.9 AS discounted
FROM courses
WHERE discounted > 400;ERROR: column "discounted" does not existThe 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.
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:
| Database | Table alias | Quoting an alias with spaces |
|---|---|---|
| PostgreSQL | students AS s or students s | "Fee (USD)" |
| Oracle | students s only: AS isn't allowed before a table alias (Oracle only) | "Fee (USD)" |
| MySQL | students AS s or students s | Backticks, `Fee (USD)`, or double quotes with the ANSI_QUOTES setting (MySQL only) |
| SQL Server | students AS s or students s | Square 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
SELECT title AS "Course", fee_usd AS "Fee (USD)"
FROM courses
ORDER BY fee_usd DESC, title
FETCH FIRST 3 ROWS ONLY; 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.
Key Takeaways
| You want to | Write |
|---|---|
| Name a calculated column | fee_usd * 0.9 AS discounted |
| A heading with spaces | fee_usd AS "Fee (USD)" |
| A short table name | FROM students AS s |
| Sort by an alias | ORDER BY discounted DESC |
| Filter by a calculation | WHERE fee_usd * 0.9 > 400 |
Was this tutorial helpful?