Bird
Raised Fist0
SQLquery~5 mins

How the join engine matches rows in SQL

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
Introduction

Joining tables helps combine related information from different places. The join engine matches rows to find pairs that belong together.

When you want to see customer orders along with customer details.
When you need to combine employee data with their department info.
When you want to list products with their supplier names.
When you want to compare sales data from two different months.
When you want to find matching records between two lists.
Syntax
SQL
SELECT columns
FROM table1
JOIN_TYPE JOIN table2
ON table1.common_column = table2.common_column;

JOIN_TYPE can be INNER, LEFT, RIGHT, or FULL depending on what rows you want.

The ON clause tells the engine how to match rows between tables.

Examples
This finds all customers who have orders by matching their IDs.
SQL
SELECT *
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
This shows all employees and their departments. If an employee has no department, they still appear.
SQL
SELECT *
FROM employees
LEFT JOIN departments
ON employees.dept_id = departments.dept_id;
This shows all suppliers and their products. Suppliers without products still appear.
SQL
SELECT *
FROM products
RIGHT JOIN suppliers
ON products.supplier_id = suppliers.supplier_id;
Sample Program

This example creates two tables: customers and orders. It then finds which products each customer bought by matching customer IDs.

SQL
CREATE TABLE customers (
  customer_id INT,
  name VARCHAR(50)
);

CREATE TABLE orders (
  order_id INT,
  customer_id INT,
  product VARCHAR(50)
);

INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
INSERT INTO orders VALUES (101, 1, 'Book'), (102, 2, 'Pen'), (103, 1, 'Notebook');

SELECT customers.name, orders.product
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
ORDER BY customers.name;
OutputSuccess
Important Notes

The join engine looks at each row in the first table and tries to find matching rows in the second table based on the ON condition.

For INNER JOIN, only rows with matches in both tables appear.

For LEFT JOIN, all rows from the first table appear, even if no match is found in the second table.

Summary

Joins combine rows from two tables by matching values in specified columns.

The join engine uses the ON condition to find matching pairs of rows.

Different join types control which rows appear when matches are missing.

Practice

(1/5)
1. What does the SQL JOIN engine use to match rows from two tables?
easy
A. The ON condition specifying matching columns
B. The order of rows in each table
C. The number of columns in each table
D. The table names only

Solution

  1. Step 1: Understand the role of the ON condition

    The ON condition defines which columns from each table must have matching values for rows to join.
  2. Step 2: Recognize what the join engine uses

    The join engine uses this condition to pair rows correctly, ignoring row order or table names alone.
  3. Final Answer:

    The ON condition specifying matching columns -> Option A
  4. Quick Check:

    Join engine matches rows using ON condition [OK]
Hint: Remember: JOIN matches rows using ON condition columns [OK]
Common Mistakes:
  • Thinking row order affects join matching
  • Assuming table names determine matches
  • Confusing number of columns with matching criteria
2. Which of the following is the correct syntax to join two tables Employees and Departments on the column DeptID?
easy
A. SELECT * FROM Employees JOIN Departments WHERE Employees.DeptID = Departments.DeptID;
B. SELECT * FROM Employees JOIN Departments USING Employees.DeptID;
C. SELECT * FROM Employees, Departments ON Employees.DeptID = Departments.DeptID;
D. SELECT * FROM Employees JOIN Departments ON Employees.DeptID = Departments.DeptID;

Solution

  1. Step 1: Identify correct JOIN syntax

    The correct JOIN syntax uses JOIN ... ON ... to specify the matching condition.
  2. Step 2: Check each option

    SELECT * FROM Employees JOIN Departments ON Employees.DeptID = Departments.DeptID; uses JOIN ... ON correctly. SELECT * FROM Employees JOIN Departments WHERE Employees.DeptID = Departments.DeptID; wrongly uses WHERE instead of ON. SELECT * FROM Employees, Departments ON Employees.DeptID = Departments.DeptID; misuses ON with comma join. SELECT * FROM Employees JOIN Departments USING Employees.DeptID; misuses USING syntax with table prefix.
  3. Final Answer:

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

    JOIN syntax requires ON condition [OK]
Hint: JOIN needs ON, not WHERE or comma with ON [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Mixing comma joins with ON clause
  • Incorrect USING syntax with table prefixes
3. Given tables Orders and Customers with columns CustomerID, what will be the result of this query?
SELECT Orders.OrderID, Customers.Name FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

Assuming Orders has 3 rows with CustomerIDs 1, 2, 4 and Customers has 2 rows with CustomerIDs 1, 2.
medium
A. 3 rows with OrderIDs 1, 2, 4 and matching customer names for IDs 1 and 2 only
B. 2 rows with OrderIDs 1 and 2 only, matching customer names
C. All 3 rows with customer names, including NULL for CustomerID 4
D. No rows because CustomerID 4 does not exist in Customers

Solution

  1. Step 1: Understand INNER JOIN behavior

    INNER JOIN returns only rows where the join condition matches in both tables.
  2. Step 2: Apply to given data

    Orders have CustomerIDs 1, 2, 4; Customers have 1, 2. Only CustomerIDs 1 and 2 match, so only those rows appear.
  3. Final Answer:

    2 rows with OrderIDs 1 and 2 only, matching customer names -> Option B
  4. Quick Check:

    INNER JOIN returns only matching rows [OK]
Hint: INNER JOIN shows only matching rows from both tables [OK]
Common Mistakes:
  • Expecting unmatched rows to appear with NULLs
  • Confusing INNER JOIN with LEFT JOIN
  • Assuming all rows from first table appear
4. You wrote this query but it returns fewer rows than expected:
SELECT * FROM Products JOIN Categories ON Products.CategoryID = Categories.ID;

What is the most likely cause?
medium
A. There are Products with CategoryID values not present in Categories
B. The query needs a WHERE clause to filter rows
C. The JOIN keyword is missing
D. The join condition column names are swapped

Solution

  1. Step 1: Analyze INNER JOIN behavior

    INNER JOIN returns only rows where the join condition matches in both tables.
  2. Step 2: Consider missing matches

    If some Products have CategoryID values not in Categories, those Products are excluded, reducing rows.
  3. Final Answer:

    There are Products with CategoryID values not present in Categories -> Option A
  4. Quick Check:

    Missing matches cause fewer rows in INNER JOIN [OK]
Hint: INNER JOIN excludes rows without matching keys [OK]
Common Mistakes:
  • Thinking swapped column names cause fewer rows
  • Assuming JOIN keyword missing causes fewer rows
  • Believing WHERE clause is needed to fix join
5. You want to list all employees and their department names, but some employees have no department assigned. Which join type should you use to ensure all employees appear, even if their department is missing?
hard
A. RIGHT JOIN
B. INNER JOIN
C. LEFT JOIN
D. FULL JOIN

Solution

  1. Step 1: Understand join types and their row inclusion

    INNER JOIN includes only matching rows; LEFT JOIN includes all rows from the left table and matches from right; RIGHT JOIN is opposite; FULL JOIN includes all rows from both.
  2. Step 2: Apply to employees and departments

    To list all employees even if no department exists, use LEFT JOIN from Employees (left) to Departments (right).
  3. Final Answer:

    LEFT JOIN -> Option C
  4. Quick Check:

    LEFT JOIN keeps all left table rows [OK]
Hint: Use LEFT JOIN to keep all left table rows [OK]
Common Mistakes:
  • Using INNER JOIN and missing employees without departments
  • Confusing RIGHT JOIN with LEFT JOIN
  • Assuming FULL JOIN is needed when LEFT JOIN suffices