## What are Functions in SQL

**Functions in DB2:**

- A function is an operation denoted by a function name followed by zero or more input values that are enclosed in parentheses. It represents a relationship between a set of input values and a set of result values. The input values to a function are called arguments.
- The types of functions are aggregate, scalar, and table. A built-in function is classified as a aggregate function or a scalar function. A user-defined function can be a column, scalar, or table function.

## SQL Scalar Functions

**Scalar functions:**

- A scalar function can be used wherever an expression can be used.
- A scalar function is applied to single set of parameter values rather than to sets of values.
- The argument of a scalar function can be a function. However, the restrictions that apply to the use of expressions and aggregate functions also apply when an expression or aggregate function is used within a scalar function. For example, the argument of a scalar function can be a aggregate function only if a aggregate function is allowed in the context in which the scalar function is used.
- If the argument of a scalar function is a string from a column with a field procedure, the function applies to the decoded form of the value and the result of the function does not inherit the field procedure.

## SQL Aggregate Functions

**Aggregate functions:**

- An aggregate function receives a set of values for each argument (such as the values of a column) and returns a single-value result for the set of input values. Certain rules apply to all aggregate functions.
- The result cannot be null for COUNT(*) and COUNT_BIG(*). But other aggregate functions the result can be null
- The argument of an aggregate function is a set of values derived from an expression.
**The expression must not include another aggregate function or a scalar fullselect.**The scope of the set is a group or an intermediate result table. - If a GROUP BY clause is specified in a query and the intermediate result from the FROM, WHERE, GROUP BY, and HAVING clauses is the empty set, then the aggregate functions are not applied and the result of the query is the empty set.
- If the GROUP BY clause is not specified in a query and the intermediate result table of the FROM, WHERE, and HAVING clauses is the empty set, then the aggregate functions are applied to the empty set.

*Ref:DB2*

## 2 thoughts

Comments are closed.