---
schema_version: "1"
id: bb5eee0c7871846211759fe7264cd108107e6dfb9319b8a3ac0e3d41277c59aa
title: COSINE_SIMILARITY
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/data-cloud/data-cloud-query-guide/references/data-cloud-sql-reference/scalar-func/array-func/cosine-similarity.md
canonical_url: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/cosine-similarity.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: f7e731fd45741e21baa16a352bf2c6a605da84c6ddec1e9ff25bf8a960b9e45e
products:
  - Data 360 Query Guide
  - Data 360 Developer Documentation
---
# `COSINE_SIMILARITY`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

Computes the cosine similarity between two vectors, measuring directional alignment.

## Syntax

```sql
cosine_similarity(<array1>, <array2>)
```

## Arguments

### Required

- `<array1>`: The first vector (`array(real)` or `array(double-precision)`).
- `<array2>`: The second vector (`array(real)` or `array(double-precision)`).

## Returns

Returns a double-precision value between -1 and 1, where 1 indicates identical direction, 0 indicates orthogonal, and -1 indicates opposite direction.

## Considerations

- Both input arrays must be of the same element type and length.
- If any element is NULL, the result is NULL.
- Zero vectors cause division by zero.
- For optimal performance, use array definitions with non-nullable elements.

## Examples

### Calculate Cosine Similarity

Compute the cosine similarity between two vectors.

```sql
SELECT cosine_similarity(array[1, 2, 3], array[-1, 2, -3]) AS result;
```

Returns approximately `-0.42857`, indicating the vectors are somewhat opposed in direction.

## Related Documentation

- [Array Data Type](https://developer.salesforce.com/docs/data/data-cloud-query-guide/datatype/array.md)
