Collects values from multiple rows into an ARRAY. A powerful BigQuery function for building nested data structures.
ARRAY<T>Diperbarui: 13 Jun 2026ARRAY_AGG([DISTINCT] expression [IGNORE NULLS | RESPECT NULLS] [ORDER BY key] [LIMIT n])A column or expression to collect into an array
Collect only unique values
Exclude NULL values (default behavior)
Include NULL values in the array
Sort elements within the array
Limit the number of elements in the array
1 SELECT 2 customer_id, 3 ARRAY_AGG(product_id) as product_ids 4 FROM `project.dataset.orders` 5 GROUP BY customer_id 6 LIMIT 3;
Creates an array of product IDs per customer.
| customer_id | product_ids |
|---|---|
| C001 | [101, 205, 342] |
| C002 | [101, 156] |
| C003 | [205, 342, 401, 502] |
1 SELECT 2 category, 3 ARRAY_AGG(product_name ORDER BY sales_count DESC LIMIT 3) as top_3_products 4 FROM `project.dataset.products` 5 GROUP BY category;
Retrieves the top 3 best-selling products per category.
| category | top_3_products |
|---|---|
| Electronics | ["iPhone", "MacBook", "iPad"] |
| Fashion | ["Nike Shoes", "Levi's Jeans", "Zara Dress"] |
1 SELECT 2 department, 3 ARRAY_AGG( 4 STRUCT(employee_name, salary, hire_date) 5 ORDER BY salary DESC 6 LIMIT 5 7 ) as top_paid_employees 8 FROM `project.dataset.employees` 9 GROUP BY department;
Creates an array of structs for the top 5 highest-paid employees.
| department | top_paid_employees |
|---|---|
| Engineering | [{name: "Alice", salary: 25M, ...}, ...] |
1 SELECT 2 user_id, 3 ARRAY_AGG(DISTINCT category ORDER BY category) as unique_categories 4 FROM `project.dataset.user_activity` 5 GROUP BY user_id;
Collects unique categories visited by each user.
Sudah paham ARRAY_AGG? Latih langsung di browser
Latihan interaktif, langsung di browser.
| user_id | unique_categories |
|---|---|
| U001 | ["Books", "Electronics", "Sports"] |
| U002 | ["Fashion", "Food", "Home"] |