Cross Join

SQL · Joining Multiple Tables

Theory

CROSS JOIN returns the Cartesian product of two tables: every row of the first is paired with every row of the second, with no join condition. Two sizes crossed with three colours give 2 x 3 = 6 combinations. Writing FROM a, b with no WHERE condition does the same thing in older comma-join syntax.

Row counts multiply quickly: 1,000 rows crossed with 1,000 rows is a million rows, so cross joins on large tables are expensive and often a mistake (for example, a forgotten ON condition). Used on purpose with small tables they are very useful: building every product-variant combination, creating a calendar of every date for every store, or attaching a single-row settings table to every row.

Why we use it

Sometimes you need all possible combinations, not just the ones present in the data, for example every store for every day so that days with zero sales still show up. CROSS JOIN generates that full grid.

When to use it

Use CROSS JOIN deliberately to generate combinations or a reporting grid, then LEFT JOIN actual data onto it. If the tables are related by a key, use INNER or LEFT JOIN with an ON condition instead.

How it works

  1. Take the first row of table A
  2. Pair it with every row of table B
  3. Move to the next row of A
  4. Rows left in A? Yes: Pair that row with all of B too. No: Stop.
  5. Result has rows(A) x rows(B) rows

Syntax

SELECT columns
FROM t1
CROSS JOIN t2;

Example

SELECT * FROM sizes CROSS JOIN colors;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic