HomeInterview QuestionsExplain the SQL query that breaks down transaction…

Explain the SQL query that breaks down transaction differences between tables txn_a and txn_b.

🟡 Medium Conceptual Mid level
1Times asked
Sep 2026Last seen
Sep 2026First seen

💡 Model Answer

This query also uses a CTE named all_txns but takes a different approach. For each row in txn_a it selects the txn_id, the notion and interest values, and then uses correlated subqueries to pull the corresponding values from txn_b for the same txn_id. If txn_b has no matching row, the subqueries return NULL. The UNION part then adds rows that exist only in txn_b, setting the txn_a columns to NULL. The result is a single result set that contains every transaction from both tables, with NULLs where a counterpart is missing. This layout makes it straightforward to spot differences: you can compare the a_notiona to b_notiona, a_interest to b_interest, and identify rows that exist only in one table. The query is useful for data quality checks, audit trails, or generating a diff report. It relies on the assumption that txn_id is unique within each table.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500