Bird
Raised Fist0
SQLquery~10 mins

INNER JOIN with ON condition in SQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - INNER JOIN with ON condition
Start with Table A
Apply ON condition
Match rows where ON condition is true
Combine matched rows
Result Table
Output
We start with two tables, check each pair of rows against the ON condition, keep only matching pairs, then combine their columns into the result.
Execution Sample
SQL
SELECT A.id, A.name, B.salary
FROM Employees A
INNER JOIN Salaries B
ON A.id = B.emp_id;
This query joins Employees and Salaries tables where employee IDs match, returning employee id, name, and salary.
Execution Table
StepEmployees.idEmployees.nameSalaries.emp_idSalaries.salaryON Condition (A.id = B.emp_id)ActionOutput Row
11Alice15000TrueInclude row(1, Alice, 5000)
21Alice26000FalseExclude row
32Bob15000FalseExclude row
42Bob26000TrueInclude row(2, Bob, 6000)
53Charlie37000TrueInclude row(3, Charlie, 7000)
63Charlie48000FalseExclude row
7EndNo more rowsQuery complete
💡 All pairs checked; only rows where Employees.id equals Salaries.emp_id are included.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
Current Employees.idNone1233
Current Salaries.emp_idNone1234
Output RowsEmpty[(1, Alice, 5000)][(1, Alice, 5000), (2, Bob, 6000)][(1, Alice, 5000), (2, Bob, 6000), (3, Charlie, 7000)][(1, Alice, 5000), (2, Bob, 6000), (3, Charlie, 7000)]
Key Moments - 2 Insights
Why are some rows from Employees or Salaries not in the output?
Only rows where the ON condition is true (Employees.id = Salaries.emp_id) are included, as shown in execution_table rows 2, 3, and 6 where condition is false and rows are excluded.
Does INNER JOIN include rows without matches in the other table?
No, INNER JOIN only includes rows with matching pairs. The execution_table shows excluded rows when ON condition is false, confirming this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output row at step 4?
A(1, Alice, 6000)
B(2, Bob, 5000)
C(2, Bob, 6000)
D(3, Charlie, 7000)
💡 Hint
Check the Output Row column at step 4 in the execution_table.
At which step does the ON condition first become false?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the ON Condition column in the execution_table to find the first 'False'.
If Salaries.emp_id 4 had a matching Employees.id 4, what would happen to the output?
AAll rows would be excluded
BA new output row with id 4 would be added
CNo change, because INNER JOIN ignores new matches
DOnly Salaries rows would appear
💡 Hint
Refer to variable_tracker showing how output rows grow when ON condition is true.
Concept Snapshot
INNER JOIN combines rows from two tables where the ON condition is true.
Syntax: SELECT columns FROM A INNER JOIN B ON condition;
Only matching rows appear in the result.
Non-matching rows are excluded.
Useful to combine related data from two tables.
Full Transcript
This visual execution shows how INNER JOIN works by checking each pair of rows from two tables against the ON condition. Only pairs where the condition is true are included in the output. The example joins Employees and Salaries tables on employee ID. The execution table traces each step, showing which rows match and are included or excluded. The variable tracker shows how output rows build up. Key moments clarify why some rows are excluded and that INNER JOIN only keeps matching rows. The quiz tests understanding of output rows and condition checks. The snapshot summarizes the INNER JOIN concept and syntax.

Practice

(1/5)
1. What does an INNER JOIN do in SQL when used with an ON condition?
easy
A. It returns all rows from the first table regardless of matches.
B. It returns all rows from both tables, matching or not.
C. It returns all rows from the second table regardless of matches.
D. It returns only rows where the join condition matches in both tables.

Solution

  1. Step 1: Understand INNER JOIN behavior

    INNER JOIN returns rows only when the join condition matches rows in both tables.
  2. Step 2: Compare with other join types

    Unlike LEFT or RIGHT JOIN, INNER JOIN excludes rows without matches.
  3. Final Answer:

    It returns only rows where the join condition matches in both tables. -> Option D
  4. Quick Check:

    INNER JOIN = matching rows only [OK]
Hint: INNER JOIN keeps only matching rows from both tables [OK]
Common Mistakes:
  • Thinking INNER JOIN returns unmatched rows
  • Confusing INNER JOIN with LEFT JOIN
  • Ignoring the ON condition effect
2. Which of the following is the correct syntax for an INNER JOIN with an ON condition between tables Employees and Departments on DepartmentID?
easy
A. SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
B. SELECT * FROM Employees INNER JOIN Departments ON Employees.ID = Departments.ID;
C. SELECT * FROM Employees JOIN Departments USING DepartmentID;
D. SELECT * FROM Employees INNER JOIN Departments WHERE Employees.DepartmentID = Departments.DepartmentID;

Solution

  1. Step 1: Identify correct INNER JOIN syntax

    The INNER JOIN requires the ON keyword followed by the join condition.
  2. Step 2: Check the join condition correctness

    The join should be on Employees.DepartmentID = Departments.DepartmentID, not just ID or WHERE clause.
  3. Final Answer:

    SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; -> Option A
  4. Quick Check:

    INNER JOIN uses ON with condition [OK]
Hint: Use ON keyword with join condition for INNER JOIN [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Joining on wrong columns
  • Omitting ON keyword
3. Given these tables:

Employees
ID | Name | DeptID
1 | Alice | 10
2 | Bob | 20
3 | Carol | 30

Departments
DeptID | DeptName
10 | Sales
20 | HR
40 | IT

What is the result of this query?
SELECT Employees.Name, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.DeptID = Departments.DeptID;
medium
A. [('Alice', 'Sales'), ('Bob', 'HR')]
B. [('Alice', 'Sales'), ('Bob', 'HR'), ('Carol', 'IT')]
C. [('Alice', 'Sales'), ('Carol', 'IT')]
D. [('Bob', 'HR'), ('Carol', 'IT')]

Solution

  1. Step 1: Match Employees.DeptID with Departments.DeptID

    Employees have DeptIDs 10, 20, 30; Departments have 10, 20, 40.
  2. Step 2: Find matching DeptIDs

    Matches are 10 and 20 only; 30 (Carol) has no matching department.
  3. Final Answer:

    [('Alice', 'Sales'), ('Bob', 'HR')] -> Option A
  4. Quick Check:

    INNER JOIN returns only matching DeptIDs [OK]
Hint: Only rows with matching DeptID appear in INNER JOIN result [OK]
Common Mistakes:
  • Including unmatched rows like Carol's
  • Confusing DeptID with ID
  • Assuming all departments join
4. Consider this query:
SELECT e.Name, d.DeptName FROM Employees e INNER JOIN Departments d ON e.DeptID = d.ID;

Given Departments has column DeptID but no ID column, what is the issue?
medium
A. The query will join on wrong columns but still return rows.
B. The query will run but return no rows.
C. The query will cause a syntax error due to wrong column name.
D. The query will join correctly because aliases fix column names.

Solution

  1. Step 1: Check column names in ON condition

    The query uses d.ID but Departments table has DeptID, not ID.
  2. Step 2: Understand SQL error on invalid column

    Referencing a non-existent column causes a syntax or runtime error.
  3. Final Answer:

    The query will cause a syntax error due to wrong column name. -> Option C
  4. Quick Check:

    Wrong column in ON causes error [OK]
Hint: Verify column names in ON condition to avoid errors [OK]
Common Mistakes:
  • Assuming aliases rename columns automatically
  • Using wrong column names in ON clause
  • Expecting empty result instead of error
5. You have two tables:

Orders
OrderID | CustomerID | Amount
1 | 101 | 50
2 | 102 | 75
3 | 103 | 100
4 | 101 | 25

Customers
CustomerID | Name
101 | John
102 | Jane
104 | Mike

Write an INNER JOIN query to find total order amount per customer name, including only customers with orders. Which query is correct?
hard
A. SELECT c.Name, SUM(o.Amount) FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.CustomerID;
B. SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name;
C. SELECT c.Name, SUM(o.Amount) FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.Name;
D. SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY o.CustomerID;

Solution

  1. Step 1: Understand requirement - total per customer with orders only

    INNER JOIN keeps only customers with matching orders; GROUP BY customer name to sum amounts.
  2. Step 2: Check query correctness

    SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name; joins Orders to Customers on CustomerID and groups by c.Name, matching requirement exactly.
  3. Step 3: Compare other options

    SELECT c.Name, SUM(o.Amount) FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.CustomerID; groups by CustomerID but c.Name not grouped (SQL error). SELECT c.Name, SUM(o.Amount) FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.Name; uses LEFT JOIN (includes customers without orders). SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY o.CustomerID; groups by o.CustomerID (not customer name).
  4. Final Answer:

    SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name; -> Option B
  5. Quick Check:

    INNER JOIN with GROUP BY customer name sums orders [OK]
Hint: Join Orders to Customers, group by customer name for totals [OK]
Common Mistakes:
  • Using LEFT JOIN includes customers without orders
  • Grouping by wrong column
  • Joining tables in wrong order causing confusion