Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Sorted Sets  ›  Lesson

ZRANGEBYSCORE and ZCOUNT

ZRANGEBYSCORE 12 minZRANGEBYSCORE returns the members whose scores fall between a minimum and a maximum, and ZCOUNT only counts them; both bounds are inclusive unless prefixed with a bracket.

ZRANGEBYSCORE and ZCOUNT

Wordcraft splits players into leagues: bronze below 100 points, silver from 100 up to 400, gold at 400 and above. A league page needs "everyone with a score in this band", and the lobby just needs "how many". That is a question about scores, not positions.

The rule

Bounds are scores, inclusive by default; a bracket makes one exclusive.

The leaderboard holds tom 90, ravi 150, asha 320 and meera 410.

127.0.0.1:6379> ZRANGEBYSCORE leaderboard 100 400
1) "ravi"
2) "asha"
127.0.0.1:6379> ZCOUNT leaderboard 100 400
(integer) 2
127.0.0.1:6379> ZRANGEBYSCORE leaderboard 400 +inf WITHSCORES
1) "meera"
2) "410"
127.0.0.1:6379> ZCOUNT leaderboard -inf (100
(integer) 1
  • ZRANGEBYSCORE key min max returns members whose score lies between min and max, lowest score first.
  • ZCOUNT key min max takes the same bounds but only replies with how many, which is cheaper when you do not need the names.
  • Both bounds are inclusive: a score exactly equal to min or max is included.
  • Put ( in front of a bound to make it exclusive: (100 means "more than 100", and as a max, "less than 100".
  • -inf and +inf mean "no lower limit" and "no upper limit".
  • LIMIT offset count pages through a big band: LIMIT 0 10 gives the first ten.

Since Redis 6.2, the same query can be written with the unified ZRANGE: ZRANGE leaderboard 100 400 BYSCORE. ZRANGEBYSCORE still works and you will meet it in plenty of existing code.

Positions (ZRANGE) and scores (ZRANGEBYSCORE) are easy to mix up: ZRANGE leaderboard 0 2 means "the first three members", while ZRANGEBYSCORE leaderboard 0 2 means "members scoring between 0 and 2", which here is nobody.

The shortcut

Draw the number line. Mark each member at its score, draw the band, use a filled dot for an inclusive end and an open dot for a ( end, then read the names inside from left to right.

The silver league, 100 up to but not including 400Step 1 of 4
  1. Place the members

    90 tom, 150 ravi, 320 asha, 410 meera.

Drills

Answer fast — trust the rule.

quick drill

What does ZCOUNT leaderboard 150 320 reply for tom 90, ravi 150, asha 320, meera 410?

quick drill

Same set. What does ZCOUNT leaderboard (150 320 reply?

quick drill

Which command lists everyone below 100 points?

quick drill

What does ZRANGEBYSCORE leaderboard 0 2 return for this leaderboard?

Remember

  • Bounds are scores, inclusive by default; a bracket makes one exclusive.
  • Draw the number line, mark the band's ends, read the names inside.
  • ZCOUNT counts the band; ZRANGE ... BYSCORE is the modern spelling of ZRANGEBYSCORE.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe silver league

Wordcraft's silver league is every player with at least 100 points but fewer than 400. Using asha 320, ravi 150, meera 410 and tom 90, write the command that counts the league and the command that lists it, give both replies, and explain the bounds.