RIGHT JOIN in MySQL - Time & Space Complexity
When using a RIGHT JOIN in SQL, it's important to understand how the time to run the query grows as the tables get bigger.
We want to know how the number of operations changes when the input tables increase in size.
Analyze the time complexity of the following code snippet.
SELECT A.id, B.value
FROM TableA A
RIGHT JOIN TableB B ON A.id = B.a_id;
This query returns all rows from TableB and matches rows from TableA where the ids match.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row in TableB, the database looks for matching rows in TableA.
- How many times: This matching happens once per row in TableB.
As TableB grows, the database must check more rows to find matches in TableA.
| Input Size (TableB rows) | Approx. Operations |
|---|---|
| 10 | About 10 lookups in TableA |
| 100 | About 100 lookups in TableA |
| 1000 | About 1000 lookups in TableA |
Pattern observation: The work grows roughly in direct proportion to the number of rows in TableB.
Time Complexity: O(n)
This means the time to run the query grows linearly with the number of rows in the right table (TableB).
[X] Wrong: "The time depends mostly on the left table (TableA) size."
[OK] Correct: In a RIGHT JOIN, the database must process every row in the right table (TableB), so its size mainly drives the work.
Understanding how JOINs scale helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we changed RIGHT JOIN to LEFT JOIN? How would the time complexity change?"
Practice
RIGHT JOIN do in SQL?Solution
Step 1: Understand the direction of RIGHT JOIN
A RIGHT JOIN returns all rows from the right table regardless of matches in the left table.Step 2: Identify matching rows from the left table
It includes matching rows from the left table and fills NULL where no match exists.Final Answer:
Returns all rows from the right table and matching rows from the left table. -> Option AQuick Check:
RIGHT JOIN = all right table rows + matched left rows [OK]
- Confusing RIGHT JOIN with LEFT JOIN
- Thinking RIGHT JOIN returns only matching rows
- Assuming RIGHT JOIN returns all rows from left table
Solution
Step 1: Check standard RIGHT JOIN syntax
The correct syntax isSELECT ... FROM table1 RIGHT JOIN table2 ON condition;.Step 2: Validate each option
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; uses correct syntax with ON clause. SELECT * FROM table1 JOIN RIGHT table2 ON table1.id = table2.id; has wrong keyword order. SELECT * FROM table1 RIGHT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON. SELECT * FROM table1 RIGHT JOIN table2 USING id; uses invalid syntax (parentheses required: USING (id)).Final Answer:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; -> Option CQuick Check:
RIGHT JOIN syntax = FROM table1 RIGHT JOIN table2 ON condition [OK]
- Using WHERE instead of ON for join condition
- Incorrect keyword order like JOIN RIGHT
- Confusing RIGHT JOIN with RIGHT OUTER JOIN (both valid but syntax matters)
Employeesid | name
1 | Alice
2 | Bob
Departmentsid | dept_name
2 | Sales
3 | Marketing
What is the result of:
SELECT Employees.name, Departments.dept_name FROM Employees RIGHT JOIN Departments ON Employees.id = Departments.id;Solution
Step 1: Identify RIGHT JOIN behavior with given tables
RIGHT JOIN keeps all rows from Departments (right table). Departments have ids 2 and 3.Step 2: Match Employees.id to Departments.id
Employees with id=2 is Bob, matches Sales. No employee with id=3, so NULL for name and Marketing for dept_name.Final Answer:
[('Bob', 'Sales'), (NULL, 'Marketing')] -> Option DQuick Check:
RIGHT JOIN keeps all right rows, unmatched left = NULL [OK]
- Expecting all employees instead of all departments
- Including unmatched left rows only
- Confusing LEFT JOIN with RIGHT JOIN results
SELECT a.id, b.value FROM tableA a RIGHT JOIN tableB b ON a.id = b.id;It returns no rows even though
tableB has data. What is the most likely cause?Solution
Step 1: Understand RIGHT JOIN behavior
RIGHT JOIN returns all rows from right table even if left table has no matching rows.Step 2: Analyze why no rows returned
If no rows appear, likely the join conditiona.id = b.iddoes not match any rows due to wrong column names or data mismatch.Final Answer:
The join condition is incorrect or mismatched column names. -> Option AQuick Check:
No rows means join condition mismatch [OK]
- Assuming RIGHT JOIN needs WHERE clause
- Thinking RIGHT JOIN returns no rows if left table empty
- Believing syntax error causes no rows instead of error
Ordersorder_id | customer_id
1 | 101
2 | 102
Customerscustomer_id | name
101 | John
103 | Alice
You want to list all customers and their orders if any. Which query using RIGHT JOIN gives the correct result?
Solution
Step 1: Identify which table is right to keep all customers
We want all customers, so Customers must be the right table in RIGHT JOIN.Step 2: Check join condition and table order
SELECT Customers.name, Orders.order_id FROM Orders RIGHT JOIN Customers ON Orders.customer_id = Customers.customer_id; uses Orders RIGHT JOIN Customers ON matching customer_id, so all Customers appear with matching Orders or NULL.Final Answer:
SELECT Customers.name, Orders.order_id FROM Orders RIGHT JOIN Customers ON Orders.customer_id = Customers.customer_id; -> Option BQuick Check:
RIGHT JOIN keeps all right table rows (Customers) [OK]
- Swapping table order and losing all customers
- Using LEFT JOIN instead of RIGHT JOIN incorrectly
- Confusing join condition direction
Ready to prove it?
Pass the test and this pattern moves unknown
