Sub topic 1
Computing Values in the SELECT List
Prerequisites
This chapter assumes that you can write basic SELECT ... FROM ... WHERE queries. You should also know common column types, such as numbers, text, and dates, and understand the purpose of GROUP BY and aggregate functions such as COUNT and SUM.
A SELECT list can return more than stored columns. An expression is a combination of column names, literal values, operators, and functions that produces a value for each result row.
SELECT first_name,
last_name,
salary,
salary * 12 AS annual_salary
FROM employees;For each employee, salary * 12 calculates a new value. The query returns that value as annual_salary. The calculation does not change the data stored in employees.
Build an expression from parts
A literal is a value written directly in a query. Examples include 12, 0.15, 'active', and DATE '2026-01-01'. Operators combine values:
- Arithmetic operators include
+,-,*, and/. - Comparison operators include
=,<>,<,>,<=, and>=. - String concatenation often uses
||, although some systems useCONCAT. - Parentheses control the order of evaluation.
SELECT employee_id,
salary,
salary * 12 AS annual_salary,
salary * 12 * 1.10 AS annual_salary_with_raise
FROM employees;SQL normally evaluates multiplication before addition or subtraction. Use parentheses when the intended order is important:
SELECT salary * (1 + bonus_rate) AS pay_with_bonus
FROM employees;Here, the expression first adds 1 and bonus_rate, then multiplies the result by salary.
Use scalar functions
A scalar function accepts values from one row and returns one value for that row. Common examples include:
UPPER(name)converts text to uppercase.LOWER(name)converts text to lowercase.ROUND(amount, 2)rounds a number.LENGTH(name)returns the length of text.COALESCE(value, replacement)chooses a replacement whenvalueisNULL.
Functions can be nested inside larger expressions.
SELECT first_name,
UPPER(last_name) AS last_name_upper,
ROUND(salary / 12, 2) AS monthly_salary
FROM employees;The exact function names and argument rules can vary between database systems. Check the documentation for the database you use, especially for date and string functions.
Account for NULL
NULL represents a missing, unknown, or inapplicable value. It is not the same as zero or an empty string.
In most expressions, NULL makes the result NULL:
SELECT first_name,
salary + commission AS total_pay
FROM employees;If commission is NULL, total_pay is also NULL. Use COALESCE when a missing value should behave like a known fallback:
SELECT first_name,
salary + COALESCE(commission, 0) AS total_pay
FROM employees;Comparisons with NULL also require care. commission = NULL does not find missing commissions. Use commission IS NULL or commission IS NOT NULL.
A division can fail when its denominator is zero. NULLIF converts a matching value to NULL, avoiding the error:
SELECT total_revenue / NULLIF(order_count, 0) AS average_order_value
FROM sales_summary;If order_count is zero, the result is NULL instead of a division-by-zero error.
Check the result
When writing an expression, test ordinary values and boundary cases. Include rows where an input is NULL, zero, negative, or unusually large. Confirm the result type too. For example, integer division may discard a fractional part in some database systems, so decimal arithmetic may require a cast or decimal literal.
Exercise
What does salary + commission return when commission is NULL, and how can you treat a missing commission as zero?
Show answer
The result is NULL. Use salary + COALESCE(commission, 0).