Bird
Raised Fist0
SQLquery~10 mins

INNER JOIN syntax 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 syntax
Start with Table A
Start with Table B
Compare rows from A and B
Match rows where join condition is true
Combine matched rows into one result row
Output combined rows as result set
End
INNER JOIN takes two tables, compares rows based on a condition, and outputs only rows where the condition matches.
Execution Sample
SQL
SELECT A.id, A.name, B.order_id
FROM Customers A
INNER JOIN Orders B ON A.id = B.customer_id;
This query joins Customers and Orders tables, showing customers with their orders only if they have matching IDs.
Execution Table
StepActionTable A RowTable B RowJoin Condition (A.id = B.customer_id)Result Row Produced
1Compare first row of A with first row of Bid=1, name='Alice'order_id=101, customer_id=1Trueid=1, name='Alice', order_id=101
2Compare first row of A with second row of Bid=1, name='Alice'order_id=102, customer_id=2FalseNo row
3Compare second row of A with first row of Bid=2, name='Bob'order_id=101, customer_id=1FalseNo row
4Compare second row of A with second row of Bid=2, name='Bob'order_id=102, customer_id=2Trueid=2, name='Bob', order_id=102
5Compare third row of A with first row of Bid=3, name='Carol'order_id=101, customer_id=1FalseNo row
6Compare third row of A with second row of Bid=3, name='Carol'order_id=102, customer_id=2FalseNo row
7No more rows to compareEnd of join
💡 All rows compared; only rows with matching customer IDs included in result.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
Current Table A RowNoneid=1, name='Alice'id=1, name='Alice'id=2, name='Bob'id=3, name='Carol'
Current Table B RowNoneorder_id=101, customer_id=1order_id=102, customer_id=2order_id=102, customer_id=2order_id=102, customer_id=2
Join Condition ResultNoneTrueFalseTrueFalse
Result Rows ProducedEmpty[id=1, Alice, 101][id=1, Alice, 101][id=1, Alice, 101], [id=2, Bob, 102][id=1, Alice, 101], [id=2, Bob, 102]
Key Moments - 2 Insights
Why are some rows from Table A not in the result?
Rows from Table A only appear if they match a row in Table B on the join condition. See execution_table rows 2, 3, 5, 6 where condition is False, so no result row is produced.
Does INNER JOIN include rows with no matching partner in the other table?
No. INNER JOIN only outputs rows where the join condition is True. Rows without matches are skipped, as shown in execution_table where some comparisons produce no result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result row produced at Step 4?
Aid=1, name='Alice', order_id=102
Bid=2, name='Bob', order_id=102
Cid=3, name='Carol', order_id=101
DNo row
💡 Hint
Check the 'Result Row Produced' column at Step 4 in the execution_table.
At which step does the join condition first become False?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look at the 'Join Condition' column in execution_table rows 1-3.
If a new row in Table B had customer_id=3, how would the result change?
ANo change, because INNER JOIN only matches existing rows
BAll rows from Table A would appear in the result
CA new result row would appear matching Table A's id=3
DThe query would return an error
💡 Hint
Refer to variable_tracker showing how matching customer_id produces result rows.
Concept Snapshot
INNER JOIN syntax:
SELECT columns
FROM TableA
INNER JOIN TableB ON TableA.key = TableB.key;

It returns rows where keys match in both tables.
Rows without matches are excluded.
Used to combine related data from two tables.
Full Transcript
INNER JOIN combines two tables by matching rows where a specified condition is true. It compares each row from the first table with each row from the second table. Only rows where the join condition matches are included in the output. Rows without a matching partner in the other table are not shown. This is useful to find related data that exists in both tables. The example query joins Customers and Orders on customer ID, showing only customers who have orders. The execution table shows step-by-step how each row pair is compared and which produce results. Variables track the current rows and condition results. Key moments clarify why some rows are excluded and how matching works. The quiz tests understanding of join results and condition evaluation. The snapshot summarizes the syntax and behavior of INNER JOIN.

Practice

(1/5)
1. What does an INNER JOIN do in SQL?
easy
A. It returns all rows from the first table only.
B. It returns rows that have matching values in both tables.
C. It returns all rows from the second table only.
D. It returns all rows from both tables, matching or not.

Solution

  1. Step 1: Understand the purpose of INNER JOIN

    INNER JOIN combines rows from two tables where the join condition matches in both tables.
  2. 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.
  3. Final Answer:

    It returns rows that have matching values in both tables. -> Option B
  4. Quick Check:

    INNER JOIN = matching rows only [OK]
Hint: INNER JOIN returns only matching rows from both tables [OK]
Common Mistakes:
  • Thinking INNER JOIN returns all rows from one table
  • Confusing INNER JOIN with LEFT or RIGHT JOIN
  • Assuming it returns unmatched rows
2. Which of the following is the correct syntax for an INNER JOIN between tables Employees and Departments on the column DeptID using the ON clause?
easy
A. SELECT * FROM Employees INNER JOIN Departments ON Employees.DeptID == Departments.DeptID;
B. SELECT * FROM Employees JOIN Departments WHERE Employees.DeptID = Departments.DeptID;
C. SELECT * FROM Employees INNER JOIN Departments USING DeptID;
D. SELECT * FROM Employees INNER JOIN Departments ON Employees.DeptID = Departments.DeptID;

Solution

  1. Step 1: Review INNER JOIN syntax

    The correct syntax uses INNER JOIN with ON and a single equals sign (=) for comparison.
  2. 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.
  3. Final Answer:

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

    INNER JOIN syntax = ON with single = [OK]
Hint: Use ON with single = for INNER JOIN conditions [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Using double equals (==) instead of single equals (=)
  • Using USING instead of ON
3. Given the tables:
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')
medium
A. [('Alice', 'HR'), ('Bob', 'Sales')]
B. [('Alice', 'HR'), ('Bob', 'Sales'), ('Carol', '30')]
C. [('Alice', 'HR'), ('Bob', 'Sales'), ('Carol', NULL)]
D. [('Alice', 'HR'), ('Bob', 'Sales'), ('Carol', 'Finance')]

Solution

  1. 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.
  2. Step 2: Understand INNER JOIN output

    INNER JOIN returns only rows with matching dept_id in both tables, so Carol is excluded.
  3. Final Answer:

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

    INNER JOIN excludes unmatched rows [OK]
Hint: INNER JOIN excludes rows without matching keys [OK]
Common Mistakes:
  • Including unmatched rows in result
  • Assuming NULL values appear for unmatched rows
  • Confusing INNER JOIN with LEFT JOIN behavior
4. Identify the error in this SQL query:
SELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id == d.dept_id;
medium
A. The join condition uses '==' instead of '='.
B. Using alias names for tables is not allowed.
C. Missing WHERE clause for filtering.
D. INNER JOIN requires USING instead of ON.

Solution

  1. Step 1: Check join condition syntax

    SQL uses a single equals sign (=) for comparison, not double equals (==).
  2. Step 2: Verify other parts of the query

    Aliases e and d are valid. WHERE clause is optional. INNER JOIN can use ON.
  3. Final Answer:

    The join condition uses '==' instead of '='. -> Option A
  4. Quick Check:

    Use = for join conditions, not == [OK]
Hint: Use single = in ON clause, not double == [OK]
Common Mistakes:
  • Using == instead of = in join condition
  • Thinking aliases are disallowed
  • Confusing ON with WHERE clause necessity
5. You have two tables:
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?
hard
A. 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;
B. SELECT c.customer_name, o.amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id;
C. 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;
D. SELECT customer_name, amount FROM Customers INNER JOIN Orders ON customer_id = customer_id;

Solution

  1. Step 1: Understand the requirement

    We want customers who placed orders and the total amount spent, so INNER JOIN with aggregation is needed.
  2. 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.
  3. 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 C
  4. Quick Check:

    INNER JOIN + GROUP BY + SUM = total spent per customer [OK]
Hint: Use INNER JOIN with GROUP BY and SUM for totals [OK]
Common Mistakes:
  • Using LEFT JOIN instead of INNER JOIN when only matching rows needed
  • Forgetting GROUP BY with aggregation
  • Incorrect join condition syntax