# ZINTERSTORE

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

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

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

- Group: Sorted set
- Complexity: O(N*K)+O(M*log(M)) worst case, where N is the smallest input, K the number of inputs and M the size of the result
- HotKV Cloud: all plans
- On-Premise: no license needed

`ZINTERSTORE` computes the same intersection as [ZINTER](https://docs.hotkv.com/commands/zinter/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`); a missing key
makes the result 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 the scores of a 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 [ZINTER](https://docs.hotkv.com/commands/zinter/index.md), with the command named
`zinterstore` 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> ZINTERSTORE both 2 q1 q2 WEIGHTS 2 1
(integer) 1
127.0.0.1:6379> ZSCORE both "bob"
"13"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZINTER](https://docs.hotkv.com/commands/zinter/index.md), [ZUNIONSTORE](https://docs.hotkv.com/commands/zunionstore/index.md), [ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md),
[SINTERSTORE](https://docs.hotkv.com/commands/sinterstore/index.md)