Course overview/Make it stick2 of 3

Capstone: find the six wrong queries

Capstone: find the six wrong queries

Ten queries all run without error. Exactly six are wrong. Find them, name the fault, and fix each.

The task

The project ships ten queries in queries/audit-lab/, named q01.sql through q10.sql. Every one of them runs without error and returns a number. Exactly six of the ten return the wrong answer. Each query has its business question in a comment at the top.

Your job, for each query: decide whether the answer is correct or wrong. If it is wrong, name the failure class, write a corrected query, and record both the wrong number and the right one.

The four correct queries matter as much as the six wrong ones. A student who flags all ten has not learned to audit. They have learned to be suspicious, which is a different and less useful thing. Part of the skill is leaving a correct query alone.

The failure classes you are looking for

Every wrong query fails in one identifiable way, and all six are faults this course has already taught you to spot:

  • A join fan-out that inflates an order-header measure.
  • A != or IN filter that silently drops NULL order_status.
  • The wrong revenue column, unit_price where net_price was meant.
  • A BETWEEN on a timestamp column that drops the last day.
  • An INNER JOIN to a dimension that drops rows with orphan keys.
  • A DISTINCT that hides a duplicated dimension row.

Run each query, then walk the seven-point checklist from "Audit what it wrote" over it. The fault, when there is one, will be one of the six above.

Record your findings

Copy queries/audit-lab/findings-template.md to queries/audit-lab/findings.md and write your findings there, one section per query. For each query state the verdict, the failure class if wrong, the corrected query, both numbers, and what one row represented before and after your fix.

Work with your coding agent

AI Prompt

I am working through the audit lab in queries/audit-lab/. Do not tell me which queries are wrong.

Pick one query I have already formed a verdict on. Check my reasoning: is my verdict right, is the failure class I named the one that actually applies, and does my corrected query return the right number? If I am wrong, ask me a question that points at the fault rather than naming it.

Rubric

Score yourself out of ten. Ten out of ten means the capstone meets the course requirements.

AreaPointsEvidence
Correct verdicts3All ten queries classified correctly, including the four that are right
Failure naming2Each wrong query is labelled with the failure class that actually applies
Corrected queries2Each fix runs and returns the right number
Both numbers stated1The wrong number and the right number are recorded for each finding
Reasoning quality1Each finding says what one row represented before and after the fix
No false positives1The four correct queries are not "fixed"

The answer key is at the bottom of this page - deliberately not in the repository, so you cannot open it by accident before you have verdicts. One related warning from the lab README applies to your agent too: docs/known-defects.md describes what is deliberately wrong with the data, which is a different question from what is wrong with these queries. Reading it hoping for verdicts will mislead you both.

Optional extensions

  • Finish the spine question. You now know everything the customer half needs: ask the agent which customers drove the growth in the winning categories, and audit the query - you know from q09 exactly what to check in a join to customers.
  • Ask the agent to audit the same ten queries, then compare its findings to yours. Where it disagreed with you, who was right?
  • Write a Bruin quality check that would have caught one of the six automatically.
  • Add the failure you found hardest to AGENTS.md, then see whether the agent avoids it next time.

Answer key

Warning

Spoilers for the entire lab. Commit to a verdict on all ten queries in findings.md before reading further.

The six wrong queries are q02, q04, q05, q07, q08, q09. The four correct ones are q01, q03, q06, and q10. Every failure class appears exactly once, and all values are exact, because the data generates identically on every machine.

QueryVerdictAnswer as writtenCorrect answerWhy
q01correct1,2001,200A plain COUNT(*) on one table. No join, no filter, nothing to fan out.
q02wrongLondon 261,246.42 (top row)London 102,911.39order_total is an order-header value, but the join to order_items repeats each order once per line. Sum it from orders alone.
q03correct202 orders per store, Paris 190sameThe join to stores only adds the city name. It does not change the grain, so COUNT(*) still counts orders.
q04wrong1,0691,093!= 'cancelled' silently drops the 24 NULL-status orders. Use IS DISTINCT FROM 'cancelled'.
q05wrong381,357.00338,209.56unit_price is the catalogue price. Revenue is what was charged: quantity * net_price.
q06correct503.39503.39AVG(order_total) over orders alone. One total per order, no join, right denominator.
q07wrong478480ordered_at is a timestamp, so BETWEEN ... AND '2024-12-31' stops at midnight and drops the two orders placed later that day. Use a half-open range.
q08wrong847,979.80 across 8 categories851,617.69 including an Unknown bucket15 order lines point at a product_id that is not in products. The INNER JOIN drops them and 3,637.89 with them.
q09wrong1,411.161,310.60Ten duplicated rows in customers repeat those customers' orders in the join, inflating the numerator while COUNT(DISTINCT customer_id) stays correct - which is exactly why it looks safe.
q10correct2.42.4Lines divided by distinct orders, both from order_items at its own grain.

One subtlety worth knowing if you checked q02 per store: the fan-out factor is exactly 2.4 across the whole table, but ranges from about 2.31 to 2.54 per store, because orders with more lines are not spread evenly. A per-store ratio near-but-not-equal to 2.4 is the data behaving correctly, not an error in your arithmetic.

Sign up to our newsletter

Practical updates on open-source data pipelines, AI analysts, governance, and what we are shipping at Bruin.

The signup form is hosted by Brevo. Allow marketing cookies to load it.