# ZDIFFSTORE

> Store the difference between the first sorted set and the others in a destination key.

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

```
ZDIFFSTORE destination numkeys key [key ...]
```

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

`ZDIFFSTORE` computes the same difference as [ZDIFF](https://docs.hotkv.com/commands/zdiff/index.md) and
stores it, with each member's score from the first set, 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.

## Return value

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

Errors:

- `ERR value is not an integer or out of range` if `numkeys` is not an
  integer, and ``ERR at least 1 input key is needed for 'zdiffstore' command``
  if it is below 1.
- `ERR syntax error` if the number of keys does not match `numkeys` (extra
  arguments included).
- `WRONGTYPE` if a key holds something other than a sorted set or a set.

## Examples

```
127.0.0.1:6379> ZADD all 1 "a" 2 "b" 3 "c"
(integer) 3
127.0.0.1:6379> ZADD done 9 "b"
(integer) 1
127.0.0.1:6379> ZDIFFSTORE todo 2 all done
(integer) 2
127.0.0.1:6379> ZRANGE todo 0 -1 WITHSCORES
1) "a"
2) "1"
3) "c"
4) "3"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[ZDIFF](https://docs.hotkv.com/commands/zdiff/index.md), [ZUNIONSTORE](https://docs.hotkv.com/commands/zunionstore/index.md), [ZINTERSTORE](https://docs.hotkv.com/commands/zinterstore/index.md),
[SDIFFSTORE](https://docs.hotkv.com/commands/sdiffstore/index.md)