LEFT JOIN vs RIGHT JOIN decision in SQL - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When using LEFT JOIN or RIGHT JOIN in SQL, it's important to understand how the time to run the query grows as the data grows.
We want to know how the choice between LEFT JOIN and RIGHT JOIN affects the work the database does.
Analyze the time complexity of this SQL join query.
SELECT a.id, b.value
FROM tableA a
LEFT JOIN tableB b ON a.key = b.key;
This query returns all rows from tableA and matches rows from tableB where keys are equal.
Look at what repeats as the database processes the join.
- Primary operation: For each row in tableA, the database looks for matching rows in tableB.
- How many times: This happens once for every row in tableA.
As tableA grows, the number of lookups in tableB grows too.
| Input Size (rows in tableA) | Approx. Operations |
|---|---|
| 10 | About 10 lookups in tableB |
| 100 | About 100 lookups in tableB |
| 1000 | About 1000 lookups in tableB |
Pattern observation: The work grows roughly in direct proportion to the size of tableA.
Time Complexity: O(n)
This means the time to run the join grows linearly with the number of rows in the left table.
[X] Wrong: "LEFT JOIN is always slower than RIGHT JOIN because it processes more data."
[OK] Correct: The speed depends on which table is on the left or right and their sizes, not just the join type name.
Understanding how join direction affects query time helps you write efficient queries and explain your choices clearly in interviews.
What if we swapped the tables and used RIGHT JOIN instead? How would the time complexity change?
Practice
Solution
Step 1: Understand LEFT JOIN behavior
LEFT JOIN returns all rows from the first table and matching rows from the second table. If no match, NULLs appear for second table columns.Step 2: Compare with RIGHT JOIN
RIGHT JOIN keeps all rows from the second table, not the first. INNER JOIN only returns matching rows. FULL JOIN returns all rows from both tables.Final Answer:
LEFT JOIN -> Option CQuick Check:
LEFT JOIN keeps all left table rows [OK]
- Confusing LEFT JOIN with RIGHT JOIN
- Thinking INNER JOIN keeps unmatched rows
- Assuming FULL JOIN keeps only one table's rows
Employees and Departments on Employees.DeptID = Departments.ID?Solution
Step 1: Check JOIN syntax correctness
RIGHT JOIN requires ON clause to specify join condition. SELECT * FROM Employees RIGHT JOIN Departments ON Employees.DeptID = Departments.ID; uses correct syntax with ON clause.Step 2: Identify errors in other options
SELECT * FROM Employees LEFT JOIN Departments ON Employees.DeptID = Departments.ID; uses LEFT JOIN, not RIGHT JOIN. SELECT * FROM Employees RIGHT JOIN Departments WHERE Employees.DeptID = Departments.ID; uses WHERE instead of ON, which is incorrect for JOIN condition. SELECT * FROM Employees JOIN Departments ON Employees.DeptID = Departments.ID; uses JOIN without specifying LEFT or RIGHT, defaults to INNER JOIN.Final Answer:
SELECT * FROM Employees RIGHT JOIN Departments ON Employees.DeptID = Departments.ID; -> Option AQuick Check:
RIGHT JOIN needs ON clause [OK]
- Using WHERE instead of ON for join condition
- Confusing LEFT JOIN with RIGHT JOIN syntax
- Omitting JOIN type defaults to INNER JOIN
Authors and Books, what will be the result of this query?SELECT Authors.Name, Books.Title FROM Authors LEFT JOIN Books ON Authors.ID = Books.AuthorID;
Assuming Authors has 3 rows and Books has 2 rows, with one author having no books, what will the output include?
Solution
Step 1: Understand LEFT JOIN output
LEFT JOIN keeps all rows from Authors (left table). For authors without matching books, Books.Title will be NULL.Step 2: Analyze given data
Authors has 3 rows, Books has 2 rows. One author has no books, so that author appears with NULL for book title.Final Answer:
All 3 authors with their book titles; NULL for authors without books -> Option BQuick Check:
LEFT JOIN keeps all left rows, fills NULLs for no match [OK]
- Thinking unmatched left rows are excluded
- Confusing LEFT JOIN with INNER JOIN
- Assuming unmatched right rows appear in LEFT JOIN
SELECT * FROM Orders RIGHT JOIN Customers ON Orders.CustomerID = Customers.ID;
What is the likely mistake?
Solution
Step 1: Identify which table's rows to keep
If Orders is the main table and you want all its rows, LEFT JOIN should be used, not RIGHT JOIN.Step 2: Understand RIGHT JOIN effect
RIGHT JOIN keeps all rows from Customers (right table), so if Orders has rows without matching Customers, those rows are excluded.Final Answer:
Using RIGHT JOIN instead of LEFT JOIN when Orders is the main table -> Option DQuick Check:
Use LEFT JOIN to keep all left table rows [OK]
- Confusing LEFT and RIGHT JOIN roles
- Expecting RIGHT JOIN to keep left table rows
- Ignoring join direction impact on row count
Students (left) and Enrollments (right). You want a list of all students, including those not enrolled in any course, showing NULL for missing enrollment data.Which query is best and why?
Option A: SELECT * FROM Students LEFT JOIN Enrollments ON Students.ID = Enrollments.StudentID;
Option B: SELECT * FROM Enrollments RIGHT JOIN Students ON Students.ID = Enrollments.StudentID;
Solution
Step 1: Identify which table's rows to keep
You want all students, even those without enrollments, so keep all rows from Students (left table).Step 2: Choose JOIN type accordingly
LEFT JOIN keeps all rows from Students and matches enrollments or shows NULL if none. RIGHT JOIN would keep all enrollments, not all students.Final Answer:
Option A, because LEFT JOIN keeps all students and matches enrollments -> Option AQuick Check:
LEFT JOIN keeps all left table rows [OK]
- Using RIGHT JOIN but expecting all left table rows
- Switching table order without adjusting JOIN type
- Assuming RIGHT JOIN keeps left table rows
