SELECT statement. They don’t exist in the table — they’re computed on the fly at query time.
Sample data
Arithmetic operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | price + 10 |
- | Subtraction | price - cost |
* | Multiplication | price * quantity |
/ | Division | price / 2 |
% | Modulo (remainder) | quantity % 3 |
^ | Exponentiation (PG-specific) | 2 ^ 3 → 8 |
|/ | Square root (PG-specific) | |/ 16 → 4 |
Basic calculated columns
Profit margin
Inventory value
Tax calculation
Mathematical functions
| Function | Description | Example | Result |
|---|---|---|---|
ROUND(n, d) | Round to d decimal places | ROUND(3.14159, 2) | 3.14 |
CEIL(n) | Round up to nearest integer | CEIL(4.2) | 5 |
FLOOR(n) | Round down to nearest integer | FLOOR(4.9) | 4 |
ABS(n) | Absolute value | ABS(-42) | 42 |
MOD(a, b) | Modulo (same as %) | MOD(10, 3) | 1 |
POWER(a, b) | Raise a to the power of b | POWER(2, 3) | 8 |
SQRT(n) | Square root | SQRT(16) | 4 |
GREATEST(a,b,..) | Largest value from a list | GREATEST(10, 20, 5) | 20 |
LEAST(a,b,..) | Smallest value from a list | LEAST(10, 20, 5) | 5 |
Type casting
Convert one data type to another usingCAST or PostgreSQL’s :: shorthand.
◆ Integer division trap
Dividing two integers in PostgreSQL gives an integer result (truncated, not rounded).
SELECT 7 / 2 returns 3, not 3.5. Cast at least one operand to NUMERIC or FLOAT to get a decimal result: SELECT 7::NUMERIC / 2 returns 3.5.CASE — conditional expressions
TheCASE expression adds if/else logic directly in SQL queries.
Simple CASE
Searched CASE (with conditions)
CASE in ORDER BY
CASE with aggregates
CASE for conditional updates
COALESCE, NULLIF, and NULL handling
COALESCE
Returns the first non-NULL value from the argument list.NULLIF
ReturnsNULL if two values are equal, otherwise returns the first value. Useful to prevent division by zero.
Date/time calculated columns
| Function | Description | Example |
|---|---|---|
AGE(date) | Interval from date to now | AGE('2020-01-01') → 5 years 1 mon... |
EXTRACT(field FROM date) | Extract part of date | EXTRACT(YEAR FROM hire_date) |
DATE_PART('field', date) | Same as EXTRACT (older syntax) | DATE_PART('month', hire_date) |
TO_CHAR(date, fmt) | Format date as string | TO_CHAR(NOW(), 'YYYY-MM-DD') |
NOW() | Current timestamp | NOW() |
CURRENT_DATE | Current date (no time) | CURRENT_DATE |
Common interview questions
How do you handle division by zero in SQL?
How do you handle division by zero in SQL?
Use
NULLIF(denominator, 0) to convert zero to NULL before dividing. Division by NULL returns NULL instead of raising an error: SELECT revenue / NULLIF(expenses, 0) AS ratio.What is the difference between COALESCE and IFNULL?
What is the difference between COALESCE and IFNULL?
COALESCE is SQL-standard and accepts multiple arguments, returning the first non-NULL value. IFNULL is MySQL-specific and accepts only two arguments. PostgreSQL doesn’t have IFNULL — use COALESCE instead.Can you use a calculated column in WHERE?
Can you use a calculated column in WHERE?
No, not by its alias. The
WHERE clause executes before SELECT, so aliases aren’t available. You must repeat the expression: WHERE price * quantity > 10000 — not WHERE inventory_value > 10000. Alternatively, use a subquery or CTE to name the calculation first, then filter on it.What is a generated/stored computed column in PostgreSQL?
What is a generated/stored computed column in PostgreSQL?
PostgreSQL 12+ supports generated columns — columns whose value is automatically computed from other columns and stored on disk:Unlike a calculated column in SELECT (computed at query time), a generated column is persisted and can be indexed.