Finding Missing Values in ClickHouse: Efficient Techniques for Data Comparison

Finding missing values in datasets is a common task in data analysis, especially when comparing two lists or tables to identify discrepancies. In ClickHouse, while there’s no built-in EXCEPT or DIFFERENCE operator like in some other SQL databases, you can achieve similar functionality through a combination of LEFT JOIN, ARRAY JOIN, or subqueries, depending on your specific use case. Below are methods to find missing values between two datasets in ClickHouse:

Method 1: Using LEFT JOIN and WHERE IS NULL

This method is straightforward for comparing two tables or datasets to find rows in one table that don’t have corresponding rows in another based on a key or set of keys.

SELECT a.key_column
FROM table_a a
LEFT JOIN table_b b ON a.key_column = b.key_column
WHERE b.key_column IS NULL;

This query selects all rows from table_a that don’t have matching rows in table_b based on key_column.

Method 2: Using ARRAY JOIN for List Comparisons

If you’re dealing with arrays or lists and want to find elements in one list that aren’t present in another, ARRAY JOIN can be used alongside array functions.

Assuming you have two arrays and want to find elements in array1 that are not in array2:

WITH array1 AS ([1, 2, 3, 4]),
     array2 AS ([3, 4, 5, 6]),
     diff AS arrayDistinct(arrayConcat(array1, array2)) -- Combine and get unique elements
SELECT arrayElement(diff, number) AS missing_values
FROM numbers(1, length(diff))
WHERE arrayElement(diff, number) NOT IN array2

This approach combines both arrays, filters out unique elements, and then uses ARRAY JOIN to filter out elements not present in the second array.

Method 3: Using NOT IN with Subqueries

For simpler cases or when working with scalar values, a NOT IN clause with a subquery can be used to identify missing values.

SELECT key_column
FROM table_a
WHERE key_column NOT IN (SELECT key_column FROM table_b);

This query selects keys from table_a that don’t exist in table_b.

Method 4: Set Operations on Arrays

If you are comparing lists stored as ClickHouse arrays, you can utilize set operations directly on these arrays without converting them to tables.

SELECT arrayDifference(arraySort(array1), arraySort(array2)) AS missing_values
    SELECT [1, 2, 3, 4] AS array1, [3, 4, 5, 6] AS array2

This method sorts both arrays and then computes the difference, effectively finding elements in array1 that are not in array2.


While ClickHouse does not directly support an EXCEPT or DIFFERENCE keyword for finding missing values, the versatile functions and join capabilities it offers allow for efficient implementation of these operations. The choice of method depends on the specific data structures (e.g., tables vs. arrays) and the context of the analysis, enabling flexible and powerful data manipulation and comparison tasks.

About Shiv Iyer 227 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.