LEFT JOIN and RIGHT JOIN in PostgreSQL - Time & Space Complexity
When we use LEFT JOIN or RIGHT JOIN in SQL, the database combines rows from two tables based on a condition.
We want to understand how the time it takes grows as the tables get bigger.
Analyze the time complexity of the following SQL query using LEFT JOIN.
SELECT a.id, b.value
FROM table_a a
LEFT JOIN table_b b ON a.id = b.a_id;
This query returns all rows from table_a and matches rows from table_b where the id matches a_id. If no match, it shows NULL for b.value.
Look at what repeats as the query runs:
- Primary operation: For each row in
table_a, the database looks for matching rows intable_b. - How many times: This happens once per row in
table_a.
Imagine the number of rows in table_a is n and in table_b is m.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 lookups in table_b |
| 100 | About 100 lookups in table_b |
| 1000 | About 1000 lookups in table_b |
As table_a grows, the number of lookups grows roughly the same amount. The size of table_b affects how fast each lookup is.
Time Complexity: O(n)
This means the time grows mostly with the number of rows in the first table (table_a), assuming lookups in table_b are fast (e.g., indexed).
[X] Wrong: "The query always takes time proportional to the product of both tables' sizes (n x m)."
[OK] Correct: Because databases use indexes or hashing to find matches quickly, they don't check every row in table_b for each row in table_a. So the time depends mostly on table_a size.
Understanding how JOINs scale helps you write queries that stay fast as data grows. This skill shows you know how databases work behind the scenes.
"What if we changed LEFT JOIN to RIGHT JOIN? How would the time complexity change?"
Practice
LEFT JOIN do in PostgreSQL?Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN returns all rows from the left table regardless of matches in the right table.Step 2: Check what happens with unmatched rows
If there is no matching row in the right table, the result includes NULLs for right table columns.Final Answer:
Returns all rows from the left table and matching rows from the right table, or NULL if no match. -> Option AQuick Check:
LEFT JOIN = all left rows + matched right rows [OK]
- Confusing LEFT JOIN with RIGHT JOIN
- Thinking LEFT JOIN returns only matched rows
- Assuming unmatched rows are dropped
Solution
Step 1: Recall correct RIGHT JOIN syntax
The correct syntax is: SELECT ... FROM left_table RIGHT JOIN right_table ON condition;Step 2: Check each option
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; matches the correct syntax exactly. Options B, C, and D have syntax errors or misplaced keywords.Final Answer:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id; -> Option AQuick Check:
RIGHT JOIN syntax = FROM left_table RIGHT JOIN right_table ON condition [OK]
- Placing JOIN keyword incorrectly
- Using WHERE instead of ON for join condition
- Misordering table names
employeesid | name
1 | Alice
2 | Bob
3 | Carol
departmentsid | dept_name
2 | Sales
3 | HR
4 | IT
What is the result of this query?
SELECT employees.name, departments.dept_name FROM employees LEFT JOIN departments ON employees.id = departments.id ORDER BY employees.id;
Solution
Step 1: Understand LEFT JOIN with given tables
LEFT JOIN keeps all employees. For each employee, it tries to find a department with matching id.Step 2: Match rows and fill NULLs
Alice (id=1) has no matching department, so dept_name is NULL. Bob (id=2) matches Sales. Carol (id=3) matches HR.Final Answer:
[{"name": "Alice", "dept_name": null}, {"name": "Bob", "dept_name": "Sales"}, {"name": "Carol", "dept_name": "HR"}] -> Option CQuick Check:
LEFT JOIN keeps all employees, unmatched dept_name is NULL [OK]
- Assuming unmatched rows are excluded
- Mixing up LEFT and RIGHT JOIN results
- Ignoring NULL values for unmatched rows
SELECT * FROM employees RIGHT JOIN departments employees.id = departments.id;
What is the error and how to fix it?
Solution
Step 1: Identify syntax error in JOIN clause
The query is missing the ON keyword before the join condition, which is required syntax.Step 2: Correct the syntax
Add ON before employees.id = departments.id to fix the error.Final Answer:
Missing ON keyword before join condition; fix by adding ON before employees.id = departments.id -> Option BQuick Check:
JOIN condition must use ON keyword [OK]
- Omitting ON keyword
- Using WHERE instead of ON for join condition
- Confusing table order in JOIN
Solution
Step 1: Identify which table to keep all rows from
We want all departments, even those without employees, so departments is the left table in LEFT JOIN.Step 2: Use LEFT JOIN from departments to employees
LEFT JOIN keeps all departments and adds matching employees or NULL if none.Final Answer:
SELECT departments.dept_name, employees.name FROM departments LEFT JOIN employees ON departments.id = employees.id; -> Option DQuick Check:
LEFT JOIN from departments keeps all departments [OK]
- Using employees as left table instead of departments
- Using RIGHT JOIN incorrectly
- Confusing which table to keep all rows from
Ready to prove it?
Pass the test and this pattern moves unknown
