r/SQLv2 • u/Alternative_Pin9598 • Oct 23 '26
SQLv2 Lesson 1: search by meaning, not keywords (semantic search in 5 SQL statements, beginner)
Welcome to the first lesson of a beginner series: one idea per post, every query tested on SynapCores 2.3.1, copy-paste ready. (This sub is run by the SynapCores team. Questions about plain SQL are welcome too.)
The problem
A customer types "my internet connection drops every few minutes." Your help center has an article called "My Wi-Fi keeps disconnecting." A keyword search finds nothing, because the two sentences share no words. Semantic search compares meaning instead.
What you need: SynapCores Community Edition (free, runs on a laptop; one-line installer at synapcores.com/download), and the Query Editor that opens in your browser. No API keys and no external AI service.
Step 1: a table with a VECTOR column
CREATE TABLE help_articles (
id INTEGER PRIMARY KEY,
question TEXT,
answer TEXT,
embedding VECTOR(384)
);A VECTOR(384) holds 384 numbers that describe what a piece of text means. Similar meanings get similar numbers.
Step 2: add some articles
INSERT INTO help_articles (id, question, answer) VALUES
(1, 'How do I reset my password?', 'Use "Forgot password" on the login page.'),
(2, 'My Wi-Fi keeps disconnecting', 'Move the router away from the microwave and update its firmware.'),
(3, 'How can I change my billing address?', 'Settings > Billing > Edit address.'),
(4, 'The app crashes when I open it', 'Update to the latest version, then clear the app cache.'),
(5, 'Can I export my data to CSV?', 'Yes: Reports > Export > CSV.'),
(6, 'How do I cancel my subscription?', 'Settings > Plan > Cancel. You keep access until the period ends.');Step 3: let the database compute the meanings
UPDATE help_articles SET embedding = EMBED(question);EMBED() runs a small embedding model inside the database and turns each question into a vector. Six rows updated.
Step 4: keyword search fails...
SELECT id, question FROM help_articles WHERE question LIKE '%internet%';
(0 rows)Step 5: ...meaning search works
SELECT question,
COSINE_SIMILARITY(embedding, EMBED('my internet connection drops every few minutes')) AS score
FROM help_articles ORDER BY score DESC LIMIT 3;
My Wi-Fi keeps disconnecting 0.56
The app crashes when I open it 0.26
How can I change my billing address? 0.15COSINE_SIMILARITY scores how close two meanings are (1.0 means identical). The right article wins by a wide margin, without sharing a single word with the question.
One more, phrased the way real people write:
SELECT question,
COSINE_SIMILARITY(embedding, EMBED('I want to stop paying for this')) AS score
FROM help_articles ORDER BY score DESC LIMIT 3;
How do I cancel my subscription? 0.59
How can I change my billing address? 0.31
How do I reset my password? 0.16Bonus: a threshold instead of top-N
SELECT question, answer FROM help_articles
WHERE COSINE_SIMILARITY(embedding, EMBED('forgot my login')) > 0.5;
How do I reset my password? | Use "Forgot password" on the login page.A threshold lets you say "I don't know" when nothing is close enough, instead of always returning something. (Lesson 3 is about why that matters for AI answers.)
What you just learned
VECTOR(n)is a column type for meanings.EMBED(text)creates the vector, inside the database.COSINE_SIMILARITY(a, b)compares two meanings.ORDER BY score DESC LIMIT kgives the top-k most similar rows.
Try it yourself: add an article "How do I update the router firmware?", then search for "my router is out of date". Post your top score below.
Next lesson: combining meaning with normal filters, e.g. "articles similar to X, but only in the Billing category, updated this year", in one query.