Module 04 · Lesson 2 of 16Detailed study · about 18 min

Cross joins and self joins

A cross join creates every combination of two inputs. A self join uses two aliases for the same input to compare related rows.

Understand the idea

A shop offers two sizes in three colors. CROSS JOIN is exactly the right operation to list six possible products. The same operation on a million rows and a million rows would describe a trillion pairs, so a missing join condition can be expensive.

Key ideas in simple English
ConceptWhat it means
CROSS JOINEvery left row paired with every right row.
Self joinThe same dataset plays two roles, such as employee and manager.
CardinalityThe number of result rows; estimate it before running a large join.

A concrete example

Two sizes multiplied by three colors gives six rows. Sorting shows M/Blue, M/Green, M/Red, S/Blue, S/Green and S/Red.

Remember: A cross join multiplies row counts: 4 × 5 = 20. Use it when every combination is intended.

Explain it step by step · full theory

Let us understand the idea

A shop offers two sizes in three colors. CROSS JOIN is exactly the right operation to list six possible products. The same operation on a million rows and a million rows would describe a trillion pairs, so a missing join condition can be expensive.

A self join is not a special storage structure. In an employee hierarchy, one alias represents the employee and another represents the manager. The table is the same, but each alias has a different job.

Reason through the SQL

For the manager query use LEFT JOIN because the chief executive has no manager. Matching employee.MANAGER_ID to manager.ID produces the relationship. Matching ID to itself would simply return each employee as their own manager.

When finding unique pairs within one table, a condition such as a.ID < b.ID can exclude self-pairs and mirrored pairs. Choose that rule intentionally rather than applying DISTINCT after generating excessive rows.

Words used in this lesson
CROSS JOIN
Every left row paired with every right row.
Self join
The same dataset plays two roles, such as employee and manager.
Cardinality
The number of result rows; estimate it before running a large join.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Cross joins and self joins

Input rows
Business question

Begin with the question

A cross join creates every combination of two inputs. A self join uses two aliases for the same input to compare related rows.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Begin with the question

    A cross join creates every combination of two inputs. A self join uses two aliases for the same input to compare related rows.

  2. Choose the operation

    A shop offers two sizes in three colors. CROSS JOIN is exactly the right operation to list six possible products. The same operation on a million rows and a million rows would describe a trillion pairs, so a missing join condition can be expensive.

  3. Check the meaning

    A cross join multiplies row counts: 4 × 5 = 20. Use it when every combination is intended.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Read first, then run manually in an authorized Snowflake learning session with a warehouse. Each WITH/VALUES example includes its own inputs and creates no stored tables. Expected results are illustrative, not live execution.

All size and color combinations

SQL · example
WITH sizes AS (SELECT COLUMN1 AS SIZE FROM VALUES ('S'),('M')),
colors AS (SELECT COLUMN1 AS COLOR FROM VALUES ('Blue'),('Green'),('Red'))
SELECT SIZE, COLOR FROM sizes CROSS JOIN colors ORDER BY SIZE, COLOR;

What to expect and why

Two sizes multiplied by three colors gives six rows. Sorting shows M/Blue, M/Green, M/Red, S/Blue, S/Green and S/Red.

Employees and their managers

SQL · example
WITH staff AS (SELECT COLUMN1 AS ID, COLUMN2 AS NAME, COLUMN3 AS MANAGER_ID
FROM VALUES (1,'Asha',NULL),(2,'Ben',1),(3,'Chen',1))
SELECT e.NAME AS EMPLOYEE, m.NAME AS MANAGER
FROM staff e LEFT JOIN staff m ON e.MANAGER_ID=m.ID ORDER BY e.ID;

What to expect and why

Two aliases let the same dataset represent both sides of a relationship.

Expected result for the supplied input (not live output)
EMPLOYEEMANAGER
AshaNULL
BenAsha
ChenAsha

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

How many rows does a cross join of 4 sizes and 5 colors produce?

Try it yourself

Add one color and calculate the new number of combinations before changing the query.

Compare your reasoning

Two sizes and four colors produce 2 × 4 = 8 combinations. No join key removes any pair.

Official sources and curriculum

Module 04 · See module scope