---
title: "Aggregation Functions"
component: "gsql-ref"
version: "4.3"
module: "querying"
html_url: "/gsql-ref/4.3/querying/func/aggregation-functions"
canonical_url: "https://www.tigergraph.com/docs/gsql-ref/4.3/querying/func/aggregation-functions"
markdown_url: "https://www.tigergraph.com/docs/gsql-ref/4.3/querying/func/aggregation-functions.md"
---

[View as HTML](https://www.tigergraph.com/docs/gsql-ref/4.3/querying/func/aggregation-functions)

# Aggregation Functions

This page lists the aggregation functions available in the GSQL query language. These functions take a single `SET/BAG`, `SetAccum/BagAccum`, or `ListAccum` as the parameter and return an aggregated value from all members of the set or bag.

> [!NOTE]
> If the `DISTINCT` option is used, then duplicate values are excluded. By default, duplicate values are included.
> 
> Example:
> 
> `sum([2,2,2])` returns 6.
> 
> `sum(DISTINCT [2,2,2])` returns 2.

## avg()

### Syntax

`avg( [DISTINCT] setExp )`

### Description

Returns the average of all elements in a set or bag. The function can only take set/bag expressions whose members are numeric types. Returns 0 if the input set is empty.

### Return type

A numeric type. If all members of the set/bag expression are integers, the return value will also be rounded down to be an integer.

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
avg([5, 4, 1, 0, 0, 0]) -> 1
avg([3, 2, 1]) -> 2
```

## count()

### Syntax

`count( [DISTINCT] setExp )`

### Description

Returns the size of the set or bag. Returns 0 if the input set is empty.

### Return type

`INT`

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
count([1, 2, 3]) => 3
count([1, 1, 2, 2) => 4
```

## max()

### Syntax

`max( [DISTINCT] setExp )`

### Description

Returns the member with the maximum value in a set or bag. The function can only take set/bag expressions whose members are numeric types.

> [!NOTE]
> If the input set is empty:
> 
> * If the function is used to aggregate the vertex or edge attributes selected by a SELECT or MERGE clause, then the return value is the minimum possible value for that data type (similar to MaxAccum behavior).
> * Otherwise, the return value is 0.

### Return type

A numeric type.

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
max([1, 2, -3, 4]) => 4
max([1, 1, 3, 3]) => 3
```

## min()

### Syntax

`min( [DISTINCT] setExp )`

### Description

Returns the member with the minimum value in a set or bag. The function can only take set/bag expressions whose members are numeric types. Returns 0 if the input set is empty.

> [!NOTE]
> If the input set is empty:
> 
> * If the function is used to aggregate the vertex or edge attributes selected by a SELECT or MERGE clause, then the return value is the maximum possible value for that data type (similar to MinAccum behavior).
> * Otherwise, the return value is 0.

### Return type

A numeric type.

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
min([1, 2, -3, 4]) => -3
min([1, 1, 3, 3]) => 1
```

## stdev()

### Syntax

`stdev( [DISTINCT] setExp )`

### Description

Returns the standard deviation of all members in a set or bag, assuming the input is a sample. Returns 0 if the input set is empty.

This function uses `N - 1` as the denominator, and should be used when taking a sample of the population for an unbiased estimate. When the standard variation of the entire population is being calculated, `stdevp` should be used.

The function can only take set/bag expressions whose members are numeric types.

### Return type

`double`

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
stdev([1]) => 0
stdev([1, 2, 3, 4, 5]) => 1.5811388300842
```

## stdevp()

### Syntax

`stdevp( [DISTINCT] setExp )`

### Description

Returns the standard deviation of all members in a set or bag, assuming the input is the population. Returns 0 if the input set is empty.

This function uses `N` as the denominator, and should be used when calculating the standard deviation for an entire population. When the standard variation of only a sample of the population is being calculated, `stDev` should be used.

The function can only take set/bag expressions whose members are numeric types.

### Return type

`double`

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
stdevp([1]) => 0
stdevp([1, 2, 3, 4, 5]) => 1.4142135623731
```

## sum()

### Syntax

`sum( [DISTINCT] setExp )`

### Description

Returns the sum of all members in a set or bag. The function can only take set/bag expressions whose members are numeric types. Returns 0 if the input set is empty.

### Return type

A numeric type.

### Parameters

| Parameter | Description | Data type |
| --- | --- | --- |
| `setExp` | An expression that evaluates to a `SET`, `BAG`, `SetAccum` or `BagAccum` | `SET`, `BAG`, `SetAccum`, `BagAccum` |

### Example

```text
sum([1, 2, -3, 4]) => 4
sum([1, 1, 3, 3]) => 8
```
