Docs
  • 0.57.0latest
  • 0.56.0
  • 0.55.0
  • 0.54.0
  • 0.53.0
  • 0.52.1
  • 0.52.0
  • 0.51.0
  • 0.48.0
  • 0.47.0
  • 0.46.0
  • 0.45.0
  • 0.44.0
  • 0.43.0
  • 0.42.0
  • 0.41.0
  • 0.40.0
  • 0.39.6
  • 0.39.5
  • 0.39.3
  • 0.39.2
  • 0.39.1
  • 0.39.0
  • 0.38.4
  • 0.38.2
  • 0.38.1
  • 0.37.1
  • 0.37.0
  • 0.36.2
  • 0.36.1
  • 0.36.0
  • 0.35.3
  • 0.35.2
  • 0.35.1
  • 0.35.0
  • 0.34.4
  • 0.34.3-beta
  • 0.34.2-beta
  • 0.34.1-beta
  • 0.34.0-beta
  • 0.33.4-beta
  • 0.33.3-beta
  • 0.33.2-beta
  • 0.33.1-beta
  • 0.33.0-beta
  • 0.32.0-beta

Joins

Matching tables on a value neither of them stores a pointer for — inner, outer and more than two.

There are two ways to reach a related record here, and they answer different questions.

A traversal follows an address somebody wrote down — an edge, a record link. See Graphs.

A join matches on a value where nobody wrote anything down. The value is the only thing the two sides share.

DEFINE COLLECTION users;
DEFINE COLLECTION orders;
CREATE users = { name: 'ada', city: 'london' };
CREATE users = { name: 'grace', city: 'new york' };
CREATE orders = { who: 'ada', total: 3 };
CREATE orders = { who: 'ada', total: 7 };
CREATE orders = { who: 'nobody', total: 1 };

One row per matching pair

SELECT * FROM users JOIN orders ON users.name = orders.who;

Two rows — ada has two orders. The two sides of ON may be written either way round.

A bare JOIN is inner

SELECT * FROM users JOIN orders ON users.city = orders.who;

No rows. grace has no order and nobody has no user, and neither appears. INNER JOIN is the same read spelled out.

A left record with nothing at the key matches nothing rather than matching everything:

SELECT * FROM users JOIN orders ON users.missing = orders.who;

Keeping what nothing matched: LEFT, RIGHT, FULL

From 0.38.0 the outer joins keep the records the other side has no match for:

SELECT * FROM users LEFT JOIN orders ON users.name = orders.who;
SELECT * FROM users RIGHT JOIN orders ON users.name = orders.who;
SELECT * FROM users FULL JOIN orders ON users.name = orders.who;

LEFT JOIN answers every user — grace, who ordered nothing, is a row holding users alone. RIGHT JOIN is the same read driven from the orders, so the order nobody placed answers too. FULL JOIN answers the left join's rows and then each order nothing matched, alone. OUTER may follow the direction and changes nothing.

The side that did not match is absent from the row, never NULL: orders.total there reads NONE, so WHERE orders = NONE finds the users with no order. A WHERE is asked of the joined rows after matching — LEFT JOIN … WHERE orders.total > 5 keeps only rows whose order is over five.

Joining more than two

Each further JOIN joins its side to the rows so far, and the row keeps every side under its name:

DEFINE COLLECTION cities;
CREATE cities = { name: 'london', country: 'uk' };
SELECT * FROM users JOIN orders ON users.name = orders.who
JOIN cities ON users.city = cities.name;

Two rows, each { users: …, orders: …, cities: … }. Each ON matches the new side to a side already joined, by equality; any join of the chain may be LEFT, RIGHT or FULL, and a name may stand in it once.

A row is a record with two named sides

So nothing collides, however the two tables name their fields:

SELECT users.name AS who, orders.total AS spent
FROM users JOIN orders ON users.name = orders.who;

WHERE, ORDER BY and LIMIT all work over the composite row:

SELECT * FROM users JOIN orders ON users.name = orders.who WHERE orders.total > 5;

SELECT * FROM users JOIN orders ON users.name = orders.who
ORDER BY orders.total DESC LIMIT 1;

The join matches exactly what = matches

Including across number kinds:

CREATE users = { name: 'exact', qty: 3 };
CREATE orders = { who: 'exact', qty: 3.0 };
SELECT * FROM users JOIN orders ON users.qty = orders.qty;

One row. An integer 3 matches a float 3.0, because a number's equality here is its order — and a comparison that disagreed with the order its own index is stored in is the failure this store keeps refusing.

That has a real consequence for how a join may be implemented. A hash-keyed join would put 3 and 3.0 in different buckets and miss the pair with no error at all — the read would simply return one row fewer than it should, and nothing would say so. So the join agrees with = by construction rather than by coincidence.

An index changes the cost and not the answer

The whole assertion is the pair of statements: identical text, identical answer, and the access path is the only thing that moved.

SELECT * FROM users JOIN orders ON users.name = orders.who;
-- 3 rows, by scan

DEFINE INDEX by_who ON orders FIELDS who;

SELECT * FROM users JOIN orders ON users.name = orders.who;
-- 3 rows, through the index

And the cross-kind rule still holds through the index:

DEFINE INDEX by_qty ON orders FIELDS qty;
SELECT * FROM users JOIN orders ON users.qty = orders.qty;

Still one row. An index that ordered numbers differently from the comparison would break this, which is why the two are the same order.