0
0
PostgreSQLquery~20 mins

JSON aggregation with JSON_AGG in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this JSON_AGG query?
Given a table employees with columns id, name, and department, what is the output of the following query?
SELECT department, JSON_AGG(name ORDER BY name) AS employee_names FROM employees GROUP BY department ORDER BY department;
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, department TEXT);
INSERT INTO employees VALUES
(1, 'Alice', 'HR'),
(2, 'Bob', 'IT'),
(3, 'Charlie', 'HR'),
(4, 'David', 'IT'),
(5, 'Eve', 'Finance');

SELECT department, JSON_AGG(name ORDER BY name) AS employee_names FROM employees GROUP BY department ORDER BY department;
A[{"department":"Finance","employee_names":["Eve"]},{"department":"HR","employee_names":["Alice","Charlie"]},{"department":"IT","employee_names":["Bob","David"]}]
B[{"department":"Finance","employee_names":["Eve"]},{"department":"HR","employee_names":["Charlie","Alice"]},{"department":"IT","employee_names":["David","Bob"]}]
C[{"department":"Finance","employee_names":["Eve"]},{"department":"HR","employee_names":["Alice","Charlie"]},{"department":"IT","employee_names":["David","Bob"]}]
D[{"department":"Finance","employee_names":["Eve"]},{"department":"HR","employee_names":["Charlie","Alice"]},{"department":"IT","employee_names":["Bob","David"]}]
Attempts:
2 left
💡 Hint
Remember that JSON_AGG with ORDER BY sorts the aggregated values inside the array.
🧠 Conceptual
intermediate
1:30remaining
What does JSON_AGG do in PostgreSQL?
Choose the best description of what the JSON_AGG function does in PostgreSQL.
AAggregates JSON objects into a single JSON object by merging keys.
BAggregates multiple rows into a single JSON array.
CConverts a JSON array into multiple rows.
DConverts a JSON string into a JSON object.
Attempts:
2 left
💡 Hint
Think about how aggregation functions combine multiple rows.
📝 Syntax
advanced
2:30remaining
Which query correctly aggregates JSON objects per department?
You want to aggregate employee data as JSON objects per department with keys id and name. Which query is correct?
PostgreSQL
Table employees(id INT, name TEXT, department TEXT)
ASELECT department, JSON_AGG(JSON_BUILD_OBJECT('id', id, 'name', name)) AS employees FROM employees GROUP BY department;
BSELECT department, JSON_AGG(ROW(id, name)) AS employees FROM employees GROUP BY department;
CSELECT department, JSON_OBJECT_AGG(id, name) AS employees FROM employees GROUP BY department;
DSELECT department, JSON_AGG({'id': id, 'name': name}) AS employees FROM employees GROUP BY department;
Attempts:
2 left
💡 Hint
Remember JSON_BUILD_OBJECT creates JSON objects from key-value pairs.
🔧 Debug
advanced
2:00remaining
Why does this JSON_AGG query raise an error?
Consider this query:
SELECT department, JSON_AGG(name ORDER BY salary) FROM employees GROUP BY department;

Assuming salary is a column in employees, why does this query raise an error?
ABecause ORDER BY inside JSON_AGG requires a subquery.
BBecause JSON_AGG does not support ORDER BY inside its parentheses.
CBecause <code>name</code> is not a valid column in employees.
DBecause <code>salary</code> is not in the GROUP BY clause or an aggregate function.
Attempts:
2 left
💡 Hint
Check what columns are allowed in SELECT when using GROUP BY.
optimization
expert
3:00remaining
How to optimize JSON_AGG for large datasets?
You have a large sales table with millions of rows. You want to aggregate sales data per region into JSON arrays using JSON_AGG. Which approach is best to optimize performance?
AUse a CROSS JOIN to combine all regions and sales before aggregation.
BUse JSON_AGG without any filtering or indexing to get all data at once.
CUse a subquery to pre-filter rows by date before applying JSON_AGG.
DUse JSON_AGG inside a window function partitioned by region.
Attempts:
2 left
💡 Hint
Filtering data early reduces the amount of data JSON_AGG processes.