Bird
0
0

A query joining orders and products is slow. The query is:

medium📝 Debug Q7 of 15
SQL - Advanced Joins
A query joining orders and products is slow. The query is:
SELECT * FROM orders JOIN products ON orders.prod_id = products.id;
Indexes exist on both tables. What join order change might improve performance?
ARemove the join condition
BAdd a WHERE clause filtering orders
CRewrite to join products first, then orders
DUse a CROSS JOIN instead
Step-by-Step Solution
Solution:
  1. Step 1: Consider join order effect

    Joining the smaller or more selective table first can reduce rows early and improve speed.
  2. Step 2: Apply to given tables

    If products is smaller or more selective, joining it first can improve performance.
  3. Final Answer:

    Rewrite to join products first, then orders -> Option C
  4. Quick Check:

    Changing join order can improve speed [OK]
Quick Trick: Join smaller or selective tables first to speed queries [OK]
Common Mistakes:
MISTAKES
  • Removing join condition causes wrong results
  • Using CROSS JOIN increases rows, slowing query
  • Assuming WHERE clause fixes join order issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes