Text-to-SQL interview question

Generative AIText-to-SQLSQLmedium

Asked for "revenue and number of orders that contain electronics, by customer segment", a text-to-SQL bot generated the query below. The Gold segment has exactly two orders: order 101 (total 80) has two electronics items, and order 102 (total 80) has one electronics item and one grocery item. What does the query return for Gold, and what is the right fix?

sql
SELECT c.segment,
       SUM(o.order_total) AS revenue,
       COUNT(o.order_id)  AS orders
FROM orders o
JOIN order_items i ON i.order_id = o.order_id
JOIN customers c   ON c.customer_id = o.customer_id
WHERE i.category = 'Electronics'
GROUP BY c.segment;
  1. ARevenue 160 and 2 orders: the WHERE filter on order_items runs before the join, so each qualifying order is matched only once and nothing is double counted.
  2. BRevenue 240 and 3 orders: the join repeats order rows, so switching to SUM(DISTINCT o.order_total) and COUNT(DISTINCT o.order_id) removes the duplicates.
  3. CRevenue 320 and 4 orders: WHERE is evaluated after GROUP BY, so the grocery item also inflates the totals; moving the filter into HAVING fixes it.
  4. DRevenue 240 and 3 orders: order 101 appears once per matching item; filter orders with EXISTS on order_items instead, so each order is joined only once.
Show the answer and explanation

Correct answer: D, Revenue 240 and 3 orders: order 101 appears once per matching item; filter orders with EXISTS on order_items instead, so each order is joined only once.

Explanation Text-to-SQL

Joining to a one-to-many child table changes the grain of the result: one row per matching order item, not per order. An order-level column such as order_total is then repeated once per matching child row, and aggregates count it several times. This join fan-out is one of the most common silent errors in generated SQL, because the query runs and the numbers look plausible.

Rows after the joins and the WHERE filter:

order_idorder_totalitem category
10180Electronics
10180Electronics
10280Electronics

So the query returns SUM = 240 and COUNT = 3, while the truth is 160 and 2. The grocery row is removed by WHERE, which always filters rows before GROUP BY. Filtering the items before the join, as optimizers often do, changes nothing: order 101 still matches two electronics rows.

Fix: keep the query at order grain and use the items table only as a filter:

SELECT c.segment, SUM(o.order_total) AS revenue, COUNT(*) AS orders
FROM orders o JOIN customers c ON c.customer_id = o.customer_id
WHERE EXISTS (SELECT 1 FROM order_items i
              WHERE i.order_id = o.order_id AND i.category = 'Electronics')
GROUP BY c.segment;

COUNT(DISTINCT o.order_id) would fix the count, but SUM(DISTINCT o.order_total) is a trap: it adds each distinct value once, so the two orders of 80 sum to 80 instead of 160.

Interview tip: for text-to-SQL, describe each table's grain in the schema prompt, add few-shot examples that aggregate before joining or use EXISTS, and test with questions whose answers you already know.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Generative AI questions

  1. The table shows the next-token distribution at one decoding step for the context "The customer wants to ___". With top-p = 0.9 (nucleus sampling, no top-k),…
  2. A team reports that a new prompt raised RAGAS faithfulness on a 300-question set from 0.81 to 0.86: the old score is from last month, the new one was computed…
  3. A loan pre-screening step uses the schema below with constrained decoding, which emits properties in schema order. Reviewers find that reasoning usually…
  4. The flow shows one generation step of a decoder-only LLM (GPT- or Llama-style). What happens at the highlighted step?
  5. From the same logits, a teacher LLM gives these next-token probabilities after "I want to ___ my subscription" (restricted to five candidate tokens): cancel…