Challenge - 5 Problems
JSON Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Remember that JSON_AGG with ORDER BY sorts the aggregated values inside the array.
✗ Incorrect
The query groups employees by department and aggregates their names into JSON arrays sorted alphabetically. So HR has ["Alice", "Charlie"] and IT has ["Bob", "David"].
🧠 Conceptual
intermediate1:30remaining
What does JSON_AGG do in PostgreSQL?
Choose the best description of what the
JSON_AGG function does in PostgreSQL.Attempts:
2 left
💡 Hint
Think about how aggregation functions combine multiple rows.
✗ Incorrect
JSON_AGG collects values from multiple rows and returns them as a JSON array.📝 Syntax
advanced2: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)
Attempts:
2 left
💡 Hint
Remember JSON_BUILD_OBJECT creates JSON objects from key-value pairs.
✗ Incorrect
Option A uses JSON_BUILD_OBJECT to create JSON objects per row, then aggregates them into a JSON array per department. Option A aggregates SQL ROW types, which is not JSON. Option A aggregates key-value pairs but requires unique keys and returns a JSON object, not array. Option A uses invalid syntax for JSON object creation.
🔧 Debug
advanced2:00remaining
Why does this JSON_AGG query raise an error?
Consider this query:
Assuming
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?Attempts:
2 left
💡 Hint
Check what columns are allowed in SELECT when using GROUP BY.
✗ Incorrect
The error occurs because
salary is used inside ORDER BY in JSON_AGG but is neither grouped nor aggregated, violating SQL rules.❓ optimization
expert3: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?Attempts:
2 left
💡 Hint
Filtering data early reduces the amount of data JSON_AGG processes.
✗ Incorrect
Pre-filtering with a subquery reduces rows before aggregation, improving performance. CROSS JOIN and window functions here increase data volume or complexity. No filtering causes unnecessary processing.