2020-06-18 08:24:31 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/any
2024-06-24 11:52:30 +00:00
sidebar_position: 102
2020-06-18 08:24:31 +00:00
---
2022-06-02 10:55:18 +00:00
# any
2020-06-18 08:24:31 +00:00
2024-11-17 10:10:51 +00:00
Selects the first encountered value of a column.
:::warning
As a query can be executed in arbitrary order, the result of this function is non-deterministic.
If you need an arbitrary but deterministic result, use functions [`min` ](../reference/min.md ) or [`max` ](../reference/max.md ).
:::
By default, the function never returns NULL, i.e. ignores NULL values in the input column.
However, if the function is used with the `RESPECT NULLS` modifier, it returns the first value reads no matter if NULL or not.
2023-11-27 18:29:20 +00:00
2024-05-04 14:20:58 +00:00
**Syntax**
2023-11-27 18:29:20 +00:00
2024-05-04 14:20:58 +00:00
```sql
2024-07-12 10:50:19 +00:00
any(column) [RESPECT NULLS]
2024-05-04 14:20:58 +00:00
```
2024-11-17 10:10:51 +00:00
Aliases `any(column)` (without `RESPECT NULLS` )
- `any_value`
- [`first_value` ](../reference/first_value.md ).
Alias for `any(column) RESPECT NULLS`
- `anyRespectNulls` , `any_respect_nulls`
- `firstValueRespectNulls` , `first_value_respect_nulls`
- `anyValueRespectNulls` , `any_value_respect_nulls`
2024-05-04 14:20:58 +00:00
**Parameters**
2024-11-17 10:10:51 +00:00
- `column` : The column name.
2024-05-04 14:20:58 +00:00
**Returned value**
2024-11-17 10:10:51 +00:00
The first value encountered.
2024-05-04 14:20:58 +00:00
:::note
2024-11-17 10:10:51 +00:00
The return type of the function is the same as the input, except for LowCardinality which is discarded.
This means that given no rows as input it will return the default value of that type (0 for integers, or Null for a Nullable() column).
You might use the `-OrNull` [combinator ](../../../sql-reference/aggregate-functions/combinators.md ) ) to modify this behaviour.
2024-05-04 14:20:58 +00:00
:::
**Implementation details**
2020-06-18 08:24:31 +00:00
2024-11-17 10:10:51 +00:00
In some cases, you can rely on the order of execution.
This applies to cases when `SELECT` comes from a subquery that uses `ORDER BY` .
2020-06-18 08:24:31 +00:00
2024-11-17 10:10:51 +00:00
When a `SELECT` query has the `GROUP BY` clause or at least one aggregate function, ClickHouse (in contrast to MySQL) requires that all expressions in the `SELECT` , `HAVING` , and `ORDER BY` clauses be calculated from keys or from aggregate functions.
In other words, each column selected from the table must be used either in keys or inside aggregate functions.
To get behavior like in MySQL, you can put the other columns in the `any` aggregate function.
2023-07-16 05:23:15 +00:00
2024-05-04 14:20:58 +00:00
**Example**
Query:
```sql
2024-11-17 10:10:51 +00:00
CREATE TABLE tab (city Nullable(String)) ENGINE=Memory;
2024-05-04 14:20:58 +00:00
2024-11-17 10:10:51 +00:00
INSERT INTO tab (city) VALUES (NULL), ('Amsterdam'), ('New York'), ('Tokyo'), ('Valencia'), (NULL);
2024-05-04 14:20:58 +00:00
2024-11-17 10:10:51 +00:00
SELECT any(city), anyRespectNulls(city) FROM tab;
2024-05-04 14:20:58 +00:00
```
```response
2024-11-16 23:49:35 +00:00
┌─any(city)─┬─anyRespectNulls(city)─┐
│ Amsterdam │ ᴺᵁᴸᴸ │
└───────────┴───────────────────────┘
2024-05-04 14:20:58 +00:00
```