Module 01 · lesson 3
Column aliases (AS)
The founder is preparing a report for partners. The header "name" looks too techie — in the report they'd rather have "Product Name" and "Unit Price", so it reads easily.
What an alias is
An alias is a temporary name for a column in the result. You set it with AS:
SELECT stock AS "In Stock", category AS "Product Type"
FROM products;
In the response the headers will be "In Stock" and "Product Type" — even though in the table itself the columns are still stock and category. An alias changes only the label in the response, not the column itself.
Quotes matter
If the new name contains a space or a special character, put it in double quotes. In PostgreSQL this is fundamental:
- double quotes
"..."— a name (an identifier); - single quotes
'...'— a text value.
Put an alias in single quotes and you'll get an error: single quotes mean a text value, and that isn't allowed where a column name (an identifier) belongs. So for aliases — always double quotes.
You can even drop the word AS (stock "In Stock"), but it reads more clearly with it.
Your turn
Make the report clear: output name as "Product Name" and price as "Unit Price".
Summary
ASrenames columns in the result.- Names with spaces or special characters go in double quotes (single quotes mean text → an error).
- The table itself doesn't change.