r/SQLv2 • u/Alternative_Pin9598 • 17d ago
SQLv2 Lesson 5: "who can help me, and who introduces us?" Graph hops + meaning search in one database (beginner)
Lesson 5 of the beginner series, every query run on SynapCores 2.3.2 this morning with the output pasted. (This sub is run by the SynapCores team.)
The problem
Ana is a frontend dev. Her Kafka consumers keep falling behind and she needs help. Lessons 1-3 taught search by meaning, so let's find the right person:
CREATE TABLE people (name TEXT PRIMARY KEY, about TEXT, embedding VECTOR(384));
INSERT INTO people (name, about) VALUES
('Ana', 'Frontend developer. React, CSS, accessibility.'),
('Ben', 'Backend developer. Postgres tuning and slow query plans.'),
('Chloe', 'SRE. Keeps our Kafka clusters up and runs the on-call rota.'),
('Dev', 'Kafka contributor. Consumer lag, partitions and rebalancing.'),
('Eli', 'Designer. Figma and the design system.'),
('Fay', 'Product manager for onboarding.'),
('Gus', 'Platform engineer. Message queues, RabbitMQ, event streaming.'),
('Hana', 'Accountant. Payroll and expenses.');
UPDATE people SET embedding = EMBED(about);
SELECT name, ROUND(COSINE_SIMILARITY(embedding,
EMBED('my Kafka consumers keep falling behind')), 2) AS score
FROM people WHERE name <> 'Ana' ORDER BY score DESC LIMIT 4;Output:
name | score
Dev | 0.72
Chloe | 0.42
Gus | 0.20
Ben | 0.14Dev is the best match on paper, but nobody Ana knows has ever met Dev. The real question is "who knows Kafka that I can actually reach", and the second half is a graph question: who is connected to whom.
Step 1: the "who knows whom" graph (same database)
SynapCores also speaks Cypher, the graph query language (nodes in round brackets, relationships in square brackets):
MERGE (ana:Person {name: "Ana"}) MERGE (ben:Person {name: "Ben"})
MERGE (ana)-[:KNOWS]->(ben);
-- 8 people, 6 links: Ana-Ben, Ana-Eli, Ben-Chloe, Eli-Fay, Fay-Gus, Dev-Hana
-- (full block in the first comment)Same Query Editor, same endpoint as your SQL.
Step 2: everyone Ana can reach in 1 to 3 hops
MATCH p = (me:Person {name: 'Ana'})-[:KNOWS*1..3]-(x:Person)
WHERE x.name <> 'Ana'
RETURN x.name AS name, length(p) AS hops, [n IN nodes(p) | n.name] AS via
ORDER BY hops;Output:
name | hops | via
Ben | 1 | Ana > Ben
Eli | 1 | Ana > Eli
Chloe | 2 | Ana > Ben > Chloe
Fay | 2 | Ana > Eli > Fay
Gus | 3 | Ana > Eli > Fay > Gus[:KNOWS*1..3] means "follow KNOWS links one to three times". With no arrow on either end, the direction doesn't matter. Dev isn't in the list: no path from Ana reaches him.
Step 3: put both answers in one SQL query
Save the reachable people as a normal table, then one query asks both questions:
-- ana_reach(name, hops, via): the 5 rows above (INSERT in the first comment)
SELECT p.name, ROUND(COSINE_SIMILARITY(p.embedding, EMBED('my Kafka consumers keep falling behind')), 2) AS score, r.hops, r.via
FROM people p JOIN ana_reach r ON r.name = p.name
WHERE COSINE_SIMILARITY(p.embedding, EMBED('my Kafka consumers keep falling behind')) > 0.3
ORDER BY score DESC;Output:
name | score | hops | via
Chloe | 0.42 | 2 | Ana > Ben > ChloeOne row, and it's actionable: ask Ben to introduce you to Chloe. The > 0.3 threshold (Lesson 3) keeps Gus (0.20) out, so the answer isn't padded with "kind of related" people.
What you just learned
- Meaning search answers "who is about this?"
- A graph answers "who is connected to whom, and through whom?"
[:KNOWS*1..3]walks 1 to 3 hops;nodes(p)gives you the introduction chain.- Both live in one engine, so the final answer is a plain SQL
JOIN. No second database to sync.
Homework
Ben leaves the company tomorrow, so his KNOWS links go away:
MATCH (b:Person {name: 'Ben'})-[k:KNOWS]-() DELETE k;- Who can Ana still reach in 1 to 3 hops?
- Refresh
ana_reachand rerun Step 3. What comes back? - Is that a bug or the right answer? (Hint: Lesson 3.)
Reply with your answers and I'll post the real output under each one.
Free Community Edition, if you want to run this yourself: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-5
Where does "who knows whom" live at your work today: a wiki page, an org chart, Slack history, or someone's head?
1
u/Alternative_Pin9598 17d ago
Full graph for Step 1 (paste as one statement):
The
ana_reachtable for Step 3:Clean up afterwards:
Want to see graph + SQL on a real problem? Recipe of the week #1 on r/synapcores uses the same kind of multi-hop walk to catch a fraud ring that an anomaly model scores as normal: https://www.reddit.com/r/synapcores/comments/1xlzo4t/
New here? r/synapcores is the product home (install, the 5 recipes to try first, a new recipe every Monday): https://www.reddit.com/r/synapcores/comments/1xfxqej/
Missed Lesson 4 (agent memory: REMEMBER / CURRENT / RECALL)? https://www.reddit.com/r/SQLv2/comments/1xnqkkm/