---
title: UniqTheta Functions reference
meta:
    description: Functions for working with uniqThetaSketch data.
headingMaxLevels: 2
---

# uniqTheta functions

UniqTheta functions perform set operations like union, intersection, and difference on two `uniqThetaSketch` objects. These functions return a new `uniqThetaSketch` object representing the result of the operation.

A `uniqThetaSketch` object is an approximate data structure used to estimate the number of unique elements in a set. It is typically created using the `uniqThetaState` aggregate function.

## uniqThetaUnion

Combines two `uniqThetaSketch` objects to produce a new sketch representing the approximate union of their unique elements.

### Syntax

```sql
uniqThetaUnion(uniqThetaSketch, uniqThetaSketch)
```

### Arguments

- `uniqThetaSketch`: `uniqThetaSketch`. The first input sketch.
- `uniqThetaSketch`: `uniqThetaSketch`. The second input sketch.

### Returns

A new `uniqThetaSketch` object containing the approximate union of the input sketches. `uniqThetaSketch`.

### Example

```sql
SELECT
    finalizeAggregation(uniqThetaUnion(a, b)) AS a_union_b,
    finalizeAggregation(a) AS a_cardinality,
    finalizeAggregation(b) AS b_cardinality
FROM
(
    SELECT
        arrayReduce('uniqThetaState', [1, 2]) AS a,
        arrayReduce('uniqThetaState', [2, 3, 4]) AS b
)
```

Result:

```result
┌─a_union_b─┬─a_cardinality─┬─b_cardinality─┐
│         4 │             2 │             3 │
└───────────┴───────────────┴───────────────┘
```

## uniqThetaIntersect

Calculates the approximate intersection of unique elements present in two `uniqThetaSketch` objects, returning a new sketch.

### Syntax

```sql
uniqThetaIntersect(uniqThetaSketch, uniqThetaSketch)
```

### Arguments

- `uniqThetaSketch`: `uniqThetaSketch`. The first input sketch.
- `uniqThetaSketch`: `uniqThetaSketch`. The second input sketch.

### Returns

A new `uniqThetaSketch` object containing the approximate intersection of the input sketches. `uniqThetaSketch`.

### Example

```sql
SELECT
    finalizeAggregation(uniqThetaIntersect(a, b)) AS a_intersect_b,
    finalizeAggregation(a) AS a_cardinality,
    finalizeAggregation(b) AS b_cardinality
FROM
(
    SELECT
        arrayReduce('uniqThetaState', [1, 2]) AS a,
        arrayReduce('uniqThetaState', [2, 3, 4]) AS b
)
```

Result:

```result
┌─a_intersect_b─┬─a_cardinality─┬─b_cardinality─┐
│             1 │             2 │             3 │
└───────────────┴───────────────┴───────────────┘
```

## uniqThetaNot

Determines the approximate set difference (elements in the first sketch but not in the second) between two `uniqThetaSketch` objects, yielding a new sketch.

### Syntax

```sql
uniqThetaNot(uniqThetaSketch, uniqThetaSketch)
```

### Arguments

- `uniqThetaSketch`: `uniqThetaSketch`. The first input sketch (minuend).
- `uniqThetaSketch`: `uniqThetaSketch`. The second input sketch (subtrahend).

### Returns

A new `uniqThetaSketch` object containing the approximate difference of the input sketches. `uniqThetaSketch`.

### Example

```sql
SELECT
    finalizeAggregation(uniqThetaNot(a, b)) AS a_not_b,
    finalizeAggregation(a) AS a_cardinality,
    finalizeAggregation(b) AS b_cardinality
FROM
(
    SELECT
        arrayReduce('uniqThetaState', [2, 3, 4]) AS a,
        arrayReduce('uniqThetaState', [1, 2]) AS b
)
```

Result:

```result
┌─a_not_b─┬─a_cardinality─┬─b_cardinality─┐
│       2 │             3 │             2 │
└─────────┴───────────────┴───────────────┘
```
