Column Aliases (AS)
Objectives
By the end of this lesson, you should be able to:
- Rename a column in a result set with
AS - Alias an expression, not just a plain column
- Explain when quoting an alias is necessary
💡 Why this matters: A column’s real name isn’t always what you want in a result, especially once queries start combining tables or calculating values, later in this course.
AScontrols exactly what a result set’s columns are called, without touching the actual table.
⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the
productstable from earlier in this module.
Renaming a Column
SELECT product_name AS name, price AS cost FROM products;
name | cost
------------------------+--------
Wireless Mouse | 24.99
Mechanical Keyboard | 89.99
...
The underlying products table still has columns named product_name and price, AS only relabels them in this particular result set. Run the query again without AS, and the columns are back to their real names.
Aliasing an Expression
Aliases matter even more for expressions (last covered two lessons ago), since an expression has no real column name to fall back on:
SELECT product_name, price, price * 1.1 AS price_with_tax FROM products;
Without AS price_with_tax, PostgreSQL would label that column with the expression itself, ?column? in some tools, far less useful than a name you chose.
Quoting an Alias
An alias follows the same identifier rules from earlier in this module. An alias with a space, or that needs to preserve specific capitalization, needs double quotes:
SELECT product_name AS "Product Name" FROM products;
Without the quotes, AS Product Name would be invalid, SQL would try to read Product and Name as two separate words. A single-word, lowercase alias like name or cost doesn’t need quoting at all.
Try It
- Write a query that selects
product_nameandcategoryfromproducts, aliased tonameandtype. - Write a query that calculates each product’s price with a 20% discount (from two lessons ago), aliased to
discounted_price. - Write a query that aliases
product_nameto"Full Product Name"(with a space), and explain why the quotes are required here.
Recap
ASrenames a column in the result set only, the underlying table is never changed.- Aliasing an expression gives it a meaningful name instead of a generated placeholder.
- An alias with a space or specific capitalization needs double quotes, a plain lowercase alias doesn’t.
Next lesson: this module’s exercises, practicing SQL syntax, SELECT, DISTINCT, and aliases together.