Column Aliases in PostgreSQL

Learn how to use column aliases in PostgreSQL SELECT statements with AS, handle spaces in alias names, and understand where aliases can and cannot be used.

5 min read · Back to overview

Quick Answer

A column alias assigns a temporary name to a column or expression in a SELECT statement using the AS keyword. Aliases rename output headings, make expressions readable, and can be referenced in ORDER BY — but not in WHERE or HAVING.

Spin up a Postgres database in 20 seconds with Vela.

Try Vela Sandbox

A column alias in PostgreSQL assigns a temporary display name to a column or expression in the SELECT list. The alias exists only for the duration of the query — it is not stored in the database, does not rename any table column, and has no effect on the underlying data.

Syntax

SELECT column_name AS alias_name
FROM table_name;

The AS keyword is optional. Both of the following are valid and equivalent:

SELECT unit_price AS price FROM products;
SELECT unit_price price FROM products;

You can alias any expression, not just bare column references:

SELECT expression AS alias_name
FROM table_name;

When an alias contains spaces or special characters, wrap it in double quotes:

SELECT unit_price * stock_qty AS "inventory value"
FROM products;

Practical Example

Create a sample order table and use aliases to produce readable output:

CREATE TABLE orders (
  id           SERIAL PRIMARY KEY,
  reference    VARCHAR(20) NOT NULL UNIQUE,
  customer_id  INT NOT NULL,
  subtotal     NUMERIC(10, 2) NOT NULL,
  tax_rate     NUMERIC(5, 4) NOT NULL DEFAULT 0.08,
  placed_at    TIMESTAMPTZ NOT NULL DEFAULT now()
);

INSERT INTO orders (reference, customer_id, subtotal, tax_rate) VALUES
  ('ORD-1001', 42, 129.99, 0.08),
  ('ORD-1002', 17,  49.50, 0.10),
  ('ORD-1003', 99, 310.00, 0.08),
  ('ORD-1004', 42,  22.75, 0.05);

Rename a column heading:

SELECT reference AS order_ref, placed_at AS ordered_on
FROM orders;

Result headings change from reference / placed_at to order_ref / ordered_on.

Alias a computed expression:

Without an alias, PostgreSQL labels computed columns with ?column?:

-- Unhelpful heading: ?column?
SELECT reference, subtotal * (1 + tax_rate)
FROM orders;

Add an alias for a meaningful heading:

SELECT
  reference,
  subtotal,
  tax_rate,
  subtotal * (1 + tax_rate) AS total_with_tax
FROM orders
ORDER BY total_with_tax DESC;

Use an alias in ORDER BY:

Because ORDER BY is evaluated after SELECT, you can reference the alias:

SELECT
  reference,
  EXTRACT(EPOCH FROM (now() - placed_at)) / 3600 AS hours_since_order
FROM orders
ORDER BY hours_since_order DESC;

Inspect output column names:

-- Use psql \g or \x to see column names in vertical format
SELECT reference AS order_ref, subtotal AS amount FROM orders LIMIT 1;

Where Aliases Can and Cannot Be Used

ClauseAlias available?Reason
ORDER BYYesEvaluated after SELECT
GROUP BYPartially (non-standard)PostgreSQL extension; avoid for portability
HAVINGNoEvaluated before SELECT
WHERENoEvaluated before SELECT
Subquery outer queryYesThe subquery result is a derived table with the alias as its column name

Workaround when you need a computed alias in WHERE: wrap the query in a subquery or CTE:

-- Use a subquery to filter on a computed alias
SELECT *
FROM (
  SELECT reference, subtotal * (1 + tax_rate) AS total_with_tax
  FROM orders
) t
WHERE total_with_tax > 100;

Testing with Vela

When refactoring queries to use aliases — for example, renaming output columns to match a new API contract — use Vela’s database branching to test the change against a production-like copy before rolling it out. Create a branch, run the updated queries, verify that application code consuming the renamed columns behaves correctly in your CI environment, and then promote the change to production with confidence.

Production Tips

  • Always include AS explicitly even though it is optional — it signals intent and makes diffs easier to review.
  • Use snake_case aliases to match PostgreSQL’s native identifier casing (total_with_tax, not TotalWithTax) — unquoted identifiers are folded to lowercase.
  • Wrap aliases with spaces or special characters in double quotes; avoid them in application-facing column names to reduce quoting overhead in downstream code.
  • Do not shadow existing column names with aliases unless intentional — it can cause confusion when queries are nested as subqueries.
  • In ORM-generated queries or reporting tools that read column names programmatically, consistent alias naming across queries prevents field-mapping bugs.
  • If an alias is defined in a SELECT and you need it in HAVING, wrap the query in a subquery or use a CTE rather than repeating the expression multiple times.

Continue in Querying Data: ORDER BY.

Related in this section: PostgreSQL SELECT · ORDER BY · SELECT DISTINCT

Frequently Asked Questions

Is the AS keyword required for column aliases in PostgreSQL?
No, AS is optional. SELECT unit_price total_price FROM products works identically to SELECT unit_price AS total_price FROM products. Including AS is recommended because it improves readability and prevents ambiguity, especially when the alias resembles a SQL keyword.
Can I use a column alias in a WHERE clause?
No. PostgreSQL evaluates WHERE before SELECT, so aliases defined in the SELECT list do not yet exist when WHERE is processed. You must repeat the original column name or expression in the WHERE clause. Aliases are available in ORDER BY, which is evaluated after SELECT.
How do I create a column alias that contains spaces?
Wrap the alias in double quotes: SELECT unit_price * qty AS "line total" FROM order_items. Single quotes denote string literals in SQL and would cause a syntax error if used around an alias name.
Does a column alias rename the actual table column?
No. A column alias is temporary — it only affects the output of that specific query. The column name in the table schema is unchanged. Use ALTER TABLE ... RENAME COLUMN if you want to permanently rename a column.
Can I use a column alias in GROUP BY or HAVING?
Not in HAVING for aliases defined in the same SELECT. PostgreSQL allows alias names in GROUP BY as a non-standard extension in some cases, but for portability and clarity, repeat the original expression in both GROUP BY and HAVING rather than relying on the alias.