The "DB::Exception: Unknown JOIN type" error in ClickHouse is triggered when you use a JOIN keyword or type that ClickHouse does not recognize. This typically happens due to a typo in the join syntax or an attempt to use a join type from another SQL dialect that ClickHouse does not support. The error code for this is UNKNOWN_JOIN.
Impact
The query is immediately rejected at the parsing or analysis stage. No data processing occurs. This is a straightforward syntax-level error that is easy to identify and fix.
Common Causes
- Typo in the JOIN keyword -- writing
INNERJOIN,LEFFT JOIN, or other misspellings. - Using a JOIN type from another database -- some databases support
NATURAL JOIN,LATERAL JOIN, or other types that ClickHouse may not recognize or may handle differently. - Incorrect strictness keyword -- using
SEMIorANTIin the wrong position or combining them incorrectly with join types. - Version mismatch -- using a join type introduced in a newer version of ClickHouse than what is running.
- Missing spaces in the JOIN clause -- writing
LEFTJOINinstead ofLEFT JOIN.
Troubleshooting and Resolution Steps
Check the spelling of your JOIN type. Valid join types in ClickHouse include:
-- Standard types [INNER] JOIN LEFT [OUTER] JOIN RIGHT [OUTER] JOIN FULL [OUTER] JOIN CROSS JOIN -- ClickHouse-specific strictness [ANY|ALL] [INNER|LEFT|RIGHT|FULL] JOIN [SEMI|ANTI] [LEFT|RIGHT] JOIN ASOF [LEFT] JOINEnsure proper spacing between keywords:
-- Correct SELECT * FROM a LEFT JOIN b ON a.id = b.id; -- Wrong: missing space SELECT * FROM a LEFTJOIN b ON a.id = b.id;Replace unsupported join types with ClickHouse equivalents:
-- NATURAL JOIN is not standard in ClickHouse; use USING instead SELECT * FROM a JOIN b USING (id); -- LATERAL JOIN: use array joins or correlated subqueries insteadVerify strictness and type ordering. The strictness modifier comes before the join type:
-- Correct order SELECT * FROM a ANY LEFT JOIN b ON a.id = b.id; SELECT * FROM a SEMI LEFT JOIN b ON a.id = b.id; -- Wrong order SELECT * FROM a LEFT ANY JOIN b ON a.id = b.id;Check your ClickHouse version for support:
SELECT version();
Best Practices
- Refer to the ClickHouse documentation for the exact syntax of supported JOIN types in your version.
- Use your SQL editor's syntax highlighting to catch misspellings early.
- When migrating queries from other databases, review join syntax for ClickHouse compatibility.
- Stick to well-tested join types (
INNER,LEFT,RIGHT,CROSS) unless you specifically need ClickHouse's extended join strictness modifiers.
Frequently Asked Questions
Q: Does ClickHouse support NATURAL JOIN?
A: ClickHouse does not support NATURAL JOIN syntax. Use JOIN ... USING (column1, column2) instead, which achieves a similar result by matching on columns with the same name.
Q: What is the difference between ANY and ALL in a join?
A: ALL is the default and returns all matching rows from the right table (standard SQL behavior). ANY returns only the first matching row from the right table, which can be faster when you know there is at most one match or only need one.
Q: Is ASOF JOIN a standard SQL feature?
A: No, ASOF JOIN is a ClickHouse extension (also found in some time-series databases). It matches on the closest value that is less than or equal to the key from the left table, which is useful for joining time-series data with different granularity.
Q: Can I use SEMI JOIN in ClickHouse?
A: Yes, ClickHouse supports SEMI LEFT JOIN and SEMI RIGHT JOIN. A SEMI join returns rows from one table that have at least one match in the other table, without duplicating rows.