ZINTER(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– ZINTER β€” bestie's guide to sorted set intersection πŸ’–

okay so this is a page from the valkey command manual (manual name: valkey, section 3, iykyk) and it's literally all about smashing multiple sorted sets together and keeping only the members they ALL agree on. no cap. 🫢

✨ NAME ✨

zinter β€” returns the intersect of multiple sorted sets. that's it, that's the whole vibe. intersection girlie. 🌸


πŸŽ€ SYNOPSIS πŸŽ€

ZINTER numkeys key [key...]  [WEIGHTS weight [weight...]]  [AGGREGATE
<SUM | MIN | MAX>] [WITHSCORES]

so you tell it how many keys you're bringing to the party (numkeys), then you list the keys, and then optionally you can do the WEIGHTS thing, the AGGREGATE thing, and the WITHSCORES thing. main character energy honestly. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

This command is similar to ZINTERSTORE, but instead of storing the resulting sorted set, it is returned to the client.

bestie listen. ZINTERSTORE is the one that saves the result somewhere. ZINTER is the one that just hands it straight to you like a little gift. no storing, no commitment, just vibes. πŸ’

For a description of the WEIGHTS and AGGREGATE options, see ZUNIONSTORE.

fr fr, go read ZUNIONSTORE if you wanna know how WEIGHTS and AGGREGATE work β€” this page is not gonna re-explain them, it's giving "go ask my sister". 😭

🌈 REPLY 🌈

  • valkey-protocol(7) Array reply: the result of the intersection including, optionally, scores when the WITHSCORES option is used.
so you get an array back, and if you passed WITHSCORES you also get the scores snuck in there. iconic behaviour honestly. ✨

πŸ‘‘ COMPLEXITY πŸ‘‘

O(NK)+O(Mlog(M)) worst case with N being the smallest input sorted set, K being the number of input sorted sets and M being the number of elements in the resulting sorted set.

the way that it's O(NK)+O(Mlog(M)) in the worst case… not me memorizing that. N is the smallest input set, K is how many sets you threw in, M is how many elements survive the intersection. periodt. πŸ’€

πŸ’œ ACL CATEGORIES πŸ’œ

@read @slow @sortedset

yes bestie it's read-only, yes it's slow (sorry), yes it lives in the sorted set category. the trifecta. πŸŽ€

⭐ HISTORY ⭐

  • Available since: 6.2.0
she's been around since 6.2.0, so she's seen things. 🌸

πŸ“ EXAMPLES πŸ“

watch me cook πŸ§‘πŸ³:

127.0.0.1:6379> ZADD zset1 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset1 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset2 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 3 "three"
(integer) 1
127.0.0.1:6379> ZINTER 2 zset1 zset2
1) "one"
2) "two"
127.0.0.1:6379> ZINTER 2 zset1 zset2 WITHSCORES
1) "one"
2) "2"
3) "4"

okay wait let me walk you through it because this is actually so satisfying 😭

  • zset1 gets "one" at score 1 and "two" at score 2. cute.
  • zset2 gets "one" at 1, "two" at 2, and "three" at 3. also cute.
  • ZINTER 2 zset1 zset2 β†’ you get "one" and "two". "three" got left out because it's not in both. sorry "three", it's giving exclusion. πŸ™ˆ
  • ZINTER 2 zset1 zset2 WITHSCORES β†’ now you also get the scores, and the default aggregation (SUM) adds them together: "one" is 1+1=2 and "two" is 2+2=4. ate. πŸ”₯

🫢 SEE ALSO 🫢

go be nosy, all of these are right there:


and that's the whole page bestie!! πŸ’– this one's from valkey version 9.0.0, dated 2025-10-21, so if you're on something older… not me judging but like. upgrade. 🧚 stay intersecting, slay, drink water, bye!! πŸŒˆβœ¨πŸ’…

πŸ“¦ where this page comes from