0
0
PostgreSQLquery~10 mins

Array aggregation with ARRAY_AGG in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select all user IDs aggregated into an array.

PostgreSQL
SELECT [1](user_id) FROM users;
Drag options to blanks, or click blank then click option'
AARRAY_AGG
BCOUNT
CMAX
DSUM
Attempts:
3 left
💡 Hint
Common Mistakes
Using COUNT instead of ARRAY_AGG returns a number, not an array.
Using SUM or MAX returns a single value, not an array.
2fill in blank
medium

Complete the code to aggregate product names into an array grouped by category.

PostgreSQL
SELECT category, [1](product_name) FROM products GROUP BY category;
Drag options to blanks, or click blank then click option'
AAVG
BCOUNT
CMIN
DARRAY_AGG
Attempts:
3 left
💡 Hint
Common Mistakes
Using COUNT returns the number of products, not their names.
Using AVG or MIN does not work on text fields.
3fill in blank
hard

Fix the error in the code to aggregate emails into an array.

PostgreSQL
SELECT ARRAY_AGG[1] FROM contacts;
Drag options to blanks, or click blank then click option'
A(DISTINCT email)
B[
C{
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Missing parentheses causes syntax errors.
Using brackets or braces is invalid syntax.
4fill in blank
hard

Complete the code to aggregate distinct tags into an array grouped by post ID.

PostgreSQL
SELECT post_id, ARRAY_AGG[1]tags FROM post_tags GROUP BY post_id;
Drag options to blanks, or click blank then click option'
A(DISTINCT
B)
C;
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to close parentheses causes errors.
Using semicolon or brackets instead of closing parenthesis is invalid.
5fill in blank
hard

Fill all three blanks to aggregate user names in uppercase into an array grouped by city.

PostgreSQL
SELECT city, ARRAY_AGG([1]([2])) AS users FROM users GROUP BY [3];
Drag options to blanks, or click blank then click option'
AUPPER
Buser_name
Ccity
DLOWER
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOWER instead of UPPER changes case incorrectly.
Grouping by user_name instead of city changes the grouping.