SQL Tutorial 0/85 lessons ~6 min read Lesson 36

    INNER JOIN

    INNER JOIN returns rows where the join condition matches in both tables.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    INNER JOIN returns rows where the join condition matches in both tables. Unmatched rows are dropped. The default and most-used join.

    Learning objective: Apply INNER JOIN correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.

    Treat every example as production-shaped SQL: explicit column lists, deterministic predicates, and indexes that match the access path. Prefer PostgreSQL dialect notes when dialects diverge; call out MySQL differences when they matter.

    Purpose of this lesson

    Apply INNER JOIN correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.

    Understanding the topic

    Core concepts to understand:

    • FROM a JOIN b ON a.id = b.a_id.
    • Drops rows with no match on either side.
    • Multiple inner joins chain naturally.
    • Best with indexed join columns.
    • Equivalent to WHERE with the same predicate (older syntax).

    Step-by-step explanation

    1. State the business question INNER JOIN answers and the tables involved.
    2. Read the workflow diagram and map each stage to parser, optimizer, or storage.
    3. Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
    4. Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.

    Syntax reference

    Visual workflow / architecture:

    bash
    users orders
    ┌────┬──┐ ┌──────┬─────┐
    │ 1 │A │ │ user │amt │
    │ 2 │B │ │ 1 │ 10 │
    │ 3 │C │ │ 1 │ 20 │
    └────┴──┘ │ 2 │ 30 │ (no match for 3)
    └──────┴─────┘
    INNER JOIN ON users.id = orders.user_id
    → 1A:10, 1A:20, 2B:30 (3C dropped)
    Syntax Pattern
    SELECT <cols>
    FROM   <a> [AS a]
    INNER JOIN <b> [AS b]
           ON  <a.key> = <b.key>;
    • INNER JOINReturns rows where the ON predicate matches in BOTH tables.
    • ONJoin condition — usually FK = PK.
    • dropsUnmatched rows on either side are excluded.
    Live Example
    sql
    SELECT u.name, o.amount
    FROM users u
    INNER JOIN orders o ON o.user_id = u.id;
    users + orders
    u.idu.nameo.user_ido.amount
    1Ana150
    2Leo230
    3Mia——
    joined
    nameamount
    Ana50
    Leo30
    ABA ∩ B

    Mia has no order, so she is dropped from the result.

    Informative example

    Worked SQL example:

    sql
    SELECT u.name, o.amount
    FROM users u
    INNER JOIN orders o ON o.user_id = u.id;

    Real-world use

    Every joined report — orders + users, posts + authors, invoices + customers — uses INNER JOIN by default.

    Security implications

    SQL is a trust boundary. Application code must never concatenate untrusted input into statements.

    • Use bind parameters / prepared statements — never string-built SQL from request bodies.
    • Least-privilege DB roles: app users should not own DDL in production.
    • Avoid returning raw DB errors to clients; log server-side with a correlation id.

    Best practices

    • Index both sides of the join.
    • Always alias tables in joins.
    • Be explicit: INNER JOIN over comma-style joins.

    Common mistakes

    • Selecting * in application queries and shipping wide rows over the network.
    • Filtering on expressions that defeat indexes (WHERE YEAR(created_at) = 2026).
    • Running unbounded deletes/updates without a dry-run SELECT first.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    5 questions
    1BeginnerQuestionQ1. What does INNER JOIN return?+

    Answer

    Returns matching rows from both tables.
    2IntermediateQuestionQ2. Difference vs CROSS JOIN.+

    Answer

    Drops unmatched.
    3IntermediateQuestionQ3. Performance impact of unindexed join columns.+

    Answer

    Default and most common join type.
    4AdvancedQuestionQ4. Equivalent old-style syntax.+

    Answer

    Default and most common join type.
    5AdvancedQuestionQ5. Inner join with multiple tables — how it chains.+

    Answer

    Default and most common join type.

    Hands-on exercise

    Hands-on + interview drill:

    • Implement the smallest correct example for INNER JOIN on an Orders-like schema.
    • Q1. What does INNER JOIN return?
    • Q2. Difference vs CROSS JOIN.
    • Q3. Performance impact of unindexed join columns.
    • Q4. Equivalent old-style syntax.
    • Q5. Inner join with multiple tables — how it chains.

    Summary

    INNER JOIN is a production SQL skill: state the question, write the precise statement, and prove it with an execution plan or constraint — not with lucky result sets.

    Ready to mark this lesson complete?Track your journey across the entire course.