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
Embeddings represent data as high-dimensional vectors
Vector search finds similar items using distance metrics
pgvector brings vector search to PostgreSQL
Cosine similarity measures angle between vectors
RAG combines vector search with LLMs
ANN enables fast, approximate search at scale
25 Interview QuestionsBeginner to Advancedpgvector, RAG, Vector DatabasesUpdated Dec 3, 2026
KW
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
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.
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.
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.
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?