CROSS JOIN — Complete Guide
CROSS JOIN — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 25 of 100
CROSS JOIN
SQL basics → Queries → Advanced
SQL basics · 1 — SELECT · ~6 min · SQL — Joins & Relationships
What is this?
CROSS JOIN returns the cartesian product — every row of A paired with every row of B. No ON clause.
Why should you care?
Useful for generating combinations (sizes × colors) or a small numbers table — dangerous on large tables.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
IF OBJECT_ID(N'dbo.Sizes', N'U') IS NOT NULL DROP TABLE dbo.Sizes;
IF OBJECT_ID(N'dbo.Colors', N'U') IS NOT NULL DROP TABLE dbo.Colors;
CREATE TABLE dbo.Sizes (SizeCode CHAR(1) PRIMARY KEY);
CREATE TABLE dbo.Colors (ColorName NVARCHAR(20) PRIMARY KEY);
INSERT INTO dbo.Sizes VALUES ('S'), ('M'), ('L');
INSERT INTO dbo.Colors VALUES (N'Red'), (N'Blue');
SELECT s.SizeCode, c.ColorName
FROM dbo.Sizes AS s
CROSS JOIN dbo.Colors AS c
ORDER BY s.SizeCode, c.ColorName;
What happened?
- 3 sizes × 2 colors = 6 rows.
- Each pair is a potential SKU variant.
- Never CROSS JOIN million-row tables by accident.
Practice next
- Run the example and count 6 rows.
- Add a third color and rerun (9 rows).
- Intentionally CROSS JOIN Orders to Products without need — then cancel if huge.
- CROSS JOIN a tally of 1..5 for demo date offsets.
- Insert results into ProductVariants.
Remember
CROSS JOIN multiplies row counts. Good for small dimension combos. Guard against accidental use on large sets.
Apparel size × color matrix
Merchandising builds DataVerse variants from Sizes × Colors.
Outcome: All valid SKUs generated in one set-based insert.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!