INNER JOIN ON clause rejects a column name that a later table also has
Author: bluestreak01Created Sep 30, 2026Updated Sep 30, 2026
An unqualified column name in the ON clause of an INNER JOIN fails with
Ambiguous column when a table joined later has a column with the same name. The
ON clause can see only the tables joined so far, so standard SQL resolves the name
to the one table in that scope that has it. The same holds for an equality conjunct
in a LEFT/RIGHT/FULL JOIN ON clause (LEFT JOIN f1 ON w = a0 ...). A non-equality
conjunct in an outer join ON clause already resolves the name this way.
Reproduce (master 641aa5fee5)
CREATE TABLE f0 (a0 INT, b0 INT);
INSERT INTO f0 VALUES (2, null), (3, 1), (2, 2);
CREATE TABLE f1 (a1 INT, b1 INT, w INT);
INSERT INTO f1 VALUES (3, 2, 1), (2, 2, 2), (null, 1, 3), (3, 3, 2), (3, 1, 3);
CREATE TABLE f2 (a2 INT, b2 INT, w INT);
INSERT INTO f2 VALUES (3, 1, 2), (1, null, 3), (3, 3, null), (1, 2, 2), (1, 3, 3);
-- fails: Ambiguous column [name=w] (position 40)
SELECT * FROM f0 JOIN f1 ON b1 = b0 AND w > 1 JOIN f2 ON a2 = a0;
-- works: the same query with f1.w
SELECT * FROM f0 JOIN f1 ON b1 = b0 AND f1.w > 1 JOIN f2 ON a2 = a0;
-- fails: Ambiguous column [name=w] (position 33)
SELECT * FROM f0 LEFT JOIN f1 ON w = a0 JOIN f2 ON a2 = a0;Expected
Both statements resolve w to f1.w, as the qualified form does. A name that more
than one of the tables joined so far has stays an error.
Notes
SqlOptimiser.processJoinConditions()sends INNERONconjuncts to the WHERE clause and equality conjuncts toanalyseEquals(). Both resolve literals throughLiteralCollector/validateColumnAndGetModelIndex(), which searches every table of the FROM clause, not the tables joined so far.- It is an error, not a wrong result. Qualifying the name works around it.
Found while working on #7674.
Source: questdb/questdb