Aliases & Expressions in SELECT in SQL
Compute per row: arithmetic, concat, guarded division
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.
Alias the output
AS renames per-row computations for clients. Optional keyword, recommended habit.
Compute per row
Arithmetic, concat (|| / CONCAT), function calls — all evaluated per input row.
Mind integer math
INT/INT truncates. Add .0 or CAST one side for decimals.
Guard the edges
NULLIF(x,0) turns division-by-zero into NULL. Aliases visible in ORDER BY only.
1SELECT name, salary / 12.0 AS monthly, salary / NULLIF(age - age, 0) AS guarded FROM employees LIMIT 2;
| name | monthly | guarded |
|---|---|---|
| Alice | 7083.33 | null |
| Bob | 4333.33 | null |
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.
Monthly pay without truncation
The .0 is load-bearing.
1SELECT name, salary / 12.0 AS monthly FROM employees ORDER BY monthly DESC LIMIT 2;
| name | monthly |
|---|---|
| Cynthia | 7583.33 |
| Alice | 7083.33 |
Guarded division + concat
NULLIF plus standard concat.
1SELECT name || ' (' || department || ')' AS label, salary / NULLIF(age - age, 0) AS guarded FROM employees LIMIT 2;
| label | guarded |
|---|---|
| Alice (Engineering) | null |
| Bob (Sales) | null |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.