Start studying
Module 5 · SQL Module contents
Module 5  ·  Card 11 / 14advanced

Self Join and Cross Join

A self join connects a table to itself, treating the same physical dataset as two distinct logical tables via aliases.

It is used to compare rows within the same table, such as linking employees to their managers who are also employees in the same roster.

A CROSS JOIN produces a Cartesian product, pairing every single row from the first table with every single row from the second table.

If the first table has ten rows and the second has five rows, a CROSS JOIN generates exactly fifty output rows.

While self joins unpack organisational hierarchies, unintended cross joins can overwhelm database memory by creating millions of rows.

Learn in depth ↓
Module 05 · SQL · 14 concepts
In depth  /  15 lines

Self Join and Cross Join, in more detail

A self join is a regular join where a table is joined to itself, using aliases to distinguish the two roles.

It allows queries to evaluate relationships between different rows within the same physical table.

Self joins are standard for querying hierarchical tree structures, such as organisational charts or category taxonomies.

In an employee table, each row records an employee_id and a manager_id pointing to another employee_id in the same table.

By joining the table to itself on manager_id equals employee_id, a query retrieves each employee alongside their manager name.

Self joins are also used to detect duplicate entries or compare sequential events, such as tracking customer purchases over time.

Joining on unequal operators, like less than, enables triangular self joins that compute pairwise combinations without self-pairings.

A CROSS JOIN produces a Cartesian product, combining every row of the first table with every row of the second table.

It requires no ON clause because every conceivable pairing between the two input tables is generated unconditionally.

If table A contains one thousand rows and table B contains one thousand rows, their CROSS JOIN yields one million rows.

CROSS JOINs are useful for generating complete grids of possibilities, such as pairing all products with all calendar dates.

Joining sales against this complete grid exposes zero-sales days that would otherwise be missing from transaction logs.

However, accidental cross joins caused by omitting a join predicate can quickly exhaust database memory and disk storage.

In multi-table queries, a missing ON condition creates a Cartesian product that severely degrades database server performance.

Analysts should always verify that self joins use distinct aliases and ensure cross joins are restricted to small dimension sets.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An employee table contains an employee_id and a manager_id, where managers are also employees. How can a query show each employee alongside their manager?
SQL

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close