> ## 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.

# Analyze embeddings with SQL

> Create embeddings, compare text and images, and explore movie relationships with neighbors and clusters.

Embeddings let you compare and group rows by their content. Chowder exposes these operations as SQL analytics. The demo bucket includes 42,306 movie summaries with a precomputed `summary_embedding` AI envelope, so you can start without generating vectors.

## Find related movies for every row

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
SELECT
    movie_id,
    title,
    neighbors(
        summary_embedding.output, -- Reuse the saved vector embedding
        3, -- Three neighbors per movie
        (title, release_year) -- Fields to include with each neighbor
    ) AS similar_movies
FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet');
```

For the recorded Toy Story row, the nearest movies include Toy Story 2, Toy Story 3, and Small Fry. The projection keeps the nested result small; each neighbor also carries its similarity.

To see one movie's neighbors, compute them in a CTE and filter afterward, as in [Flatten neighbors](https://docs.chowder.ai/reference/column-functions/neighbors.md#flatten-neighbors). A `WHERE` filter or a `LIMIT` in a subquery changes which movies are compared. For one movie against the full catalog, use a [nearest-neighbor join](https://docs.chowder.ai/guides/nearest-neighbor-joins.md).

## Group similar plots and count the groups

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
WITH labeled AS (
    SELECT
        title,
        clusterize(summary_embedding.output, 0.95, 100) AS cluster -- Similarity threshold 0.95; 100 candidate neighbors
    FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet')
)
SELECT
    cluster,
    COUNT(*) AS movie_count, -- Count movies, including remakes with the same title
    array_agg(DISTINCT title) AS titles
FROM labeled
GROUP BY cluster
HAVING COUNT(*) > 1
ORDER BY movie_count DESC, cluster
LIMIT 5;
```

At this high threshold, one recorded group contains versions of A Christmas Carol. Other groups include remakes with the same title. This is similarity of plot summaries, not a genre classifier. Cluster IDs and approximate-neighbor results can change between executions.

`clusterize` connects rows through a thresholded nearest-neighbor graph. Lower thresholds may merge groups through chains of intermediate rows. `COUNT(*)` counts movies even when titles repeat; `array_agg(DISTINCT title)` lists distinct titles.

## Compare one pair of populations

```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;
```

This compares Toy Story to the same catalog and excludes the source movie by ID. Both vectors come from the same saved embedding space. Before comparing the saved movie vectors with new `embed` vectors, check that their `model_id` values match.

## Create text embeddings

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
SELECT
    title,
    embed(content) AS content_embedding -- AI envelope; vector in .output
FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/amazon-reviews/*.parquet')
LIMIT 3;
```

Keep the full envelope to retain `model`, `model_id`, and `error`; access `content_embedding.output` for the `FLOAT[N]` vector.

## Compare product images with a description

The default embedding model supports text and images in the same vector space. Start with a bounded set of 24 cup photos:

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
WITH products AS (
    SELECT
        source,
        image
    FROM read_images('hf://buckets/chowderai/chowder-demo/image/data/amazon-products/drinking-cup/*.jpg')
    LIMIT 24 -- Bound the input before computing image embeddings
)
SELECT
    source,
    sim(
        embed(image).output, -- Embed images and text in the same vector space
        embed('a stack of disposable paper cups').output
    ) AS similarity
FROM products
ORDER BY similarity DESC
LIMIT 3;
```

The result ranks that selected set, not the entire product catalog. `ORDER BY` requires embedding every selected image even though the final `LIMIT` returns only three. Increase the input limit deliberately when expanding the analysis.

## Inspect the result

```python theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
import chowder

SQL = """
SELECT
    movie_id,
    title,
    neighbors(
        summary_embedding.output, -- Reuse the saved vector embedding
        3, -- Three neighbors per movie
        (title, release_year) -- Fields to include with each neighbor
    ) AS similar_movies
FROM read_parquet('hf://buckets/chowderai/chowder-demo/parquet/data/cmu-movie-summaries-embeddings/*.parquet');
"""

relation = chowder.sql(SQL)
result = relation.to_polars()

chowder.display(result)
```

Replace `SQL` with any of the complete queries above. Materialize the relation with `to_polars()`, then pass the DataFrame to `chowder.display(result)` to inspect nested neighbor values or image columns.

Null inputs preserve row count. `neighbors` and `clusterize` require `FLOAT[N]` embeddings and need the complete selected input population. Use [remote compute](https://docs.chowder.ai/guides/remote-compute.md) when you want model work to run on a configured server.

## Generate embeddings on Chowder Cloud

The saved movie vectors let you explore analytical operators without an embedding call. To create embeddings for your own text or images on managed GPU compute, follow the [Cloud quickstart](https://docs.chowder.ai/cloud/quickstart.md). Keep model identity with saved vectors and inspect usage on a bounded input batch first.


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