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

    CROSS JOIN

    CROSS JOIN returns the Cartesian product of two tables — every combination of A × B.

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

    Introduction

    CROSS JOIN returns the Cartesian product of two tables — every combination of A × B. Powerful for generating series and grids; dangerous if used by accident.

    Learning objective: Apply CROSS 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 CROSS 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 CROSS JOIN b — no ON clause.
    • Result count = rows(A) × rows(B).
    • Use case: generate calendar × users matrix for cohort reports.
    • Accidental cross join (missing ON) is a classic perf bug.
    • Combine with generate_series in Postgres for date grids.

    Step-by-step explanation

    1. State the business question CROSS 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
    dates users
    2024-01 ─┐ ┌── alice
    2024-02 ─┤ CROSS JOIN ─┤── bob
    2024-03 ─┘ └── carol
    Result: 9 rows (3 dates × 3 users)
    Useful for "every user per month" cohort grids.
    Syntax Pattern
    SELECT <cols>
    FROM   <a>
    CROSS JOIN <b>;
    -- no ON clause — every A × every B
    • CROSS JOINReturns the Cartesian product: rows(A) × rows(B).
    • use casePair with generate_series to create date × user grids.
    • dangerAn accidental cross join (missing ON) explodes row counts.
    Live Example
    sql
    SELECT d.day, u.name
    FROM generate_series('2026-01-01'::date, '2026-01-03', '1 day') AS d(day)
    CROSS JOIN users u;
    inputs
    dates (3)users (2)
    Jan 1, Jan 2, Jan 3Ana, Leo
    result (3 × 2 = 6)
    dayname
    Jan 1Ana
    Jan 1Leo
    Jan 2Ana
    Jan 2Leo
    Jan 3Ana
    Jan 3Leo
    A × B (every combination)

    Every combination of A and B — great for cohort grids, deadly by accident.

    Informative example

    Worked SQL example:

    sql
    SELECT d.day, u.name
    FROM generate_series('2026-01-01'::date, '2026-01-03', '1 day') AS d(day)
    CROSS JOIN users u;

    Real-world use

    Reports needing zero-filled rows ('show 0 sales for months with none') use CROSS JOIN with a generated date series.

    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

    • Use it intentionally — never let a missing ON clause sneak in.
    • Pair with generate_series for time grids.
    • Watch out for explosion on big tables.

    Common mistakes

    • Forgotten ON → millions of rows, slow query, memory blow up.

    Advanced interview questions

    Interview Prep

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

    5 questions
    1BeginnerQuestionQ1. What is a Cartesian product?+

    Answer

    Cartesian product.
    2IntermediateQuestionQ2. Use case for CROSS JOIN.+

    Answer

    Use intentionally for grids and zero-fills.
    3IntermediateQuestionQ3. How to detect an accidental cross join.+

    Answer

    Accidental cross joins are a performance disaster.
    4AdvancedQuestionQ4. CROSS JOIN with generate_series — example.+

    Answer

    Accidental cross joins are a performance disaster.
    5AdvancedQuestionQ5. Difference vs INNER JOIN with ON true.+

    Answer

    Accidental cross joins are a performance disaster.

    Hands-on exercise

    Hands-on + interview drill:

    • Implement the smallest correct example for CROSS JOIN on an Orders-like schema.
    • Q1. What is a Cartesian product?
    • Q2. Use case for CROSS JOIN.
    • Q3. How to detect an accidental cross join.
    • Q4. CROSS JOIN with generate_series — example.
    • Q5. Difference vs INNER JOIN with ON true.

    Summary

    CROSS 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.