Learn SQL
-- tables.aliases_expressions

Aliases & Expressions in SELECT in SQL

Compute per row: arithmetic, concat, guarded division

--concept

SELECT evaluates expressions per row: arithmetic, string concat (|| standard, CONCAT everywhere), and function calls. AS renames output (salary * 12 AS annual). Integer division truncates — cast one side (salary / 12.0). Division by zero errors, so guard with NULLIF(denominator, 0). Aliases work in ORDER BY, never in WHERE.

--how it works
1

Alias the output

AS renames per-row computations for clients. Optional keyword, recommended habit.

2

Compute per row

Arithmetic, concat (|| / CONCAT), function calls — all evaluated per input row.

3

Mind integer math

INT/INT truncates. Add .0 or CAST one side for decimals.

4

Guard the edges

NULLIF(x,0) turns division-by-zero into NULL. Aliases visible in ORDER BY only.

--visualization
monthly = salary / 12.0 AS monthly — cents kept
namemonthly
Alice7083.33
Bob4333.33
Cynthia7583.33
Stage 1 / 1
Decimal divisor keeps cents. Alias monthly reusable in ORDER BY, invisible in WHERE.
--example
query.sql
1SELECT name, salary / 12.0 AS monthly, salary / NULLIF(age - age, 0) AS guarded FROM employees LIMIT 2;
Result
namemonthlyguarded
Alice7083.33null
Bob4333.33null

Monthly pay uses /12.0 for decimals, not truncated integers. The guarded column divides by zero but NULLIF turns it into NULL instead of an error.

--variants

Monthly pay without truncation

The .0 is load-bearing.

query.sql
1SELECT name, salary / 12.0 AS monthly FROM employees ORDER BY monthly DESC LIMIT 2;
Result
namemonthly
Cynthia7583.33
Alice7083.33
1Each salary divides by decimal 12.0 — fractional cents kept.
2Alias monthly labels the output for the client.
3ORDER BY reuses the alias legally (runs after SELECT).

Guarded division + concat

NULLIF plus standard concat.

query.sql
1SELECT name || ' (' || department || ')' AS label, salary / NULLIF(age - age, 0) AS guarded FROM employees LIMIT 2;
Result
labelguarded
Alice (Engineering)null
Bob (Sales)null
1Concat builds a display label per row.
2Denominator is zero — NULLIF converts to NULL, no error.
3Result NULL signals undefined, not wrong-number.
--key facts
Integer trapsalary/12 truncates — the .0 carries the decimal
NULLIF guardNULLIF(x,0) makes division-by-zero yield NULL
Alias scopeusable in ORDER BY, invisible in WHERE/GROUP BY
--use cases
Report columns: annual/monthly pay, display labels
Rate math with zero-safe denominators
--under the hood
Expression evaluation is per-row CPU; wrapping indexed columns in functions kills sargability — compute in SELECT, filter on bare columns.
Quoting varies: "..." standard, [...] MSSQL, `...` MySQL.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.