# cross-join

Default severity: **warn**

## What it does

Reports a table of a comma join, or of a `JOIN ... ON` whose condition names no column of it, that no condition connects to the other tables of its `FROM`: each of its rows pairs with every row of the others.

```python
from sqlty import SQL

# error: [cross-join] "`orders` is joined by no condition, so each of its rows pairs with every row of the other tables: add one, or write `CROSS JOIN` to mean it"
q = SQL[tuple[int, int]]("--sql\nSELECT users.id AS u, orders.id AS o FROM users, orders")
```

A condition in `ON`, `USING`, `NATURAL` or `WHERE` connects the tables it names, directly or through others. An explicit `CROSS JOIN`, a `LATERAL` subquery, a function of a joined row (`jsonb_array_elements(t.data)`) and a side of one row (an aggregate without `GROUP BY`, `VALUES` with one row, `LIMIT 1`, a CTE of one row) are not reported:

```python
from sqlty import SQL

# error:
q = SQL[tuple[int, int]]("--sql\nSELECT users.id AS u, orders.id AS o FROM users, orders WHERE orders.user_id = users.id")
r = SQL[tuple[int, int]]("--sql\nSELECT users.id AS u, sizes.n FROM users CROSS JOIN (VALUES (1), (2)) AS sizes (n)")
s = SQL[tuple[int, int]]("--sql\nSELECT users.id AS u, t.n FROM users, (SELECT count(*) AS n FROM orders) AS t")
```

## Why is this bad?

Two users and two orders give four rows. A forgotten join condition multiplies the rows, and every total computed over them, without an error.

## How to fix

Add the condition that relates the tables, or write `CROSS JOIN` when every pair is meant.

## Configuration

A warning by default: the query is valid SQL. Turn it into an error or off:

```toml
[tool.sqlty.rules]
cross-join = "error"
```

Needs no schema, but a condition naming a column sqlty can't place in one of the tables, as an unqualified one without a schema, or holding a subquery leaves the query unchecked. A set-returning function of constants (`generate_series(1, 3)`) is reported: write `CROSS JOIN`.
