# ZRANK

> Get the rank of a member in a sorted set, lowest score first.

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

```
ZRANK key member [WITHSCORE]
```

- 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

`ZRANK` returns the position of `member` in the sorted set at `key`, ordered
from the lowest score to the highest. The lowest-scored member has rank `0`.
For the rank from the highest score down, use [ZREVRANK](https://docs.hotkv.com/commands/zrevrank/index.md).

## Options

| Option | Effect |
| --- | --- |
| `WITHSCORE` | Also return the member's score. |

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Member exists | Integer: the rank | Same |
| Member exists, with `WITHSCORE` | Two-element array: the rank (integer) and the score (bulk string) | Two-element array: the rank (integer) and the score (double) |
| Member or key does not exist | Nil bulk string | Null |
| Member or key does not exist, with `WITHSCORE` | Nil array | Null |

Errors:

- `ERR syntax error` if the third argument is not `WITHSCORE`.
- `WRONGTYPE` if `key` holds a value that is not a sorted set.

## Examples

```
127.0.0.1:6379> ZADD board 100 "alice" 90 "bob" 95 "carol"
(integer) 3
127.0.0.1:6379> ZRANK board "carol"
(integer) 1
127.0.0.1:6379> ZRANK board "alice" WITHSCORE
1) (integer) 2
2) "100"
127.0.0.1:6379> ZRANK board "nobody"
(nil)
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZREVRANK](https://docs.hotkv.com/commands/zrevrank/index.md), [ZSCORE](https://docs.hotkv.com/commands/zscore/index.md), [ZRANGE](https://docs.hotkv.com/commands/zrange/index.md)