0
0
PostgreSQLquery~20 mins

Array aggregation with ARRAY_AGG in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array 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 ARRAY_AGG query?

Given the table orders with columns customer_id and product, what is the result of this query?

SELECT customer_id, ARRAY_AGG(product ORDER BY product) AS products
FROM orders
GROUP BY customer_id
ORDER BY customer_id;
PostgreSQL
CREATE TABLE orders (customer_id INT, product TEXT);
INSERT INTO orders VALUES
(1, 'apple'), (1, 'banana'), (2, 'carrot'), (2, 'apple'), (3, 'banana');
A[{"customer_id":1,"products":["banana"]},{"customer_id":2,"products":["apple","carrot"]},{"customer_id":3,"products":["banana"]}]
B[{"customer_id":1,"products":["apple","banana"]},{"customer_id":2,"products":["apple","carrot"]},{"customer_id":3,"products":["banana"]}]
C[{"customer_id":1,"products":["apple","banana"]},{"customer_id":2,"products":["carrot"]},{"customer_id":3,"products":["banana"]}]
D[{"customer_id":1,"products":["banana","apple"]},{"customer_id":2,"products":["carrot","apple"]},{"customer_id":3,"products":["banana"]}]
Attempts:
2 left
💡 Hint

Remember that ARRAY_AGG collects values into an array, and ORDER BY inside it sorts the array elements.

📝 Syntax
intermediate
1:30remaining
Which ARRAY_AGG usage is syntactically correct?

Choose the option with correct syntax for using ARRAY_AGG with ordering.

ASELECT ARRAY_AGG(product ORDER BY product) FROM orders;
BSELECT ARRAY_AGG(ORDER BY product) FROM orders;
CSELECT ARRAY_AGG(product, ORDER BY product) FROM orders;
DSELECT ARRAY_AGG(product) ORDER BY product FROM orders;
Attempts:
2 left
💡 Hint

Look for the correct placement of ORDER BY inside the ARRAY_AGG function.

optimization
advanced
2:30remaining
How to optimize ARRAY_AGG for distinct values?

You want to aggregate unique product names per customer into an array. Which query is the most efficient?

ASELECT customer_id, ARRAY_AGG(product) FROM (SELECT DISTINCT customer_id, product FROM orders) sub GROUP BY customer_id;
BSELECT customer_id, ARRAY(SELECT DISTINCT product FROM orders WHERE orders.customer_id = o.customer_id) FROM orders o GROUP BY customer_id;
CSELECT customer_id, ARRAY_AGG(DISTINCT product) FROM orders GROUP BY customer_id;
DSELECT customer_id, ARRAY_AGG(product ORDER BY product) FROM orders GROUP BY customer_id;
Attempts:
2 left
💡 Hint

PostgreSQL supports DISTINCT inside ARRAY_AGG to remove duplicates efficiently.

🧠 Conceptual
advanced
1:30remaining
What does ARRAY_AGG return when no rows match?

Consider this query with a filter that matches no rows:

SELECT ARRAY_AGG(product) FROM orders WHERE customer_id = 999;

What is the result?

ANULL
BAn empty array {}
CAn array with one NULL element [NULL]
DAn error is raised
Attempts:
2 left
💡 Hint

Think about aggregate functions behavior when no rows are aggregated.

🔧 Debug
expert
2:00remaining
Why does this ARRAY_AGG query fail?

Given the query:

SELECT customer_id, ARRAY_AGG(product ORDER BY quantity) FROM orders GROUP BY customer_id;

Assuming quantity is not in the orders table, what error will PostgreSQL raise?

AERROR: aggregate function calls cannot contain ORDER BY
BNo error, query runs successfully
CERROR: syntax error at or near "ORDER"
DERROR: column "quantity" does not exist
Attempts:
2 left
💡 Hint

Check if all columns used inside ARRAY_AGG exist in the table.