AI basics & verificationPractical AI workflows

One row disappeared, and the total fell from 7 to 4: reviewing AI’s UNION

Three input records become two and the quantity total falls from 7 to 4. SQL reports no error, and no result is blank. To find what disappeared, inspect the columns actually selected: provenance that distinguished the original rows was omitted before the merge.

Library shelves filled with books and records beside a clock
Illustrative photo: Javier Vinals · Unsplash LicenseStock illustration for the topic, not a screenshot, test of the pictured equipment or product recommendation.

What changed: Added a tested UNION with provenance columns, distinguishing projection-dependent equality from business identity.

Key Takeaways

SQLite UNION removes duplicate complete result rows; UNION ALL retains every row returned by its branches. Selecting only item and qty makes two desk, 3 records identical in the output. For a detail append, retain source and row identifiers with UNION ALL first, then apply an explicit business deduplication rule. Different sources alone establish neither duplicate nor distinct real-world events.

In this article

Put the meaning of combine into the acceptance rule

The exercise vertically appends same-structure tables a and b while retaining every input detail, without business deduplication. Fictional source rows a/A1 and b/B1 both contain desk, 3. They demonstrate SQL behavior, not proof of two separate transactions in an actual business.

The SQLite compound SELECT documentation distinguishes all rows under UNION ALL from distinct results under UNION. Equality concerns the entire selected result row, rather than an inferred order key or an unselected source identifier. Check matching column meanings first; workbook column alignment covers that structural layer.

Keep three input records in an isolated exercise

On December 3, 2026, the editor executed this sample using Node.js 22.23.2 and in-memory SQLite 3.51.3. No business database was connected and no AI model’s query generation was tested. Explicit ordering makes the displays comparable; default row order is not assumed.

Complete SQL exercise

CREATE TABLE a (row_id TEXT PRIMARY KEY, item TEXT, qty INTEGER);
CREATE TABLE b (row_id TEXT PRIMARY KEY, item TEXT, qty INTEGER);
INSERT INTO a VALUES ('A1','desk',3), ('A2','pen',1);
INSERT INTO b VALUES ('B1','desk',3);

SELECT item, qty FROM a UNION SELECT item, qty FROM b ORDER BY item, qty;

SELECT item, qty FROM a UNION ALL SELECT item, qty FROM b ORDER BY item, qty;

SELECT 'a' AS source, row_id, item, qty FROM a UNION ALL SELECT 'b' AS source, row_id, item, qty FROM b ORDER BY source, row_id;
CREATE and INSERT write to a practice database. Use an isolated exercise database; the editorial run used memory only.

Observed outputs
Selected columns and operationRowsSum of qtyInformation retained
item, qty; UNION24desk 3; pen 1
item, qty; UNION ALL37Two desk 3 rows; pen 1
source, row_id, item, qty; UNION ALL37Traceable a/A1, a/A2, b/B1

Equal values are not a record-identity rule

Projecting a/A1 and b/B1 onto item and qty produces the same desk, 3 row. UNION then collapses those results because the distinguishing fields are absent. This explains the change from 7 to 4; it does not justify replacing every UNION. Distinct combinations may be the intended task.

Retaining source and row_id supports tracing back to input. Separately investigate whether the same event was imported across systems and define a business key, conflict policy and retention rule. For example, if the business confirms that order number plus line number identifies one detail, compare that composite key and conflicting content rather than source alone. Adding different source labels makes result rows distinct without resolving a duplicated order.

Use composite-key and blank-value review for business deduplication instead of deleting rows merely because names or amounts match. Real data containing NULLs, mixed types or special collations needs the actual database comparison rules checked; this sample contains only non-null text and integers.

Reconcile provenance, counts and totals together

For an append, compare the rows actually returned by each branch with the combined count, then inspect identifiers, important values and agreed totals. A matching count establishes only that this step retained rows; column mapping, units and business uniqueness still need review. With filters, compare filtered inputs rather than whole-table counts.

UNION operations stack compatible columns vertically. JOIN combines columns using a relationship and can change counts differently. When reviewing AI code, establish the task before inspecting selected columns, operators and business keys; do not add invented rows afterward to make a total match.

Why adding provenance makes UNION appear repaired

Run the following query in the original three-row exercise. It adds source and row_id while retaining UNION. The result returns to three rows and a qty total of seven, because a/A1 and b/B1 are now different complete result rows. Projection changed what counts as equal; UNION did not learn business-event identity.

An unreliable repair is adding a random marker so every output differs. The row count may recover while repeated imports of the same business event remain. When reviewing AI-written SQL, ask which record unit each operator represents and verify against source records. Do not merely ask it to adjust the total to a target.

For this article’s task of retaining every input detail, UNION ALL directly expresses the requirement. If the task instead asks for distinct item/qty combinations, the original UNION may be correct. Choose the operator from the task rather than from which result happens to be larger.

Continue in the independent exercise database

SELECT 'a' AS source, row_id, item, qty FROM a
UNION
SELECT 'b' AS source, row_id, item, qty FROM b
ORDER BY source, row_id;
Rerun by the editor on December 4, 2026: three output rows, quantity total seven. No real database was queried.

Further reading & sources