New Relic Query Language (NRQL) supports basic math functions within a `SELECT`

clause. You can apply addition, subtraction, multiplication, and division on both individual attributes as well as the results of aggregator functions.

Want to watch videos on using NRQL? Check out the New Relic University tutorial NRQL queries with math. Or go to the full online course Writing NRQL queries.

## Use math operators with SELECT

To use basic math functions in NRQL, include operators within the `SELECT`

clause:

- Addition:
`+`

- Subtraction:
`-`

- Multiplication:
`*`

- Division:
`/`

Here are some examples:

SELECT duration-databaseDuration FROM Transaction

SELECT count(*)/uniqueCount(session) FROM PageView

SELECT average(duration-databaseDuration) FROM Transaction

## Results with STRING or FLOAT

Here is how NRQL handles strings present in math calculations:

Examples:

`sum(1+STRING)`

= 0`sum(1+MIXED)`

= skips records where MIXED is a string`average(1+STRING)`

= 0`average(1+MIXED)`

= skips records where MIXED is a string

NULL and zero both appear as 0 in the dashboard.

To override NULL values with another numeric value, use the syntax:

`SELECT average(purchasePrice OR 0)`

This will replace NULL values with 0 or any number specified.

This can also be used to test whether something returns NULL or zero. `(zero) OR 1`

returns 0. `(NULL) OR 1`

returns 1.

## Advanced math functions

NRQL includes advanced mathematical functions that can be used for complex calculations and for processing data to display more effectively in the UI.

`abs`

`abs(`

returns the absolute value of *n*)*n*. For non-negative *n* it returns *n*, and for negative *n* it returns the positive number -*n*. For example `abs(2) = 2`

, and `abs(-4) = 4`

.

`round`

, `floor`

, `ceil`

(ing)

These three functions force decimal numbers to one of the neighboring integers.

`floor(`

returns the closest integer less than or equal to*n*)*n*.`ceil(`

returns the closest integer greater than or equal to*n*)*n*.`round(`

returns the closest integer to*n*)*n*in either direction.

`clamp_max`

, `clamp_min`

The clamping functions impose an upper or lower bound on values.

For example, `clamp_max(duration, 10)`

returns the duration, unless it exceeds 10, in which case 10 is returned.

Similarly `clamp_min(duration, 1)`

will not return any value lower than 1. The following chart shows the result of clamping both min and max to keep the value in the range 70-90.

`pow`

`pow(`

computes *n*, *m*)*n* raised to the power *m*. (I.e.

with *n* * *n* * ... * *n*,*m* copies of *n*)

`sqrt`

`sqrt(`

returns the square root of *n*)*n*, that is, the number such that` sqrt(`

*n*) * sqrt(n) = *n*.

`exp`

Computes the natural exponential function of the argument: `exp(`

.*n*) = pow(*e*, *n*)

`l`*n*, log2, log10, log

*n*, log2, log10, log

These functions compute the logarithm of the argument for various bases.

`ln(`

computes the natural logarithm: the logarithm base e.*n*)`log2(`

computes the logarithm base 2.*n*)`log10(`

computes the logarithm base 10.*n*)`log(`

allows logarithms to be computed with an arbitrary base b.*n*, b)- All logarithms satisfy the identity:
`log(pow(b,`

*n*), b) =*n*.

Note that `log(0`

) is undefined, for all bases. Be aware that if you take the logarithm of something that might be zero, you may end up getting "No Value" back from your query.