r/SQLv2 • u/Alternative_Pin9598 • 4h ago
SQLv2 Lesson 11: not sure between what? With a black and a white mouse in the catalog, 'Wireless mouse' could be either. One query lists what each line could be, lets the price settle two of four, and writes the supplier question for the rest (real output)
Lesson 10 ended with four invoice lines marked NOT SURE: 'Wireless mouse', 'USB-C laptop charger', '27-inch monitor', 'Mechanical keyboard'. Not sure between what, though? The catalog had one of each. This week Lesson 9's look-alikes become real items (a white mouse, a 100 W charger, a 1080p monitor, a UK keyboard), so 'Wireless mouse' really could be either mouse, and the right move is to ask the supplier with both names in the question.
Tables: Lesson 10's catalog and invoice_lines (script in the first comment).
Step 1: the catalog grows
INSERT INTO catalog (id, name, list_price) VALUES
(109, 'Wireless mouse, white', 19.00), (110, 'USB-C laptop charger, 100 W', 59.00),
(111, '27-inch 1080p monitor', 179.00), (112, 'Mechanical keyboard, UK layout', 79.00);
UPDATE catalog SET vec = EMBED(name) WHERE id > 108;Step 2: what could each line be?
Two rules from earlier lessons. Near-ties: every item within 0.10 of the line's best similarity (and above Lesson 8's 0.43 floor) stays in the running. Then Lesson 9's contradiction check, read both ways, throws out what the line rules out.
WITH sims AS (
SELECT i.line, i.item, i.unit_price, c.name, c.list_price,
COSINE_SIMILARITY(i.vec, c.vec) AS sim
FROM invoice_lines i CROSS JOIN catalog c),
best AS (
SELECT line, MAX(sim) AS best FROM sims GROUP BY line),
near AS (
SELECT s.line, s.item, s.unit_price, s.name, s.list_price, s.sim
FROM sims s JOIN best b ON b.line = s.line
WHERE s.sim >= 0.43 AND s.sim >= b.best - 0.10),
checked AS (
SELECT line, item, unit_price, name, list_price, sim,
JSON_EXTRACT(NLI_CLASSIFY('nli-default', name, item), '$.scores.contradiction') AS c1,
JSON_EXTRACT(NLI_CLASSIFY('nli-default', item, name), '$.scores.contradiction') AS c2
FROM near),
could_be AS (
SELECT * FROM checked WHERE NOT (c1 > 0.5 AND c2 > 0.5))
SELECT line, item, name, ROUND(sim, 2) AS sim, ROUND(c1, 2) AS c1, ROUND(c2, 2) AS c2
FROM could_be ORDER BY line, sim DESCRows that changed (all 21 in the first comment; lines 15 to 17 look like 14):
4 UHD 27in display 27-inch 4K monitor 0.60 0.00 0.00
14 Wireless mouse Wireless mouse, black 0.77 0.01 0.00
14 Wireless mouse Wireless mouse, white 0.76 0.00 0.00
21 rows in 0.34 sLook at line 4. With a 1080p monitor in the catalog, Lesson 10's best-match check now picks it for 'UHD 27in display':
4 UHD 27in display -> 27-inch 1080p monitor 0.65 0.16 0.00 DIFFERENTSimilarity alone books the wrong screen (0.65 beats the 4K monitor's 0.60). UHD contradicts 1080p both ways, so that row is gone and the 4K monitor is all that's left. The vague lines keep two candidates each.
Step 3: book, confirm, or ask
Same CTEs plus a GROUP BY (statement in the first comment). One candidate: BOOK. Two, and exactly one has the invoiced price as its list price: CONFIRM that one. Otherwise ASK, and SQL writes the question with STRING_AGG(name, ' or '):
14 ASK Line 14, "Wireless mouse": is this the Wireless mouse, black or Wireless mouse, white?
15 CONFIRM Line 15, "USB-C laptop charger": we read this as "USB-C laptop charger, 65 W" (the price matches). Correct?All 17 lines: 9 BOOK, 2 CONFIRM, 4 ASK, 2 NO MATCH (the chair, and the 5 m cable, whose only near item is the 2 m cable it contradicts). Two questions are new since Lesson 10, 'blk' and 'ANSI': the model doesn't read them as black or as the US layout, so once a second colour and layout exist, it asks. A question beats a wrong booking; a lookup table of your supplier's abbreviations, joined before Step 2, is the plain-SQL fix.
Ran on SynapCores 2.5.1 on a Mac mini (Apple M4); embeddings and the NLI model (nli-default) run inside the engine, no API key. This sub is run by the SynapCores team. Download: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-11 . Lesson 10: https://www.reddit.com/r/SQLv2/comments/1y59s39/
Homework. The supplier answers: line 14 is the white mouse, line 17 the UK layout. Update both lines (item and vec), run Step 3 again.
- What do lines 14 and 17 say now?
- The US keyboard was within 0.04 of the UK one. Why does it drop out of line 17's candidates?
1
u/Alternative_Pin9598 4h ago
Already have Lesson 10's tables? Step 1 in the post is all you need (if you kept the homework line 18 from Lesson 10,
DELETE FROM invoice_lines WHERE line > 17first).Starting fresh? The full script (Python, standard library only) is in the two replies below.
python3 lesson11.py setupcreates Lesson 10's tables with all 17 lines,growruns Step 1,couldbeStep 2,questionsStep 3,homeworkapplies the supplier's answers, andcleanupdrops everything. Point it at your server withSC_URLandSC_TOKEN.NLI is a one-time opt-in:
synapcores pull nli-default, thensynapcores models enable-nli nli-default, both with the--configyour server uses, then restart the server.Step 3 in full (the CTEs
sims,best,near,checkedandcould_beare the ones from Step 2):All 21 rows of Step 2:
All 17 rows of Step 3:
The same NLI check, used to put several tags on a support ticket in one query, is in r/synapcores: https://www.reddit.com/r/synapcores/comments/1y6vuvz/ . New here? Start with https://www.reddit.com/r/synapcores/comments/1xfxqej/