What Are SQL Functions?
Understand what a function is, the difference between single-row and multi-row functions, how nesting works, and the SQL functions family.
Your IPL table stores names like Virat Kohli, runs like 750 and dates like 2008-04-20. But reports rarely want the raw value — you want the name in capitals, the strike rate to two decimals, the debut year only, or "Unsold" instead of an empty team. Functions are the built-in tools that do this work inside the query itself, without changing the stored data.
Definition
A SQL function is a named, built-in operation that takes one or more input values (arguments), performs a calculation or transformation, and returns an output value. Functions can be used wherever a value is allowed — in
SELECT,WHERE,ORDER BY,GROUP BYand more.
- Named — every function has a fixed name such as
LOWER,ROUNDorYEAR, followed by brackets. - Arguments — the values you put inside the brackets. Some functions take one (
UPPER(name)), some take several (ROUND(x, 2)), and some take none (GETDATE()). - Returns a value — a function always hands back a result that you can display, compare, sort by, or pass into another function.
- Functions do not modify the table.
UPPER(name)only changes what you see in the result; the stored name stays exactly as it was.
Single-row vs multi-row functions
| Single-row (row-level) function | Multi-row (aggregate) function | |
|---|---|---|
| Input | one value from one row | values from many rows |
| Output | one value per row | one value for the whole group |
| Row count | result has the same number of rows as the input | rows are collapsed |
| Examples | LOWER, LEN, ROUND, YEAR, ISNULL, CASE | SUM, AVG, COUNT, MIN, MAX |
| IPL example | LOWER('VIRAT') → 'virat' | SUM(750, 500, 850 …) → one total |
Single-row functions run once for every row, independently of the other rows. Multi-row functions read a whole set of rows and squeeze them into a single answer.
This chapter is all about single-row functions (also called row-level functions or scalar functions). Aggregate and window functions come in the next chapter.
Syntax
sql
FUNCTION_NAME(argument1, argument2, ...)| Part | Meaning |
|---|---|
FUNCTION_NAME | the built-in function, e.g. UPPER, ROUND, DATEADD |
( ) | brackets are always required, even when there are no arguments: GETDATE() |
| arguments | a column, a literal value ('Virat', 2), an expression (runs * 100.0) or another function |
Nested functions
A nested function is a function used as the argument of another function. SQL always evaluates the innermost function first and passes its result outwards.
sql
SELECT LEN(LOWER(LEFT('VIRAT', 2))) AS result;| Step | Expression | Result |
|---|---|---|
| 1 (innermost) | LEFT('VIRAT', 2) | 'VI' |
| 2 | LOWER('VI') | 'vi' |
| 3 (outermost) | LEN('vi') | 2 |
Read nested functions from the inside out, like peeling an onion. There is no fixed limit you will realistically hit — but more than 2–3 levels gets hard to read, so format long ones over several lines.
The SQL functions family
| Family | Type | What it works with | Main functions |
|---|---|---|---|
| String functions | single-row | text | CONCAT, UPPER, LOWER, TRIM, REPLACE, LEN, LEFT, RIGHT, SUBSTRING |
| Numeric functions | single-row | numbers | ROUND, ABS (plus CEILING, FLOOR, POWER, SQRT) |
| Date & time functions | single-row | dates / times | GETDATE, YEAR, DATEPART, DATENAME, DATETRUNC, EOMONTH, DATEADD, DATEDIFF, ISDATE, FORMAT, CONVERT, CAST |
| NULL functions | single-row | missing values | ISNULL, COALESCE, NULLIF (+ IS NULL / IS NOT NULL checks) |
| Aggregate functions | multi-row | groups of rows | SUM, AVG, COUNT, MIN, MAX — next chapter |
| Window functions | multi-row (advanced) | rows + their neighbours | ROW_NUMBER, RANK, LAG … — next chapter |
The CASE expression is not a function by name, but it behaves like one (one value in, one value out per row), so it is covered in this chapter too.
Example — the nested-function steps, one column per step
sql
SELECT
LEFT('VIRAT', 2) AS step1,
LOWER(LEFT('VIRAT', 2)) AS step2,
LEN(LOWER(LEFT('VIRAT', 2))) AS result;| step1 | step2 | result |
|---|---|---|
| VI | vi | 2 |
How it works: each column adds one more layer around the previous one, so you can see the inside-out evaluation.
Common mistakes
- ❌
SELECT GETDATE;→ ✅SELECT GETDATE();— brackets are mandatory even with no arguments. - ❌ Expecting
UPDATE-like behaviour fromUPPER(name)→ ✅ functions inSELECTonly change the output, never the stored data. - ❌ Mixing up row-level and aggregate functions:
SELECT name, SUM(runs) FROM playersfails in SQL Server becauseSUMcollapses rows butnamedoes not.
Interview tip
Q: What is the difference between a scalar (single-row) function and an aggregate function?
A scalar function returns one value for each row (UPPER(name) keeps 6 rows as 6 rows). An aggregate function returns one value for a group of rows (SUM(runs) turns 6 rows into 1). Scalar functions can be used freely in WHERE; aggregate functions cannot be used in WHERE — they belong in SELECT/HAVING.
Key points to remember
- A function takes input value(s), performs an operation and returns an output value — it never changes the stored data.
- Single-row (row-level / scalar) functions return one value per row; the number of rows stays the same.
- Multi-row (aggregate) functions such as SUM and COUNT collapse many rows into one value.
- Nested functions are evaluated from the innermost to the outermost: LEN(LOWER(LEFT('VIRAT', 2))) = 2.
- Row-level functions come in four families: string, numeric, date & time, and NULL functions (plus the CASE expression).
- Brackets are always required, even for functions without arguments like GETDATE().
Try it yourself
Nested functions, step by step
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Single-row vs multi-row on the same table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: build your own nested function
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
