SQL ANY_VALUE function
Returns one arbitrary value from the group, bypassing full GROUP BY validation.
- Kategorie
- Aggregates
- Returns
same as input- In this category
- 40 of 52
Syntax
ANY_VALUE(x)
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Ja | PostgreSQL 16+ |
| MySQL | Ja | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Ja | 19c+ |
Beispiele
MySQL
SELECT customer_id, ANY_VALUE(region) FROM orders GROUP BY customer_id;
PostgreSQL
SELECT customer_id, any_value(region) FROM orders GROUP BY customer_id;
Gut zu wissen
- The engine picks the value: it is arbitrary and can differ between runs, so never build logic on which row it came from.
- MySQL added it to escape ONLY_FULL_GROUP_BY errors; PostgreSQL 16 and Oracle 19c+ followed.
- PostgreSQL and Oracle skip NULLs and return NULL only for an all-NULL group. MySQL's ANY_VALUE is not a real aggregate: it can return NULL even when the group has a non-NULL value.
- Older engines: MIN(x), MAX(x), a window FIRST_VALUE, or PostgreSQL's DISTINCT ON.
Questions about ANY_VALUE
Is ANY_VALUE deterministic?
No. It returns some value from the group; which one is not guaranteed. SQL Server has no ANY_VALUE; use MIN or MAX there.
What is ONLY_FULL_GROUP_BY?
MySQL's mode of rejecting selected columns that are neither grouped nor aggregated; ANY_VALUE declares one column exempt.
What replaces it on PostgreSQL 15 or older?
MIN, MAX, FIRST_VALUE over a partition, or DISTINCT ON.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.