---
title: vector_cosine_similarity function
description: Learn the syntax of the vector\_cosine\_similarity function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_cosine_similarity
---

# `vector_cosine_similarity` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18.1 and above

Computes the cosine similarity between two vectors, measuring the cosine of the angle between them.

## Syntax

```
vector_cosine_similarity(vector1, vector2)
```

## Arguments

* **vector1**: An `ARRAY<FLOAT>` expression representing the first vector.
* **vector2**: An `ARRAY<FLOAT>` expression representing the second vector.

## Returns

A `FLOAT` value representing the cosine similarity between the two vectors. The result ranges from -1.0 (opposite directions) to 1.0 (same direction), where 0.0 indicates orthogonality.

Returns `NULL` for empty vectors, if either vector has zero magnitude, or if either input is `NULL` or contains `NULL`.

## Notes

* Only `ARRAY<FLOAT>` is supported; other types such as `ARRAY<DOUBLE>` or `ARRAY<DECIMAL>` raise an error.
* Both vectors must have the same dimension; otherwise the function raises [VECTOR\_DIMENSION\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#vector_dimension_mismatch).
* Higher values indicate greater similarity; commonly used for semantic similarity in embedding spaces.

## Error conditions

* [VECTOR\_DIMENSION\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#vector_dimension_mismatch)

## Examples

```sql
-- Basic cosine similarity
> SELECT vector_cosine_similarity(array(1.0f, 2.0f, 3.0f), array(4.0f, 5.0f, 6.0f));
  0.9746318461970762

-- Identical vectors (maximum similarity)
> SELECT vector_cosine_similarity(array(1.0f, 0.0f, 0.0f), array(1.0f, 0.0f, 0.0f));
  1.0

-- Orthogonal vectors
> SELECT vector_cosine_similarity(array(1.0f, 0.0f), array(0.0f, 1.0f));
  0.0
```

## Related functions

* [`vector_inner_product` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_inner_product.md)
* [`vector_l2_distance` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_l2_distance.md)
* [`vector_normalize` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_normalize.md)
* [`vector_norm` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/vector_norm.md)
