# VEC\_DISTANCE\_COSINE

<table data-view="cards"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td align="center"><strong>WEBINAR</strong></td><td align="center">The Next Generation of MariaDB: Powered by Vector Search</td><td align="center"><a href="https://go.mariadb.com/GLBL-WBN-2025-01-30-WhatsnewinMariaDB-ES.html?utm_source=onpagepromo&#x26;utm_medium=kb&#x26;utm_campaign=webinar-platform-vector"><strong>Watch Now</strong></a></td><td><a href="/files/57TC3V6duqnJsPBeRWiq">/files/57TC3V6duqnJsPBeRWiq</a></td></tr></tbody></table>

{% hint style="info" %}
[Vectors](/docs/server/reference/sql-structure/vectors.md) are available from [MariaDB 11.7](/docs/release-notes/community-server/old-releases/11.7/what-is-mariadb-117.md).
{% endhint %}

## Syntax

```sql
VEC_DISTANCE_COSINE(v, s)
```

## Description

`VEC_Distance_Cosine` is an SQL function that calculates the [Cosine distance](https://en.wikipedia.org/wiki/Cosine_similarity#Cosine_distance) between two (not necessarily normalized) vectors.

Vectors must be of the same length. A distance between two vectors of different lengths is not defined, and `VEC_Distance_Cosine` will return `NULL` in such cases.

If the vector index was not built for the cosine function (see [CREATE TABLE with Vectors](/docs/server/reference/sql-structure/vectors/create-table-with-vectors.md)), the index is not used — a full table scan is performed instead. The [VEC\_DISTANCE](/docs/server/reference/sql-functions/vector-functions/vector-functions-vec_distance.md) function is a generic function that behaves either as [VEC\_DISTANCE\_EUCLIDEAN](/docs/server/reference/sql-functions/vector-functions/vec_distance_euclidean.md) or `VEC_DISTANCE_COSINE`, depending on the underlying index type.

## Example

```sql
SELECT VEC_DISTANCE_COSINE(vec_fromtext('[1,2,3]'), vec_fromtext('[3,5,7]'));
+-----------------------------------------------------------------------+
| VEC_DISTANCE_COSINE(vec_fromtext('[1,2,3]'), vec_fromtext('[3,5,7]')) |
+-----------------------------------------------------------------------+
|                                                   0.00258509695694209 |
+-----------------------------------------------------------------------+
```

## See Also

* [VEC\_DISTANCE](/docs/server/reference/sql-functions/vector-functions/vector-functions-vec_distance.md)
* [VEC\_DISTANCE\_EUCLIDEAN](/docs/server/reference/sql-functions/vector-functions/vec_distance_euclidean.md)
* [Vector Overview](/docs/server/reference/sql-structure/vectors/vector-overview.md)
* [CREATE TABLE with Vectors](/docs/server/reference/sql-structure/vectors/create-table-with-vectors.md)

<sub>*This page is licensed: CC BY-SA / Gnu FDL*</sub>

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-functions/vector-functions/vec_distance_cosine.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
