🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

CROSS JOIN

SQL JoinsJoinsđŸŸĸ Free Lesson
SQL Joins

CROSS JOIN

Every row from one table paired with every row from another — the Cartesian product.

  • No Join Condition — pairs every combination without filtering
  • Row Explosion — result size is A × B rows
  • Combinatorial Power — ideal for generating test data and variant catalogs Unleash every possible pairing.

What Is a CROSS JOIN?

Cartesian Product: Table A (3) x Table B (4) = 12 RowsTable A (3)A1A2A3Table B (4)B1B2B3B4Result: 3 x 4 = 12 rows(A1,B1)(A1,B2)(A1,B3)(A1,B4)(A2,B1)(A2,B2)(A2,B3)(A2,B4)(A3,B1)(A3,B2)(A3,B3)(A3,B4)Every A paired with every B — no filterWarning: Row explosion on large tables! 10K x 10K = 100M rows
SELECT *
FROM table_a
CROSS JOIN table_b;

Syntax

-- Explicit CROSS JOIN syntax
SELECT columns
FROM table_a
CROSS JOIN table_b;

-- Implicit syntax (comma-separated)
SELECT columns
FROM table_a, table_b;
Syntax StyleExampleNotes
CROSS JOINFROM a CROSS JOIN bExplicit, preferred
CommaFROM a, bImplicit, less readable
MultipleFROM a CROSS JOIN b CROSS JOIN cChain for 3+ tables

How It Works

Table A RowsTable B RowsResult Rows
3412
10010010,000
1,0001,0001,000,000
10,00010,000100,000,000

Basic Example

-- Pair every customer with every product
SELECT
    c.first_name,
    c.last_name,
    p.product_name
FROM customers c
CROSS JOIN products p
ORDER BY c.first_name, p.product_name;

Generating Product Variants

-- Generate all size × color combinations for a product catalog
SELECT
    p.product_name,
    s.size_name,
    c.color_name,
    CONCAT(p.product_name, ' - ', s.size_name, ' - ', c.color_name) AS variant_name
FROM products p
CROSS JOIN sizes s
CROSS JOIN colors c
ORDER BY p.product_name, s.size_name, c.color_name;

Creating Test Data

-- Populate a user_permissions table with all user × permission pairs
INSERT INTO user_permissions (user_id, permission_id, granted)
SELECT
    u.user_id,
    p.permission_id,
    FALSE
FROM users u
CROSS JOIN permissions p
WHERE NOT EXISTS (
    SELECT 1
    FROM user_permissions up
    WHERE up.user_id = u.user_id
      AND up.permission_id = p.permission_id
);

CROSS JOIN with WHERE (Filtered Cross Join)

-- A CROSS JOIN with a WHERE clause can behave like an INNER JOIN
SELECT
    c.first_name,
    c.last_name,
    o.order_id
FROM customers c
CROSS JOIN orders o
WHERE c.id = o.customer_id;

-- Equivalent to:
SELECT
    c.first_name,
    c.last_name,
    o.order_id
FROM customers c
INNER JOIN orders o ON c.id = o.customer_id;
ApproachWhen to Use
CROSS JOIN + WHEREWhen the filtering condition is complex or non-equi
INNER JOIN + ONStandard practice for equi-joins (preferred)

Practical Use Cases

1. Calendar and Date Matrix

-- Generate a daily sales report for every product × every day in January
SELECT
    d.date_value,
    p.product_name,
    COALESCE(SUM(s.quantity), 0) AS total_sold
FROM (
    SELECT DATE '2024-01-01' + n AS date_value
    FROM generate_series(0, 30) AS n
) d
CROSS JOIN products p
LEFT JOIN sales s
    ON s.sale_date = d.date_value
   AND s.product_id = p.product_id
GROUP BY d.date_value, p.product_name
ORDER BY d.date_value, p.product_name;

2. Comparison Reports

-- Compare each employee's salary against every other employee
SELECT
    e1.first_name AS employee1,
    e1.salary AS salary1,
    e2.first_name AS employee2,
    e2.salary AS salary2,
    e1.salary - e2.salary AS difference
FROM employees e1
CROSS JOIN employees e2
WHERE e1.employee_id < e2.employee_id
  AND ABS(e1.salary - e2.salary) > 10000
ORDER BY difference DESC;

3. Pivot-Style Reports

-- Generate a month × product matrix with zeros for empty cells
SELECT
    m.month_name,
    p.product_name,
    COALESCE(SUM(o.total_amount), 0) AS revenue
FROM (
    SELECT 'January' AS month_name, 1 AS month_num UNION ALL
    SELECT 'February', 2 UNION ALL
    SELECT 'March', 3
) m
CROSS JOIN products p
LEFT JOIN orders o
    ON MONTH(o.order_date) = m.month_num
   AND o.product_id = p.product_id
GROUP BY m.month_name, p.product_name
ORDER BY m.month_num, p.product_name;

Performance Considerations

RiskMitigation
Row explosionLimit source tables or add WHERE early
Memory pressureUse temporary tables for intermediate results
No index usageCROSS JOIN ignores indexes — no join predicate exists
Slow queriesAlways EXPLAIN before running on production data
-- Always check result size before executing
SELECT COUNT(*) FROM table_a;  -- M rows
SELECT COUNT(*) FROM table_b;  -- N rows
-- Result will be M × N — proceed with caution

Practice Exercises

Exercise 1: Generate all combinations of 3 sizes and 5 colors.

-- Solution
SELECT
    s.size_name,
    c.color_name
FROM sizes s
CROSS JOIN colors c
ORDER BY s.size_name, c.color_name;
-- Expected: 15 rows

Exercise 2: Create a report showing every product alongside every month of 2024, with zero sales where no data exists.

-- Solution
SELECT
    p.product_name,
    m.month_name,
    COALESCE(SUM(o.total_amount), 0) AS monthly_revenue
FROM products p
CROSS JOIN (
    SELECT 'Jan' AS month_name, 1 AS m UNION ALL SELECT 'Feb', 2 UNION ALL
    SELECT 'Mar', 3 UNION ALL SELECT 'Apr', 4 UNION ALL
    SELECT 'May', 5 UNION ALL SELECT 'Jun', 6
) m
LEFT JOIN orders o
    ON o.product_id = p.product_id
   AND MONTH(o.order_date) = m.m
GROUP BY p.product_name, m.month_name, m.m
ORDER BY p.product_name, m.m;

Exercise 3: Generate a test dataset assigning every role to every user.

-- Solution
INSERT INTO user_roles (user_id, role_id, assigned_at)
SELECT
    u.user_id,
    r.role_id,
    NOW()
FROM users u
CROSS JOIN roles r
WHERE NOT EXISTS (
    SELECT 1 FROM user_roles ur
    WHERE ur.user_id = u.user_id
      AND ur.role_id = r.role_id
);

See Also


Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement