Bird
0
0

What will be the result of this query on table orders with columns order_id and price if order_id values are not unique?

medium📝 query result Q5 of 15
SQL - Advanced Query Patterns
What will be the result of this query on table orders with columns order_id and price if order_id values are not unique?
SELECT o1.order_id, o1.price, (SELECT SUM(o2.price) FROM orders o2 WHERE o2.order_id <= o1.order_id) AS running_total FROM orders o1 ORDER BY o1.order_id;
AQuery will fail with syntax error because order_id is not unique.
BRunning totals may be incorrect due to duplicate order_id values causing sums to include multiple rows.
CRunning totals will be correct regardless of duplicate order_id values.
DQuery will return NULL for running_total when duplicates exist.
Step-by-Step Solution
Solution:
  1. Step 1: Understand effect of duplicates in correlated subquery

    The condition o2.order_id <= o1.order_id sums all rows with order_id less or equal, but duplicates cause repeated sums.
  2. Step 2: Identify impact on running total

    Duplicates cause running totals to be larger than expected because multiple rows share the same order_id and are summed multiple times.
  3. Final Answer:

    Running totals may be incorrect due to duplicate order_id values causing sums to include multiple rows. -> Option B
  4. Quick Check:

    Duplicates affect running total sums = Incorrect totals [OK]
Quick Trick: Ensure unique ordering column to avoid running total errors [OK]
Common Mistakes:
  • Assuming duplicates don't affect sums
  • Expecting syntax error on duplicates
  • Ignoring impact of non-unique keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes