> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# SUNIONCARD

> Get set union cardinality.

Use `SUNIONCARD` to count the distinct members that appear in at least one of the given sets. It returns only the count; use [`SUNION`](/docs/redis/commands/set/sunion) to retrieve the members.

Members shared by multiple sets count once. Keys that do not exist count as empty sets, and the command returns `0` if all keys are missing. It does not modify the source sets or store the union.

`LIMIT` stops counting once that many distinct members have been found and returns the limit. Omitting `LIMIT` or using `LIMIT 0` returns the full count.

## Syntax

```redis theme={"system"}
SUNIONCARD <numkeys> <key> [<key> ...] [APPROX] [LIMIT <limit>]
```

## Arguments

| Argument        | Required | Repeatable | Description                                                                   |
| --------------- | -------- | ---------- | ----------------------------------------------------------------------------- |
| `<numkeys>`     | Yes      | No         | Positive integer specifying how many key arguments follow.                    |
| `<key>`         | Yes      | Yes        | Set key to include in the union.                                              |
| `APPROX`        | No       | No         | Estimate the union cardinality using HyperLogLog instead of counting exactly. |
| `LIMIT <limit>` | No       | No         | Non-negative integer that caps the returned count; `0` means no limit.        |

## Important points

* `numkeys` must equal the number of key arguments that immediately follow it.
* The count is exact unless `APPROX` is specified.
* `APPROX` uses a temporary HyperLogLog to reduce the memory needed to track distinct members. The inputs are ordinary sets, and the result may differ from the exact cardinality.
* `APPROX` and `LIMIT` can appear in either order after the keys. With both options, counting can stop when the estimate reaches the limit, and the returned value never exceeds that limit.

## Response

The number of distinct members in the union, or an estimate when `APPROX` is specified. A positive `LIMIT` caps the returned value in either mode.

| Protocol | Reply   |
| -------- | ------- |
| RESP2    | Integer |
| RESP3    | Integer |

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. Client examples query `set1` and `set2` from the Redis CLI example.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```redis theme={"system"}
    > SADD set1 a b c
    (integer) 3
    > SADD set2 c d e
    (integer) 3
    > SUNIONCARD 2 set1 set2
    (integer) 5
    > SUNIONCARD 2 set1 set2 LIMIT 3
    (integer) 3
    > SUNIONCARD 2 set1 set2 LIMIT 0
    (integer) 5
    ```

    The union contains `a`, `b`, `c`, `d`, and `e`. The shared member `c` counts once.

    To estimate the count, optionally capped at a limit:

    ```redis theme={"system"}
    SUNIONCARD 2 set1 set2 APPROX
    SUNIONCARD 2 set1 set2 APPROX LIMIT 3
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    <Note>
      This command is not supported yet in `@upstash/redis`.
    </Note>
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    <Note>
      This command is not supported yet in `upstash_redis`.
    </Note>
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.sunioncard(2, "set1", "set2");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.sUnionCard(["set1", "set2"]);
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.sunioncard(2, ["set1", "set2"])
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        defer client.Close()
        result, err := client.SUnionCard(context.Background(), nil, "set1", "set2").Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      long result = jedis.sunioncard("set1", "set2");
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let mut command = redis::cmd("SUNIONCARD");
        command.arg(2).arg("set1").arg("set2");
        let result: i64 = command.query(&mut connection)?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [Changelog](/docs/redis/overall/changelog.md)
