Complete the code to aggregate all employee names into a JSON array using JSON_AGG.
SELECT JSON_AGG([1]) AS employee_names FROM employees;The JSON_AGG function collects values into a JSON array. Here, we want to aggregate the name column.
Complete the code to aggregate employee names grouped by their department.
SELECT department, JSON_AGG([1]) AS employees FROM employees GROUP BY department;We want to collect employee names per department, so name is the correct column to aggregate.
Fix the error in the code to aggregate employee names as JSON objects with their salaries.
SELECT JSON_AGG(JSON_BUILD_OBJECT('name', name, 'salary', [1])) AS employee_info FROM employees;
The JSON_BUILD_OBJECT needs the salary value for the 'salary' key, so the correct column is salary.
Fill both blanks to aggregate employee names and salaries grouped by department as JSON arrays.
SELECT department, JSON_AGG(JSON_BUILD_OBJECT('name', [1], 'salary', [2])) AS employees FROM employees GROUP BY department;
The first blank is the employee name, the second is salary. Grouping should be by department, but the blank for grouping is not present here, so only fill the first two blanks.
Fill all three blanks to create a JSON object with department as key and aggregated employee names as value.
SELECT JSON_OBJECT_AGG([1], [2]) AS department_employees FROM (SELECT [3], JSON_AGG(name) AS names FROM employees GROUP BY [3]) sub;
The JSON_OBJECT_AGG needs the department as key and the aggregated names as value. The subquery groups by department.