// aggregates reference

SQL ANY_VALUE function

Returns one arbitrary value from the group, bypassing full GROUP BY validation.

カテゴリ
Aggregates
Returns
same as input
In this category
40 of 52

構文

ANY_VALUE(x)

エンジンサポート

エンジンSupportメモ
SQL Server Not built in No built-in equivalent.
PostgreSQL はい PostgreSQL 16+
MySQL はい Supported under this name.
SQLite Not built in No built-in equivalent.
Oracle はい 19c+

例

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;

知っておくと便利なこと

  • 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.

Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle.

// fewer lookups

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.