Expressions and Aliases
Computing new values inside a query — arithmetic, text, and functions — and naming the results so your output reads clearly.
A query does not just hand back stored values; it can compute new
ones. You have already done a little of this (price * 2). This
page makes it a deliberate tool: building calculated columns,
combining text, calling functions, and giving every result a clear
name.
Calculated columns
Anywhere you can name a column, you can instead write an expression — a small calculation. The database evaluates it for each row:
price * qty is not stored anywhere — it is computed fresh for each
row and returned as a new column named line_total. This is how
queries turn raw data into answers.
Aliases make output readable
That AS line_total is an alias — a name you give a result
column. Without it, a computed column gets an ugly auto-name like
?column?. Aliases are not decoration; they make results
self-explanatory and let later parts of bigger queries refer to the
value by name.
The word AS is optional, but be kind
PostgreSQL lets you write the alias without AS —
price * qty line_total works. Including AS is clearer for
readers, so this course always writes it. (One caution: alias names
with spaces or capitals need double quotes, e.g.
AS "Line Total".)
Working with text
Numbers are not the only thing you can compute. The || operator
joins (concatenates) text, and functions like UPPER, LOWER,
and LENGTH transform it:
first || ' ' || last glues the first name, a space, and the last
name into one value. Reading and reshaping text like this is
everyday SQL.
Functions do focused jobs
A function takes input(s) and returns a value. You have just
used UPPER and LENGTH. There are many, grouped by what they work
on:
| Kind | Examples | Does |
|---|---|---|
| Numeric | ROUND, ABS, CEIL | Round, absolute value, round up |
| Text | UPPER, LOWER, TRIM, LENGTH | Transform and measure text |
| Date | NOW, AGE, EXTRACT | Work with dates and times |
Here one expression multiplies price by quantity, adds 8% tax, and
ROUND(..., 2) trims it to two decimal places — a realistic little
calculation, expressed in a single readable line.
Expressions work in WHERE and ORDER BY too
Expressions are not limited to SELECT. You can filter and sort by
them as well:
The database computes price * qty to decide which rows pass the
filter and how to sort them — even though you wrote that expression
in three different clauses.
Check your understanding
In SELECT price * qty AS line_total, where is the value line_total stored?
It is permanently saved as a new column in the table.
Nowhere permanently — it is computed for each row and returned only in the result.
In a separate table created automatically.
It replaces the price column in storage.
What does the || operator do in PostgreSQL?
It adds two numbers together.
It compares two values for equality.
It concatenates (joins) text values into one string.
It selects every column.
What is the purpose of an alias (the AS name part)?
It changes the value being computed.
It gives a result column a clear, readable name in the output.
It is required for every column or the query fails.
It permanently renames the column in the table.
Sorting and Limiting
Putting results in a deliberate order with ORDER BY, and grabbing just the top rows with LIMIT — the tools behind every "top 10" list.
Working with NULL
The special value that means "unknown," why it behaves so strangely, and how to test for it correctly — a concept that trips up nearly every beginner.