Lesson

MySQL INNER JOIN

10 min reading
Free Course

MySQL INNER JOIN: Matching Rows Across Tables

The MySQL INNER JOIN clause combines rows from two tables only when the join condition evaluates to true in both tables. Any row in the left or right table without a matching key in the other table is excluded from the result set. Use LEFT JOIN instead whenever you need to preserve unmatched rows from the primary table.

Basic Syntax

Place the matching key rule inside the ON clause and keep extra row filters in WHERE:

SELECT a.column1, b.column2
FROM table_a AS a
INNER JOIN table_b AS b
    ON a.id = b.table_a_id
WHERE a.is_active = 1;

Practical SQL Examples

This query joins demo_orders, demo_customers, and demo_products to show complete order details in one result set:

SELECT
    o.id AS order_id,
    c.first_name AS customer_name,
    p.product_name,
    o.total_amount
FROM demo_orders AS o
INNER JOIN demo_customers AS c
    ON o.customer_id = c.id
INNER JOIN demo_products AS p
    ON o.product_id = p.id
WHERE o.status = 'paid'
ORDER BY o.id DESC;
order_id customer_name product_name total_amount
105 Jane Wireless Headphones 89.99
102 Alice Mechanical Keyboard 129.99

Common Mistakes and Best Practices

  • Watch One-to-Many Row Multiplication: Joining demo_customers to demo_orders repeats a customer's row once per order. Never run SUM(c.account_credit) after joining a one-to-many child table—aggregate before joining or sum child columns directly.
  • Keep Join Keys the Same Data Type: Joining INT to VARCHAR disables indexes and forces row-by-row string casting. Match INT to INT (and matching signedness/collation).

Frequently Asked Questions

What happens if the join key contains NULL in both tables?

NULL = NULL evaluates to unknown, so INNER JOIN excludes rows with NULL join keys.

Does table order matter for INNER JOIN results?

No, A INNER JOIN B returns the exact same rows as B INNER JOIN A, and the MySQL optimizer automatically picks which table to scan first.

Is INNER JOIN faster than a correlated subquery?

In most cases yes, because MySQL resolves INNER JOIN with index lookups or hash joins instead of re-running a subquery per outer row.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum