Course contentsShow
← Databases & SQL
Lesson 152 of 1,9532. Querying and Data RetrievalFree lesson

Vector Similarity Search

Perform semantic-based similarity filtering using embedded vector representations.

In this lesson, you will learn how to go beyond simple text matching by using numerical vectors to find data that is "semantically" similar rather than just linguistically identical.

Moving Beyond Exact Keywords

Standard SQL filtering often relies on exact matches (WHERE name = 'Apple') or fuzzy pattern matching (LIKE '%appl%'). However, these methods fail to capture meaning. If a user searches for "mobile phone," a standard query might miss rows labeled "smartphone" because the letters don't overlap.

Vector Similarity Search solves this by converting data (like text, images, or audio) into an embedding: a long list of numbers (a vector) that represents the data's core meaning in a multi-dimensional space. In this space, things that "mean" the same thing are placed close together.

Querying with Vectors

To perform a similarity search, your database treats these vectors as geometric points. The "closeness" of two items is calculated using math, most commonly Cosine Similarity (measuring the angle between two vectors) or Euclidean Distance (measuring the straight-line distance).

Imagine you have a table products with a column embedding containing these vectors. To find items similar to a user's input, you perform a calculation against every row and return the closest ones:

SELECT product_name
FROM products
ORDER BY embedding <=> '[0.12, -0.05, 0.88, ...]' 
LIMIT 5;

In this syntax, the <=> operator represents a distance function. The query calculates how far the product's vector is from the user's input vector and sorts them from closest to farthest. Even though "smartphone" and "mobile phone" look nothing alike in text, their mathematical vectors will have a very small distance between them, allowing the database to return the correct result.

This technique is the foundation of modern recommendation engines and AI-powered search, as it allows the database to understand context, intent, and synonyms automatically based on the geometric proximity of the data points.

Key Takeaway: Vector similarity search replaces rigid keyword filtering with geometric math, allowing you to retrieve data based on its underlying meaning and semantic relationships.