Sub topic 3
Scalar Functions Inside Expressions
Assume you already know basic SELECT ... FROM ... WHERE queries, common column types, and the difference between a row and a group. This chapter uses those ideas to build one value for each row.
A scalar function is a function that accepts values from one row and returns one value for that row. You can place it anywhere an expression is allowed, including inside the SELECT list, a calculation, or another function call.
Calling a scalar function
A function call has a function name followed by parentheses. Its arguments are the values passed to the function.
SELECT first_name,
UPPER(last_name) AS last_name_upper,
ROUND(salary * 1.05, 2) AS adjusted_salary,
ABS(salary - 75000) AS distance_from_target
FROM employees;This query evaluates each expression separately for every employee:
UPPER(last_name)converts the last name to uppercase.salary * 1.05calculates a five percent increase.ROUND(..., 2)keeps the calculated salary to two decimal places.ABS(...)returns the positive distance from75000.
The result of a scalar function can be used as an input to an operator. An operator can also be used as an input to a function.
flowchart TD
A[Column value: salary] --> B[Multiply by 1.05]
B --> C[ROUND to 2 decimal places]
C --> D[Computed result for one row]Combining and nesting functions
Expressions can contain several function calls. SQL evaluates the inner call before using its result in the outer call.
SELECT employee_id,
LOWER(TRIM(email)) AS normalized_email
FROM employees;TRIM(email) removes spaces at the beginning and end of the value. LOWER(...) then converts the trimmed result to lowercase. The alias normalized_email names the final value.
You can also pass a calculation as a function argument:
SELECT product_id,
ROUND(unit_price * quantity, 2) AS line_total
FROM order_items;Here, multiplication happens before ROUND receives the result. Parentheses make the intended order clear and prevent mistakes when an expression becomes more complex.
Common scalar functions
| Function | Purpose | Example |
|---|---|---|
| `UPPER` | Converts text to uppercase | `UPPER(last_name)` |
| `LOWER` | Converts text to lowercase | `LOWER(email)` |
| `TRIM` | Removes surrounding spaces | `TRIM(customer_name)` |
| `ROUND` | Rounds a numeric value | `ROUND(amount, 2)` |
| `ABS` | Returns the absolute value | `ABS(balance)` |
| `CHAR_LENGTH` | Counts characters in text | `CHAR_LENGTH(description)` |
Function behavior depends on the input type. For example, applying UPPER to a number is invalid in many databases. A numeric function may also return a numeric type whose precision or scale differs from the input.
NULL and function results
Many scalar functions return NULL when an important input is NULL. The function cannot usually invent a value for missing input.
SELECT employee_id,
UPPER(preferred_name) AS display_name
FROM employees;If preferred_name is NULL, display_name will commonly also be NULL. Handle missing values explicitly when the business rule requires a fallback. For example, use COALESCE before applying a text function:
SELECT employee_id,
UPPER(COALESCE(preferred_name, 'UNKNOWN')) AS display_name
FROM employees;The exact set of available functions varies by SQL dialect. One database may use CHAR_LENGTH, while another commonly provides LENGTH. Date formatting, string concatenation, and rounding details also differ. Check the documentation for the database that runs your query instead of assuming that a function name works everywhere.
A scalar function in the SELECT list can usually be named with AS, just like any other computed expression. The alias labels the output column; it does not change the stored data.
Exercise
What value does LOWER(TRIM(' SQL ')) return?
Show answer
It returns sql. TRIM removes the surrounding spaces, and LOWER converts the remaining text to lowercase.