INNER JOIN syntax in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use INNER JOIN in SQL, we combine rows from two tables based on matching values. Understanding how the time it takes grows as tables get bigger helps us write better queries.
We want to know: How does the work needed change when the tables have more rows?
Analyze the time complexity of the following code snippet.
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;
This query finds employees and their department names by matching department IDs in both tables.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each employee against departments to find matching department_id.
- How many times: For each employee row, the database looks for matching department rows.
As the number of employees and departments grows, the work to find matches grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 employees, 5 departments | About 50 checks |
| 100 employees, 10 departments | About 1,000 checks |
| 1,000 employees, 100 departments | About 100,000 checks |
Pattern observation: The number of checks grows roughly by multiplying the number of rows in both tables.
Time Complexity: O(n * m)
This means the time grows by multiplying the number of rows in the first table by the number of rows in the second table.
[X] Wrong: "INNER JOIN always runs in linear time because it just matches rows once."
[OK] Correct: Actually, the database may need to compare many rows from both tables, so the work grows with both table sizes, not just one.
Knowing how INNER JOIN scales helps you explain query performance clearly. It shows you understand how databases handle matching data, a useful skill in many real projects.
"What if we add an index on the department_id column? How would the time complexity change?"
Practice
INNER JOIN do in SQL?Solution
Step 1: Understand the purpose of INNER JOIN
INNER JOIN combines rows from two tables where the join condition matches in both tables.Step 2: Compare options with INNER JOIN behavior
Only the description "It returns rows that have matching values in both tables." correctly states that it returns rows with matching values in both tables.Final Answer:
It returns rows that have matching values in both tables. -> Option BQuick Check:
INNER JOIN = matching rows only [OK]
- Thinking INNER JOIN returns all rows from one table
- Confusing INNER JOIN with LEFT or RIGHT JOIN
- Assuming it returns unmatched rows
Employees and Departments on the column DeptID using the ON clause?Solution
Step 1: Review INNER JOIN syntax
The correct syntax uses INNER JOIN with ON and a single equals sign (=) for comparison.Step 2: Check each option
SELECT * FROM Employees INNER JOIN Departments ON Employees.DeptID = Departments.DeptID; uses correct INNER JOIN syntax with ON and =. SELECT * FROM Employees JOIN Departments WHERE Employees.DeptID = Departments.DeptID; uses WHERE instead of ON. SELECT * FROM Employees INNER JOIN Departments USING DeptID; uses USING instead of ON. SELECT * FROM Employees INNER JOIN Departments ON Employees.DeptID == Departments.DeptID; uses double equals (==), which is not valid in SQL.Final Answer:
SELECT * FROM Employees INNER JOIN Departments ON Employees.DeptID = Departments.DeptID; -> Option DQuick Check:
INNER JOIN syntax = ON with single = [OK]
- Using WHERE instead of ON for join condition
- Using double equals (==) instead of single equals (=)
- Using USING instead of ON
Employees(id, name, dept_id)Departments(dept_id, dept_name)What is the result of this query?
SELECT name, dept_name FROM Employees INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;
Assuming:
Employees: (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Carol', 30)
Departments: (10, 'HR'), (20, 'Sales')
Solution
Step 1: Identify matching rows by dept_id
Employees with dept_id 10 and 20 match Departments with same dept_id. Carol's dept_id 30 has no match.Step 2: Understand INNER JOIN output
INNER JOIN returns only rows with matching dept_id in both tables, so Carol is excluded.Final Answer:
[('Alice', 'HR'), ('Bob', 'Sales')] -> Option AQuick Check:
INNER JOIN excludes unmatched rows [OK]
- Including unmatched rows in result
- Assuming NULL values appear for unmatched rows
- Confusing INNER JOIN with LEFT JOIN behavior
SELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id == d.dept_id;
Solution
Step 1: Check join condition syntax
SQL uses a single equals sign (=) for comparison, not double equals (==).Step 2: Verify other parts of the query
Aliases e and d are valid. WHERE clause is optional. INNER JOIN can use ON.Final Answer:
The join condition uses '==' instead of '='. -> Option AQuick Check:
Use = for join conditions, not == [OK]
- Using == instead of = in join condition
- Thinking aliases are disallowed
- Confusing ON with WHERE clause necessity
Orders(order_id, customer_id, amount)Customers(customer_id, customer_name)You want to find all customers who have placed orders and the total amount they spent.
Which query correctly uses INNER JOIN and aggregation to get this result?
Solution
Step 1: Understand the requirement
We want customers who placed orders and the total amount spent, so INNER JOIN with aggregation is needed.Step 2: Analyze each option
SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; correctly uses INNER JOIN on customer_id and sums amounts grouped by customer_name. SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; uses LEFT JOIN, which includes customers without orders. SELECT c.customer_name, o.amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id; does not aggregate amounts. SELECT customer_name, amount FROM Customers INNER JOIN Orders ON customer_id = customer_id; has ambiguous join condition and no aggregation.Final Answer:
SELECT c.customer_name, SUM(o.amount) AS total_spent FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name; -> Option CQuick Check:
INNER JOIN + GROUP BY + SUM = total spent per customer [OK]
- Using LEFT JOIN instead of INNER JOIN when only matching rows needed
- Forgetting GROUP BY with aggregation
- Incorrect join condition syntax
