# ZCOUNT

> Count the members of a sorted set with scores in a range.

URL: https://docs.hotkv.com/commands/zcount/

```
ZCOUNT key min max
```

- Group: Sorted set
- Complexity: O(log(N)) where N is the size of the sorted set
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZCOUNT` returns how many members of the sorted set at `key` have a score
between `min` and `max`.

Both bounds are inclusive by default. Prefix a bound with `(` to make it
exclusive, and use `-inf` and `+inf` for an open end. For example,
`ZCOUNT key (1 5` counts scores greater than 1 and at most 5.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of members in the range | Same |
| Key does not exist | Integer `0` | Same |

Errors:

- `ERR min or max is not a float` if a bound is not a number. The bounds are
  checked before the key is looked up.
- `WRONGTYPE` if `key` holds a value that is not a sorted set.

## Examples

```
127.0.0.1:6379> ZADD board 1 "a" 2 "b" 3 "c" 4 "d"
(integer) 4
127.0.0.1:6379> ZCOUNT board 2 3
(integer) 2
127.0.0.1:6379> ZCOUNT board (2 +inf
(integer) 2
127.0.0.1:6379> ZCOUNT board -inf +inf
(integer) 4
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZRANGEBYSCORE](https://docs.hotkv.com/commands/zrangebyscore/index.md), [ZLEXCOUNT](https://docs.hotkv.com/commands/zlexcount/index.md),
[ZCARD](https://docs.hotkv.com/commands/zcard/index.md), [ZREMRANGEBYSCORE](https://docs.hotkv.com/commands/zremrangebyscore/index.md)