# ZDIFF

> Return the members of the first sorted set that are not in any of the others.

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

```
ZDIFF numkeys key [key ...] [WITHSCORES]
```

- 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

`ZDIFF` returns the members of the first sorted set that do not appear in any
of the following ones, ordered by their score in the first set. Scores are not
combined: each member keeps its score from the first set.

Inputs can also be plain sets (members score `1`), and missing keys count as
empty. To store the result instead, use [ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md).

## Options

| Option | Effect |
| --- | --- |
| `numkeys` | How many keys follow. Must be at least 1. |
| `WITHSCORES` | Return each member with its score. |

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Success | Array of members | Same |
| With `WITHSCORES` | Flat array: member, score, ... (scores as bulk strings) | Array of `[member, score]` pairs, the score a double |

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 'zdiff' command`` if
  it is below 1.
- `ERR syntax error` if fewer keys follow than `numkeys` says.
- `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> ZDIFF 2 all done WITHSCORES
1) "a"
2) "1"
3) "c"
4) "3"
```

## Compatibility

Same replies as Redis 7.4 and Valkey 8 for valid input. Extra or
unrecognised trailing arguments are ignored, where Redis and Valkey reply
`ERR syntax error`.

## See also

[ZDIFFSTORE](https://docs.hotkv.com/commands/zdiffstore/index.md), [ZINTER](https://docs.hotkv.com/commands/zinter/index.md), [ZUNION](https://docs.hotkv.com/commands/zunion/index.md),
[SDIFF](https://docs.hotkv.com/commands/sdiff/index.md)