Bird
0
0

How would you modify this query to correctly join Orders and Shipments on OrderID and only include shipments with status 'Delivered' or 'In Transit'?

hard📝 Application Q9 of 15
SQL - INNER JOIN
How would you modify this query to correctly join Orders and Shipments on OrderID and only include shipments with status 'Delivered' or 'In Transit'?
SELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID AND Shipments.Status = 'Delivered' OR Shipments.Status = 'In Transit';
ASELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID WHERE Shipments.Status = 'Delivered' OR Shipments.Status = 'In Transit';
BSELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID OR Shipments.Status = 'Delivered' AND Shipments.Status = 'In Transit';
CSELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID AND Shipments.Status IN ('Delivered', 'In Transit');
DSELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID AND (Shipments.Status = 'Delivered' OR Shipments.Status = 'In Transit');
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator precedence in ON clause

    AND has higher precedence than OR, so parentheses are needed to group OR conditions correctly.
  2. Step 2: Evaluate options

    SELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID AND (Shipments.Status = 'Delivered' OR Shipments.Status = 'In Transit'); correctly groups OR conditions with parentheses; A moves filter to WHERE which changes join logic; B misuses OR and AND; D uses IN which is valid but not asked for modification.
  3. Final Answer:

    SELECT * FROM Orders INNER JOIN Shipments ON Orders.ID = Shipments.OrderID AND (Shipments.Status = 'Delivered' OR Shipments.Status = 'In Transit'); -> Option D
  4. Quick Check:

    Use parentheses to group OR in join conditions [OK]
Quick Trick: Group OR conditions with parentheses in ON clause [OK]
Common Mistakes:
MISTAKES
  • Not using parentheses causing wrong logic
  • Confusing AND/OR precedence
  • Moving join filters to WHERE changing join behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes