{
  "schema_version": 2,
  "id": "commands/zrangebylex",
  "title": "ZRANGEBYLEX",
  "url": "https://redis.io/docs/latest/commands/zrangebylex/",
  "summary": "Returns members in a sorted set within a lexicographical range.",
  "since": "2.8.9",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-07-24T11:07:00+02:00",
  "page_type": "content",
  "content_hash": "3ff55bb9a801cf2c9d6f0fa2ffffa9a93afcab170aaf17bd7e2cbf4f7d6faa88",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns all the elements in the sorted set at `key` with a value between `min` and `max`.\n\nIf the elements in the sorted set have different scores, the returned elements are unspecified.\n\nThe elements are considered to be ordered from lower to higher strings as compared byte-by-byte using the `memcmp()` C function. Longer strings are considered greater than shorter strings if the common part is identical.\n\nThe optional `LIMIT` argument can be used to only get a range of the matching\nelements (similar to _SELECT LIMIT offset, count_ in SQL). A negative `count`\nreturns all elements from the `offset`.\nKeep in mind that if `offset` is large, the sorted set needs to be traversed for\n`offset` elements before getting to the elements to return, which can add up to\nO(N) time complexity."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the sorted set.\n\n</details>\n\n<details open><summary><code>min</code></summary>\n\nThe minimum member, compared lexicographically. Prefix with `[` for an inclusive bound or `(` for an exclusive bound; use `-` for the lowest possible value.\n\n</details>\n\n<details open><summary><code>max</code></summary>\n\nThe maximum member, compared lexicographically. Prefix with `[` for an inclusive bound or `(` for an exclusive bound; use `+` for the highest possible value.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "<details open><summary><code>LIMIT offset count</code></summary>\n\nSkip `offset` matching members and return up to `count` of them. A negative `count` returns all remaining members.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g\n(integer) 7\nredis> ZRANGEBYLEX myzset - [c\n1) \"a\"\n2) \"b\"\n3) \"c\"\nredis> ZRANGEBYLEX myzset - (c\n1) \"a\"\n2) \"b\"\nredis> ZRANGEBYLEX myzset [aaa (g\n1) \"b\"\n2) \"c\"\n3) \"d\"\n4) \"e\"\n5) \"f\""
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": ""
    },
    {
      "id": "how-to-specify-intervals",
      "title": "How to specify intervals",
      "role": "content",
      "text": "Valid *start* and *stop* must start with `(` or `[`, in order to specify\nif the range item is respectively exclusive or inclusive.\nThe special values of `+` or `-` for *start* and *stop* have the special\nmeaning or positively infinite and negatively infinite strings, so for\ninstance the command **ZRANGEBYLEX myzset - +** is guaranteed to return\nall the elements in the sorted set, if all the elements have the same\nscore."
    },
    {
      "id": "details-on-strings-comparison",
      "title": "Details on strings comparison",
      "role": "content",
      "text": "Strings are compared as binary array of bytes. Because of how the ASCII character\nset is specified, this means that usually this also have the effect of comparing\nnormal ASCII characters in an obvious dictionary way. However this is not true\nif non plain ASCII strings are used (for example utf8 strings).\n\nHowever the user can apply a transformation to the encoded string so that\nthe first part of the element inserted in the sorted set will compare as the\nuser requires for the specific application. For example if I want to\nadd strings that will be compared in a case-insensitive way, but I still\nwant to retrieve the real case when querying, I can add strings in the\nfollowing way:\n\n    ZADD autocomplete 0 foo:Foo 0 bar:BAR 0 zap:zap\n\nBecause of the first *normalized* part in every element (before the colon character), we are forcing a given comparison, however after the range is queries using `ZRANGEBYLEX` the application can display to the user the second part of the string, after the colon.\n\nThe binary nature of the comparison allows to use sorted sets as a general\npurpose index, for example the first part of the element can be a 64 bit\nbig endian number: since big endian numbers have the most significant bytes\nin the initial positions, the binary comparison will match the numerical\ncomparison of the numbers. This can be used in order to implement range\nqueries on 64 bit values. As in the example below, after the first 8 bytes\nwe can store the value of the element we are actually indexing."
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | Deprecated as of Redis v6.2.0. |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\n[Array reply](../../develop/reference/protocol-spec#arrays): a list of elements in the specified score range.\n\n**RESP3:**\n\n[Array reply](../../develop/reference/protocol-spec#arrays): a list of elements in the specified score range."
    }
  ],
  "examples": []
}
