DataDriven
LearnPracticeInterviewDiscussDaily
HelpContactPrivacyTermsSecurityiOS App

© 2026 DataDriven

Loading lesson...

  1. Home
  2. Learn
  3. Joins: Advanced

Joins: Advanced

Self joins and CROSS joins. Send help.

Self joins and CROSS joins. Send help.

Category
SQL
Difficulty
advanced
Duration
32 minutes
Challenges
0 hands-on challenges

Topics covered: Broadcast joins (distributed), Inequality joins with BETWEEN, Self joins for hierarchical data, Recursive CTEs for trees, CROSS JOIN (Cartesian product)

Lesson Sections

  1. Broadcast joins (distributed)

    In distributed query engines, data is split across multiple nodes. When you join two tables, the engine must decide how to bring matching rows together. This decision dramatically affects query performance. The Distribution Problem Consider joining a 100 billion row fact table with a 10,000 row dimension table. The fact table is distributed across 100+ nodes. For each fact row to find its dimension match, the dimension data must be accessible. Broadcast Join Mechanics A broadcast join copies the

  2. Inequality joins with BETWEEN

    Not all joins match on exact values. Range-based matching opens up powerful patterns for lookups, attribution, and temporal analysis. Range Matching Example Suppose you have employee metrics with salaries and a cost allocation table with budget brackets. Each bracket has a min and max income. To find each employee's tax rate, match their salary to the bracket range: Each employee's salary is matched to the bracket where it falls between min_income and max_income. This is impossible with an equal

  3. Self joins for hierarchical data

    A self join is when a table is joined to itself. This sounds strange at first, but it's essential for querying hierarchical data, comparing rows within the same table, and finding related records. Why Self Join? Self joins solve problems where relationships exist within a single table. The most common example: an employee_metrics table where each employee has a manager_id that references another row in the same table. Alice is the CEO (no manager). Bob and Carol report to Alice. Dave and Eve rep

  4. Recursive CTEs for trees

    Multi-Level Hierarchies Self joins work for one-level relationships (employee to manager). But what if you need all ancestors: employee to manager to manager's manager to CEO? You'd need multiple self joins, and you'd need to know the maximum depth in advance. Recursive CTE Structure Recursive CTE in Practice Recursive CTEs enable traversal of arbitrary-depth hierarchies, path building, and cycle detection in graph structures. Hierarchy Example Let's find all entries under Alice (the CEO) and th

  5. CROSS JOIN (Cartesian product)

    A cross join combines every row from one table with every row from another, creating all possible combinations. Syntax Options SQL supports two equivalent syntaxes for cross joins: CROSS JOIN syntax (preferred) Comma syntax (older SQL-89 style) Observe how every row from the first table pairs with every row from the second, producing all possible combinations. Two rows crossed with two rows yields four output rows. This multiplicative behavior is why cross joins require caution with larger table

Related

  • All Lessons
  • Practice Problems
  • Mock Interview Practice
  • Daily Challenges