# ZUNIONSTORE

> Store the union of sorted sets in a destination key.

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

```
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX]
```

- Group: Sorted set
- Complexity: O(N)+O(M*log(M)) where N is the total size of the input sets and M is the size of the result
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZUNIONSTORE` computes the same union as [ZUNION](https://docs.hotkv.com/commands/zunion/index.md) and
stores it as a sorted set at `destination`, replacing whatever was there. If
the result is empty, `destination` is deleted. The inputs are read and the
result written as one atomic step, and `destination` can also be one of the
inputs.

Inputs can be sorted sets or plain sets (members score `1`); missing keys count
as empty.

## Options

| Option | Effect |
| --- | --- |
| `numkeys` | How many keys follow. Must be at least 1. |
| `WEIGHTS weight ...` | One multiplier per input key, applied before combining. Default `1` each. |
| `AGGREGATE SUM \| MIN \| MAX` | How scores of a shared member combine. Default `SUM`. |

`WITHSCORES` is not accepted.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Integer: the number of members in the stored sorted set | Same |

Errors are those of [ZUNION](https://docs.hotkv.com/commands/zunion/index.md), with the command named
`zunionstore` in the `numkeys` error; `WITHSCORES` gives `ERR syntax error`.

## Examples

```
127.0.0.1:6379> ZADD q1 10 "alice" 5 "bob"
(integer) 2
127.0.0.1:6379> ZADD q2 3 "bob" 7 "carol"
(integer) 2
127.0.0.1:6379> ZUNIONSTORE total 2 q1 q2
(integer) 3
127.0.0.1:6379> ZRANGE total 0 -1 WITHSCORES
1) "carol"
2) "7"
3) "bob"
4) "8"
5) "alice"
6) "10"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZUNION](https://docs.hotkv.com/commands/zunion/index.md), [ZINTERSTORE](https://docs.hotkv.com/commands/zinterstore/index.md), [ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md),
[SUNIONSTORE](https://docs.hotkv.com/commands/sunionstore/index.md)