Bird
0
0

You want to optimize a query joining employees, departments, and locations. Employees has 5000 rows, departments 50, locations 5. Which join order best improves performance?

hard📝 Application Q9 of 15
SQL - Advanced Joins
You want to optimize a query joining employees, departments, and locations. Employees has 5000 rows, departments 50, locations 5. Which join order best improves performance?

Query:
SELECT * FROM employees JOIN departments ON employees.dept_id = departments.id JOIN locations ON departments.loc_id = locations.id;
AJoin locations to employees first, then departments
BJoin employees to departments first, then locations
CJoin locations to departments first, then employees
DJoin departments to employees first, then locations
Step-by-Step Solution
Solution:
  1. Step 1: Consider table sizes

    Employees is largest, departments medium, locations smallest.
  2. Step 2: Join large to medium first

    Joining employees to departments first reduces rows early, then joining small locations last is efficient.
  3. Final Answer:

    Join employees to departments first, then locations -> Option B
  4. Quick Check:

    Large to medium, then small [OK]
Quick Trick: Join from largest to smallest tables for best performance [OK]
Common Mistakes:
MISTAKES
  • Joining smallest tables first causing inefficiency
  • Ignoring join condition correctness
  • Assuming join order does not affect performance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes