Vector Search · AI Embeddings · pgvector · RAG · Guide + 25 Interview Questions

Vector Search & AI Embeddings in SQL: Semantic Search with pgvector

Vector search finds similar items using high-dimensional embeddings, enabling semantic search, recommendations, and RAG (Retrieval-Augmented Generation). pgvector brings this capability directly into PostgreSQL, combining vector search with the full power of SQL. This guide covers embeddings, similarity search, cosine similarity, pgvector, RAG, vector databases, and the 25 interview questions that actually come up — with deep, descriptive theory aligned to the topic.

🎯 Key Takeaways
25 Interview Questions Beginner to Advanced pgvector, RAG, Vector Databases Updated Dec 3, 2026
Kailas Warade ✓ Verified Author
Data Engineer · Oracle Certified · SQL & PL/SQL
Kailas works with SQL, PL/SQL, Python, and PySpark to build data pipelines. He writes the SQL interview guides at Sankalan Data Tech, focused on practical, real-world answers.
🏆 Oracle Certified 💼 Data Engineer 📝 SQL & PL/SQL Guides 📍 Pune, India
📤 Share:

On This Page

What Vector Search Really Is

Vector search finds similar items using high-dimensional embeddings, enabling semantic search, recommendations, and RAG.

-- Vector search flow
Query → Embedding → Similarity Search → Results

The core insight is that traditional keyword search matches exact terms. Vector search understands meaning, finding semantically similar items even if they use different words.

Vector search is the foundation of modern AI applications — semantic search, recommendations, RAG, and more.

pgvector brings this capability directly into PostgreSQL, combining vector search with the full power of SQL.

Embeddings in Depth

An embedding is a numerical representation of data (text, image, etc.) in a high-dimensional vector space.

-- Text embedding
"Hello world" → [0.1, 0.2, 0.3, ..., 0.9]

-- Similar texts have similar embeddings
"Hello world" ≈ "Hi there"

Embeddings capture semantic meaning. Similar items have similar embeddings, enabling similarity search.

Embeddings are generated by models like OpenAI's text-embedding-ada-002, Sentence Transformers, or Cohere.

Embedding dimension (like 1536) determines the vector size and affects storage and search performance.

Similarity Search in Depth

Similarity search finds the most similar items to a query vector using distance metrics.

-- Similarity metrics
Cosine similarity: angle between vectors
Euclidean distance: straight-line distance
Dot product: magnitude × cosine

Cosine similarity is most common for text because it measures direction (semantic meaning), not magnitude.

Cosine similarity ranges from -1 to 1, where 1 means identical direction.

Euclidean distance is better for spatial data where magnitude matters.

pgvector in Depth

pgvector is a PostgreSQL extension that adds vector data types and similarity search operators.

-- Install pgvector
CREATE EXTENSION vector;

-- Create a vector column
CREATE TABLE items (
    id INT PRIMARY KEY,
    embedding vector(1536)
);

-- Query with cosine distance
SELECT * FROM items
ORDER BY embedding <=> query_vector
LIMIT 5;

pgvector enables vector search directly in PostgreSQL, combining it with full SQL power.

pgvector supports cosine distance (<=>), L2 distance (<->), and inner product (<#>).

pgvector is ideal for applications that already use PostgreSQL.

ANN Search in Depth

ANN (Approximate Nearest Neighbor) search finds approximate nearest neighbors quickly, trading exactness for speed.

-- ANN algorithms
HNSW: graph-based, fast and accurate
IVF: cluster-based, balanced
LSH: hash-based, fast but less accurate

ANN is essential for large-scale vector search where exact search is too slow.

ANN trades a small amount of accuracy for massive speed gains.

HNSW is the most popular ANN algorithm in modern vector databases.

RAG in Depth

RAG (Retrieval-Augmented Generation) combines vector search with LLMs, retrieving relevant documents to ground LLM responses.

-- RAG flow
Query → Embedding → Vector Search → Relevant Docs → LLM → Response

RAG solves the "hallucination" problem by grounding LLM responses in real documents.

RAG is the most common pattern for building AI applications with enterprise data.

RAG requires a vector database (like pgvector) for the retrieval step.

25 Vector Search & AI Embeddings Interview Questions and Answers

These are the vector search and AI embeddings questions that come up most often in data engineering and AI interviews. Every answer stands on its own — read them in any order, or work through them in sequence to build a complete understanding of vector search, embeddings, pgvector, similarity search, ANN, and RAG.

🔍

No matching questions found

Try a keyword like pgvector, cosine, RAG, or embedding.

An embedding is a numerical representation of data (text, image, etc.) in a high-dimensional vector space.

-- Text embedding
"Hello world" → [0.1, 0.2, 0.3, ..., 0.9]

Embeddings capture semantic meaning. Similar items have similar embeddings.

Embeddings are generated by models like OpenAI's text-embedding-ada-002.

Vector search is the process of finding the most similar items to a query vector.

Query → Embedding → Similarity Search → Results

Vector search understands meaning, finding semantically similar items.

It is the foundation of semantic search, recommendations, and RAG.

pgvector is a PostgreSQL extension that adds vector data types and similarity search operators.

CREATE EXTENSION vector;

CREATE TABLE items (
    id INT PRIMARY KEY,
    embedding vector(1536)
);

pgvector enables vector search directly in PostgreSQL.

It supports cosine distance, L2 distance, and inner product.

Cosine similarity measures the angle between two vectors, ranging from -1 to 1.

-- Cosine similarity
cos(θ) = (A · B) / (|A| × |B|)

Cosine similarity measures direction (semantic meaning), not magnitude.

It is most common for text similarity.

RAG combines vector search with LLMs, retrieving relevant documents to ground LLM responses.

Query → Embedding → Vector Search → Relevant Docs → LLM → Response

RAG solves the "hallucination" problem by grounding LLM responses in real documents.

RAG is the most common pattern for AI applications with enterprise data.

A vector database is a database optimized for storing and querying high-dimensional vectors.

-- Vector databases
Pinecone, Weaviate, pgvector, Milvus

Vector databases are optimized for similarity search.

They use ANN algorithms for fast search at scale.

Cosine similarity measures angle (direction). Euclidean distance measures magnitude (straight-line distance).

Aspect Cosine Similarity Euclidean Distance
Measures Angle Straight-line distance
Range -1 to 1 0 to infinity
Best for Text Spatial data

Cosine is better for text; Euclidean for spatial data.

A vector database is optimized for high-dimensional vector search. A relational database is optimized for structured, tabular data.

-- Vector database: vector search
-- Relational database: structured data

Vector databases use ANN algorithms. Relational databases use indexes and joins.

ANN search finds approximate nearest neighbors quickly, trading exactness for speed.

-- ANN: approximate but fast
-- Exact: accurate but slow

ANN is essential for large-scale vector search.

HNSW is the most popular ANN algorithm.

HNSW (Hierarchical Navigable Small World) is a graph-based ANN algorithm.

-- HNSW: graph-based, fast and accurate
-- Used in most modern vector databases

HNSW is the most popular ANN algorithm in modern vector databases.

It balances speed and accuracy well.

IVF (Inverted File) is an ANN algorithm that clusters vectors and searches only relevant clusters.

-- IVF: cluster-based, balanced
-- Clusters vectors, searches relevant clusters

IVF balances speed and accuracy.

It is simpler than HNSW but less accurate.

pgvector is a PostgreSQL extension for vector search. Pinecone is a dedicated, managed vector database service.

-- pgvector: PostgreSQL extension
-- Pinecone: managed vector database service

pgvector is ideal for existing PostgreSQL users. Pinecone is ideal for dedicated vector workloads.

Use the vector type from pgvector.

CREATE TABLE items (
    id INT PRIMARY KEY,
    embedding vector(1536)
);

The vector type specifies the dimension (1536 in this example).

Dimension must match the embedding model's output.

Use the <=> operator for cosine distance.

SELECT * FROM items
ORDER BY embedding <=> query_vector
LIMIT 5;

<=> is cosine distance. <-> is L2 distance. <#> is inner product.

ORDER BY + LIMIT finds the top-K similar items.

Semantic search understands meaning using embeddings. Keyword search matches exact terms.

-- Semantic: understands meaning
-- Keyword: matches exact terms

Semantic search is better for natural language queries.

Keyword search is better for exact matching.

Use HNSW or IVFFlat indexes in pgvector.

-- HNSW index
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);

-- IVFFlat index
CREATE INDEX ON items USING ivfflat (embedding vector_cosine_ops);

Indexes enable fast ANN search.

HNSW is generally better for accuracy; IVFFlat for speed.

HNSW is graph-based and more accurate. IVFFlat is cluster-based and faster.

-- HNSW: graph-based, more accurate
-- IVFFlat: cluster-based, faster

HNSW is generally better for accuracy. IVFFlat for speed.

An embedding is a dense, learned representation. A one-hot encoding is a sparse, fixed representation.

-- Embedding: dense, learned
[0.1, 0.2, 0.3, ...]

-- One-hot: sparse, fixed
[0, 0, 1, 0, ...]

Embeddings capture semantic meaning. One-hot encodings do not.

An embedding is a learned representation from a model. A feature vector is a hand-crafted representation.

-- Embedding: learned from model
-- Feature vector: hand-crafted

Embeddings are more powerful because they capture complex patterns.

An embedding is a vector representation of text. A token is a unit of text (word or sub-word).

-- Token: "Hello" → token
-- Embedding: "Hello" → [0.1, 0.2, ...]

Tokens are input to embedding models. Embeddings are output.

An embedding preserves similarity. A hash does not.

-- Embedding: similar texts → similar vectors
-- Hash: no similarity guarantee

Embeddings enable similarity search. Hashes do not.

An embedding is a semantic representation. A fingerprint is a compact, similarity-preserving representation.

-- Embedding: semantic, high-dimensional
-- Fingerprint: compact, similarity-preserving

Fingerprints are a type of embedding optimized for compactness.

An embedding is a specific type of latent representation, usually from a pretrained model.

-- Embedding: from pretrained model
-- Latent representation: from any model

Embeddings are a subset of latent representations.

An embedding is a vector with semantic meaning. A vector is any array of numbers.

-- Vector: any array of numbers
-- Embedding: vector with semantic meaning

Embeddings are a specific type of vector.

Well-designed vector search follows a few key principles.

  • Use the right embedding model — match the model to the task.
  • Use the right distance metric — cosine for text, Euclidean for spatial.
  • Use ANN indexes — for fast search at scale.
  • Use pgvector for PostgreSQL — combine vector search with SQL.
  • Use RAG for LLM applications — ground responses in real data.
  • Monitor search quality — measure relevance and latency.
-- Well-designed vector search
CREATE TABLE items (
    id INT PRIMARY KEY,
    embedding vector(1536)
);

CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);

SELECT * FROM items
ORDER BY embedding <=> query_vector
LIMIT 5;

These principles produce a vector search system that is accurate, fast, and scalable.

Mistakes I Keep Seeing in Interviews

These come up repeatedly. None of them are exotic, and every one of them is easy to avoid once you know it is coming.

Mistake one: confusing embeddings with one-hot encodings. Embeddings are dense and learned; one-hot is sparse and fixed.
Mistake two: confusing cosine similarity with Euclidean distance. Know the difference: angle vs magnitude.
Mistake three: not understanding ANN. Exact search is too slow at scale; ANN is essential.
Mistake four: confusing pgvector with Pinecone. pgvector is a PostgreSQL extension; Pinecone is a managed service.
Mistake five: not understanding RAG. RAG is the most common pattern for AI applications with enterprise data.
Mistake six: not indexing vectors. Without indexes, vector search is slow at scale.

Follow-Up Questions Interviewers Ask

Getting the first answer right is only half the interview. What usually decides the outcome is what happens next. Here is what to prepare for on the vector search topic.

After “What is an embedding?” — expect

  • “How are embeddings generated?”
  • “What is the dimension?”
  • “What is the difference from one-hot?”
  • “What models generate embeddings?”

After “What is vector search?” — expect

  • “What is cosine similarity?”
  • “What is Euclidean distance?”
  • “What is ANN?”
  • “What is HNSW?”

After “pgvector” — expect

  • “How do you create a vector column?”
  • “How do you query vectors?”
  • “How do you index vectors?”
  • “What is the difference from Pinecone?”

After “RAG” — expect

  • “What is the RAG flow?”
  • “How does RAG solve hallucination?”
  • “What is needed for RAG?”
  • “What are the challenges?”

For every question above, prepare a second-level answer. The candidates who pass are not the ones who memorized the first answer — they are the ones who kept going for another thirty seconds and said something useful about why it matters.

Quick Quiz

Five questions to check whether the topics above have landed.

1. What is an embedding?
2. Which distance metric is most common for text similarity?
3. What is pgvector?
4. What does RAG stand for?
5. What is the main advantage of ANN search?