// aggregates reference
SQL JSON_GROUP_ARRAY function
SQLite's pair of JSON aggregates that collect group values into arrays and objects.
- カテゴリ
- Aggregates
- Returns
json- In this category
- 19 of 52
構文
json_group_array(x) | json_group_array(x ORDER BY x) | json_group_object(k, v)
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | はい | Supported under this name. |
| Oracle | Not built in | No built-in equivalent. |
例
SQLite
SELECT post_id, json_group_array(tag ORDER BY tag) FROM post_tags GROUP BY post_id;
SQLite
SELECT json_group_object(code, label) FROM status_codes;
知っておくと便利なこと
- Without ORDER BY the element order is arbitrary; SQLite 3.44+ accepts ORDER BY inside the aggregate, as in json_group_array(x ORDER BY x).
- NULL values become JSON null elements, they do not disappear.
- json_group_object keeps duplicate keys as separate pairs and silently drops pairs whose key is NULL.
- Elsewhere: PostgreSQL json_agg, MySQL and Oracle JSON_ARRAYAGG, SQL Server 2025 JSON_ARRAYAGG or FOR JSON PATH.
Questions about JSON_GROUP_ARRAY
How do I keep the array sorted?
Use ORDER BY inside the aggregate (SQLite 3.44+) or aggregate over a subquery that is already ordered.
What is the MySQL equivalent?
JSON_ARRAYAGG(x): same idea, standard name.
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: SQLite.
// 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.