# Count

This function can be used when counting field values and expressions.

This function can be used when counting field values and expressions.

<table>
  <thead>
    <tr>
      <th scope="col">Function</th>
      <th scope="col">Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td scope="row" data-label="Function"><a href="#count"><code>count()</code></a></td>
      <td scope="row" data-label="Description">Counts a row, or whether a given value is truthy</td>
    </tr>
  </tbody>
</table>

## `count`

The count function counts the number of times that the function is called. In a [`SELECT`](/docs/reference/query-language/statements/select.md) that aggregates with [`GROUP BY`](/docs/reference/query-language/clauses/group.md) or `GROUP ALL`, that call count is the size of each group.

_(since v3.2.5)_

When every field in the projection is a bare zero-argument `count()` (optionally aliased), SurrealDB [implies `GROUP ALL`](/docs/reference/query-language/statements/select.md#bare-count-implies-group-all). Before this change, a bare `count()` returned the constant `1` once per record.

```surql title="API DEFINITION"
count() -> 1
```
If a value is given as the first argument, then this function checks whether a given value is [truthy](/docs/reference/query-language/language-primitives/data-types/values.md#values-and-truthiness). This is useful for returning the total number of rows which match a certain condition in a [`SELECT`](/docs/reference/query-language/statements/select.md) with a `GROUP BY` or `GROUP ALL` clause.

```surql title="API DEFINITION"
count(any) -> number
```

If an array is given, this function counts the number of items in the array which are [truthy](/docs/reference/query-language/language-primitives/data-types/values.md#values-and-truthiness). If, instead, you want to count the total number of items in the given array, then use the [`array::len()`](/docs/reference/query-language/functions/database-functions/array.md#arraylen) function.

```surql title="API DEFINITION"
count(array) -> number
```
The following example shows this function, and its output, when used in a [`RETURN`](/docs/reference/query-language/statements/return.md) statement:

```surql 
RETURN count();

-- 1
```

```surql
RETURN count(true);

-- 1
```

```surql
RETURN count(10 > 15);

-- 0
```

```surql
RETURN count([ 1, 2, 3, null, 0, false, (15 > 10), rand::uuid() ]);

5
```

The following examples show this function being used in a [`SELECT`](/docs/reference/query-language/statements/select.md) statement with a `GROUP ALL` clause. From 3.2.5, a projection made only of bare `count()` [implies `GROUP ALL`](/docs/reference/query-language/statements/select.md#bare-count-implies-group-all); the examples keep the explicit form.

```surql
SELECT 
	count() 
FROM [
	{ age: 33 }, 
	{ age: 45 }, 
	{ age: 39 }
] 
GROUP ALL;
```

```surql title="Response"
[
	{ count: 3 }
]
```

```surql
SELECT 
	count(age > 35) 
FROM [
	{ age: 33 }, 
	{ age: 45 }, 
	{ age: 39 }
] 
GROUP ALL;
```

```surql title="Response"
[
	{ count: 2 }
]
```

An advanced example of the count function can be seen below:

```surql
SELECT
	country,
	count(age > 30) AS total
FROM [
	{ age: 33, country: 'GBR' },
	{ age: 45, country: 'GBR' },
	{ age: 39, country: 'USA' },
	{ age: 29, country: 'GBR' },
	{ age: 43, country: 'USA' }
]
GROUP BY country;
```

```surql title="Response"
[
	{
		country: 'GBR',
		total: 2
	},
	{
		country: 'USA',
		total: 2
	}
]
```

<br /><br />

## Using a `COUNT` index with `count()`

_(since v3.0.0)_

A `COUNT` index can be defined to speed up `count()` when used with a `GROUP ALL` clause. This allows `count()` to access a single stored value when it is called instead of iterating over the entire table. From 3.2.5, a bare `count()` projection [implies `GROUP ALL`](/docs/reference/query-language/statements/select.md#bare-count-implies-group-all); prefer the explicit form in examples and production queries.

```surql
CREATE user;
-- One record in table, very fast
SELECT count() FROM user GROUP ALL;

-- 10,000 new records,
-- count() takes a bit longer than before
CREATE |user:10000| RETURN NONE;
SELECT count() FROM user GROUP ALL;

-- Add index, wait a moment for it to build
DEFINE INDEX user_count ON user COUNT;
-- count() very performant again
SELECT count() FROM user GROUP ALL;
```
