> For the complete documentation index, see [llms.txt](https://qb.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qb.ortusbooks.com/9.0.0/query-builder/executing-queries/aggregates.md).

# Aggregates

The query builder also provides a variety of aggregate methods such as `count`, `max`, `min`, and `sum`. These methods take the headache out of setting up these common aggregate functions.

When executing any of the aggregate functions, any `where` restrictions on your query will still be applied.

Instead of returning a query, these methods return a simple value.

## exists

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns `true` if the query returns any rows.  Returns `false` otherwise.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).where( "username", "like", "jon%" ).exists();
```

{% endcode %}

{% code title="SQL (MySQL)" %}

```sql
SELECT COUNT(*) AS aggregate FROM `users` WHERE `username` LIKE 'jon%'
```

{% endcode %}

## count

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| column  | string | `false`  | `"*"`   | The column on which to count records.  |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns an integer number of rows returned by the query.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).count();
```

{% endcode %}

{% tabs %}
{% tab title="SQL (MySQL)" %}

```sql
SELECT COUNT(*) AS aggregate FROM `users`
```

{% endtab %}

{% tab title="SQL Server" %}

```sql
SELECT COUNT(*) FROM [users]
```

{% endtab %}
{% endtabs %}

## max

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| column  | string | `true`   |         | The column on which to find the max.   |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns the maximum value for the given column.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).max( "age" );
```

{% endcode %}

{% code title="SQL (MySQL)" %}

```sql
SELECT MAX(age) AS aggregate FROM `users`
```

{% endcode %}

## min

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| column  | string | `true`   |         | The column on which to find the min.   |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns the minimum value for the given column.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).min( "age" );
```

{% endcode %}

{% code title="SQL (MySQL)" %}

```sql
SELECT MIN(age) AS aggregate FROM `users`
```

{% endcode %}

## sum

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| column  | string | `true`   |         | The column to sum.                     |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns the sum of all returned rows for the given column.

{% code title="QueryBuilder" %}

```javascript
query.from( "employees" ).sum( "salary" );
```

{% endcode %}

{% code title="SQL (MySQL)" %}

```sql
SELECT SUM(salary) AS aggregate FROM `employees`
```

{% endcode %}

## sumRaw

| Name    | Type   | Required | Default | Description                            |
| ------- | ------ | -------- | ------- | -------------------------------------- |
| column  | string | `true`   |         | The column to sum.                     |
| options | struct | `false`  | `{}`    | Any additional `queryExecute` options. |

Returns the sum of all returned rows for the expression.

{% code title="QueryBuilder" %}

```javascript
query.from( "accounts" ).sumRaw( "netAdditions + netTransfers" )
```

{% endcode %}

{% code title="SQL (MySQL)" %}

```sql
SELECT SUM(netAdditions + netTransfers) AS aggregate FROM `accounts`
```

{% endcode %}

## columnList

| Name       | Type    | Required | Default | Description                                                     |
| ---------- | ------- | -------- | ------- | --------------------------------------------------------------- |
| asQuery    | boolean | `false`  | `false` | Flag to retrieve the columnList as a query instead of an array. |
| datasource | string  | `false`  |         | Optional datasource to from which to retrieve the columnList.   |

Retrieves the columns for the configured table.

{% code title="QueryBuilder" %}

```javascript
query.from( "users" ).columnList();
```

{% endcode %}

{% code title="Result" %}

```json
[ "id", "firstName", "lastName", "username", "email", "password" ]
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://qb.ortusbooks.com/9.0.0/query-builder/executing-queries/aggregates.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
