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

# sim

> Compute cosine similarity between vectors.

`sim` computes the cosine similarity between two vectors for each row and returns a column of `DOUBLE` scores. `similarity` is an equivalent alias.

## Signature

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
sim(left_vector, right_vector)
similarity(left_vector, right_vector)
```

| Parameter | Type | Description |
| - | - | - |
| `left_vector` | Numeric `T[N]`, such as `FLOAT[N]` | First vector. |
| `right_vector` | Numeric `T[N]`, such as `FLOAT[N]` | Second vector, with the same width as `left_vector`. |

Either vector can be a column or a constant, such as one query vector compared with every row.

## Rank rows against one query

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
WITH movies AS (
    SELECT
        movie_id,
        title,
        summary_embedding.output AS embedding -- Saved vector inside the AI envelope
    FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet')
)
SELECT
    d.title,
    sim(q.embedding, d.embedding) AS similarity
FROM movies AS q
CROSS JOIN movies AS d
WHERE q.movie_id = 53085
AND d.movie_id <> q.movie_id
ORDER BY similarity DESC
LIMIT 3;
```

## Return type

`DOUBLE`, from `-1` to `1`.

## Behavior

* A null vector, a vector with a null element, or a zero-magnitude vector returns null.
* Integer and floating-point elements are supported. `DECIMAL` elements are not.

## Related operations

* Use [`neighbors`](https://docs.chowder.ai/reference/column-functions/neighbors) to find the nearest rows within one relation.
* Use [nearest-neighbor joins](https://docs.chowder.ai/guides/nearest-neighbor-joins.md) to find the closest matches between two relations.
* Use [`clusterize`](https://docs.chowder.ai/reference/column-functions/clusterize) to cluster similar rows.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.