← Back to SQL, page 3

sql-0054 · Easy fresher

When is a CROSS JOIN useful? Give a practical example.

A CROSS JOIN returns every combination of rows from two tables. It is useful when you deliberately need all combinations, such as every size with every colour for a product catalogue, or every student with every course to build an attendance grid that a LEFT JOIN then fills in. Without that intent, it is usually a bug.

Full answer

A CROSS JOIN has no condition. Every row on the left is paired with every row on the right, so 3 sizes and 2 colours give 6 rows:

sql
WITH sizes(size) AS (VALUES ('S'), ('M'), ('L')),
     colours(colour) AS (VALUES ('Red'), ('Blue'))
SELECT size, colour
FROM sizes CROSS JOIN colours
ORDER BY size, colour;
sizecolour
LBlue
LRed
MBlue
MRed
SBlue
SRed

The more interesting use is as a scaffold. Suppose a report must show every student against every course, with the mark if it exists and a blank otherwise. Start from all combinations, then LEFT JOIN the real data onto them:

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);
sql
SELECT s.name, c.title, e.marks
FROM students s
CROSS JOIN courses c
LEFT JOIN enrolments e ON e.student_id = s.id AND e.course_id = c.id
WHERE s.id IN (1, 4)
ORDER BY s.name, c.title;
nametitlemarks
AshaAlgorithms92
AshaDatabases88
AshaEthicsNULL
AshaStatisticsNULL
DaraAlgorithmsNULL
DaraDatabasesNULL
DaraEthicsNULL
DaraStatisticsNULL

The CROSS JOIN produces 5 x 4 = 20 student-course combinations (the WHERE keeps two students so the output stays short), and the LEFT JOIN fills in marks where an enrolment exists. Dara gets a full row of blanks, which is exactly what an attendance grid needs to show. The same technique fills gaps in time series: cross join every store with every date in a calendar table, then left join the sales.

The danger is size. Two tables of 10,000 rows each give 100,000,000 combinations. An accidental cross join, often from an old-style comma join with a missing WHERE condition, is a classic way to overload a database.

sql
CREATE TABLE students (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  city TEXT,
  year INTEGER
);
CREATE TABLE courses (
  id      INTEGER PRIMARY KEY,
  title   TEXT NOT NULL,
  credits INTEGER
);
CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course_id  INTEGER REFERENCES courses(id),
  marks      INTEGER,
  PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
  (1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
  (3,10,64),(3,20,71),(3,30,80),(5,20,55);

WITH sizes(size) AS (VALUES ('S'), ('M'), ('L')),
     colours(colour) AS (VALUES ('Red'), ('Blue'))
SELECT size, colour
FROM sizes CROSS JOIN colours
ORDER BY size, colour;

SELECT s.name, c.title, e.marks
FROM students s
CROSS JOIN courses c
LEFT JOIN enrolments e ON e.student_id = s.id AND e.course_id = c.id
WHERE s.id IN (1, 4)
ORDER BY s.name, c.title;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • CROSS JOIN returns every combination: rows multiply
  • Useful for building complete grids, then LEFT JOIN the facts
  • Calendar x entity is the standard gap-filling pattern
  • An accidental cross join can explode the row count

Common mistakes

  • Writing FROM a, b and forgetting the join condition
  • Cross joining two large tables without a filter

Follow-ups

  • How would you produce a row for every day in March even when there were no sales?
  • How can you spot an accidental cross join in a query plan?

Tips

  • Give the scaffold-then-LEFT JOIN example; it shows a real reason to use it