|
| 1 | +--- |
| 2 | +title: How to filter a ClickHouse table by an array-column? |
| 3 | +description: "Knowledgebase article on how to filter a ClickHouse table by an array-column." |
| 4 | +date: 2024-12-17 |
| 5 | +--- |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +Filtering a ClickHouse table by an array-column is a common task and the product offers a lot of [functions](/docs/en/sql-reference/functions/array-functions) to work with array-columns. |
| 10 | + |
| 11 | +In this article, we're going to focus on filtering a table by an array-column, but the video below covers a lot of other array-related functions: |
| 12 | + |
| 13 | +<iframe width="560" height="315" src="https://www.youtube.com/embed/JKHAdCFtYDg?si=OqS3ry1LFrOlF8Iy" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> |
| 14 | + |
| 15 | +## Example |
| 16 | + |
| 17 | +We'll use an example of a table with two columns `tags_string` and `tags_int` that contain an array of strings and integers respectively. |
| 18 | + |
| 19 | +- Create a sample database and table. |
| 20 | + |
| 21 | +```sql |
| 22 | +CREATE DATABASE db1; |
| 23 | +``` |
| 24 | + |
| 25 | +- Create a sample table |
| 26 | + |
| 27 | +```sql |
| 28 | +CREATE TABLE db1.tags_table |
| 29 | +( |
| 30 | + `id` UInt64, |
| 31 | + `tags_string` Array(String), |
| 32 | + `tags_int` Array(UInt64), |
| 33 | +) |
| 34 | +ENGINE = MergeTree |
| 35 | +ORDER BY id; |
| 36 | +``` |
| 37 | + |
| 38 | +- Insert some sample data into the table. |
| 39 | + |
| 40 | +```sql |
| 41 | +INSERT INTO db1.tags_table VALUES (1, ['tag1', 'tag2', 'tag3'], [1, 2, 3]), (2, ['tag2', 'tag3', 'tag4'], [2, 3, 4]), (3, ['tag1', 'tag3', 'tag5'], [1, 3, 5]); |
| 42 | +``` |
| 43 | + |
| 44 | +Filter the table using the `has(arr, elem)` function to return the rows where the `arr` array contains the `elem` element. |
| 45 | + |
| 46 | +Filter the table to return the rows where the `tags_string` array contains the `tag1` element. |
| 47 | + |
| 48 | +```sql |
| 49 | +SELECT * FROM db1.tags_table WHERE has(tags_string, 'tag1'); |
| 50 | +``` |
| 51 | + |
| 52 | +```text |
| 53 | +┌─id─┬─tags_string────────────┬─tags_int─┐ |
| 54 | +│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │ |
| 55 | +│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │ |
| 56 | +└────┴────────────────────────┴──────────┘ |
| 57 | +``` |
| 58 | + |
| 59 | +Use the `hasAll(arr, elems)` function to return the rows where all the elements in the `elems` array are present in the `arr` array. |
| 60 | + |
| 61 | +Filter the table to return the rows where all the elements in the `tags_string` array are present in the `['tag1', 'tag2']` array. |
| 62 | + |
| 63 | +```sql |
| 64 | +SELECT * FROM db1.tags_table WHERE hasAll(tags_string, ['tag1', 'tag2']); |
| 65 | +``` |
| 66 | + |
| 67 | +```text |
| 68 | +┌─id─┬─tags_string────────────┬─tags_int─┐ |
| 69 | +│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │ |
| 70 | +└────┴────────────────────────┴──────────┘ |
| 71 | +``` |
| 72 | + |
| 73 | +Use the `hasAny(arr, elems)` function to return the rows where at least one element in the `elems` array is present in the `arr` array. |
| 74 | + |
| 75 | +Filter the table to return the rows where at least one element in the `tags_string` array is present in the `['tag1', 'tag2']` array. |
| 76 | + |
| 77 | +```sql |
| 78 | +SELECT * FROM db1.tags_table WHERE hasAny(tags_string, ['tag1', 'tag2']); |
| 79 | +``` |
| 80 | + |
| 81 | +```text |
| 82 | +┌─id─┬─tags_string────────────┬─tags_int─┐ |
| 83 | +│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │ |
| 84 | +│ 2 │ ['tag2','tag3','tag4'] │ [2,3,4] │ |
| 85 | +│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │ |
| 86 | +└────┴────────────────────────┴──────────┘ |
| 87 | +``` |
| 88 | + |
| 89 | +We can use a lambda function to filter the table using the `arrayExists(lambda, arr)` function. |
| 90 | + |
| 91 | +Filter the table to return the rows where at least one element in the `tags_int` array is greater than 3. |
| 92 | + |
| 93 | +```sql |
| 94 | +SELECT * FROM db1.tags_table WHERE arrayExists(x -> x > 3, tags_int); |
| 95 | +``` |
| 96 | + |
| 97 | +```text |
| 98 | +┌─id─┬─tags_string────────────┬─tags_int─┐ |
| 99 | +│ 2 │ ['tag2','tag3','tag4'] │ [2,3,4] │ |
| 100 | +│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │ |
| 101 | +└────┴────────────────────────┴──────────┘ |
| 102 | +``` |
0 commit comments