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.

без ASwithout AS sin AS SELECT stock, category FROM products; stock category 40 Electronics 15 Electronics 25 Home з ASwith AS con AS SELECT stock AS "Залишок",SELECT stock AS "In Stock", SELECTstockAS "Cantidad", category AS "Розділ" category AS "Product Type" categoryAS "Sección" FROM products; ЗалишокIn Stock Cantidad РозділProduct Type Sección 40 Electronics 15 Electronics 25 Home Дані ті самі — AS змінює лише заголовок стовпцяSame data — AS only changes the column header Mismos datos —ASsolo cambia el encabezado

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

  • AS renames 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.