# ZRANGESTORE

> Store a range of a sorted set, by rank, score or lexicographic order, in another key.

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

```
ZRANGESTORE dst src min max [BYSCORE | BYLEX] [REV] [LIMIT offset count]
```

- Group: Sorted set
- Complexity: O(log(N)+M) where N is the size of the source sorted set and M is the number of members stored
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZRANGESTORE` selects a range from the sorted set at `src` exactly as
[ZRANGE](https://docs.hotkv.com/commands/zrange/index.md) would, and stores it, with scores, as a sorted set
at `dst`. Whatever `dst` held before is replaced. If the range is empty, `dst`
is deleted.

`min` and `max` are ranks by default, scores with `BYSCORE`, or lexicographic
bounds with `BYLEX`. With `REV` and `BYSCORE` or `BYLEX`, give the higher bound
first.

## Options

| Option | Effect |
| --- | --- |
| `BYSCORE` | Treat `min` and `max` as scores (`(` for exclusive, `-inf` / `+inf` allowed). |
| `BYLEX` | Treat `min` and `max` as lexicographic bounds (`[`, `(`, `-`, `+`). |
| `REV` | Select from the highest to the lowest. |
| `LIMIT offset count` | Skip `offset` matching members, then take at most `count`. Only with `BYSCORE` or `BYLEX`. |

`WITHSCORES` is not accepted: scores are always stored.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of members stored in `dst` | Same |

Errors are those of [ZRANGE](https://docs.hotkv.com/commands/zrange/index.md): syntax errors for unknown or
conflicting options (including `WITHSCORES`), the `LIMIT` combination error,
bad bounds, and `WRONGTYPE` if `src` 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> ZRANGESTORE top2 board 0 1 REV
(integer) 2
127.0.0.1:6379> ZRANGE top2 0 -1 WITHSCORES
1) "c"
2) "3"
3) "d"
4) "4"
127.0.0.1:6379> ZRANGESTORE mid board (1 (4 BYSCORE
(integer) 2
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZRANGE](https://docs.hotkv.com/commands/zrange/index.md), [ZUNIONSTORE](https://docs.hotkv.com/commands/zunionstore/index.md), [ZINTERSTORE](https://docs.hotkv.com/commands/zinterstore/index.md),
[ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md)