0
0
PostgreSQLquery~5 mins

Array aggregation with ARRAY_AGG in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ARRAY_AGG function do in PostgreSQL?
ARRAY_AGG collects values from multiple rows into a single array.
Click to reveal answer
beginner
How do you use ARRAY_AGG to get all employee names in one array from a table named employees?
Use: SELECT ARRAY_AGG(name) FROM employees;
Click to reveal answer
intermediate
Can ARRAY_AGG be combined with GROUP BY? If yes, why?
Yes, to collect values into arrays grouped by a column, like departments.
Click to reveal answer
intermediate
What happens if you use ARRAY_AGG on a column with NULL values?
NULL values are included in the resulting array unless filtered out.
Click to reveal answer
advanced
How can you order elements inside the array created by ARRAY_AGG?
Use ORDER BY inside ARRAY_AGG, like ARRAY_AGG(name ORDER BY name ASC).
Click to reveal answer
What is the main purpose of ARRAY_AGG in PostgreSQL?
ATo sort rows alphabetically
BTo count rows in a table
CTo delete duplicate rows
DTo combine multiple row values into an array
Which SQL clause is commonly used with ARRAY_AGG to group results?
AWHERE
BGROUP BY
CORDER BY
DHAVING
How do you exclude NULL values from the array created by ARRAY_AGG?
AUse WHERE column IS NOT NULL before aggregation
BUse ARRAY_AGG with NULLS EXCLUDED option
CUse DISTINCT inside ARRAY_AGG
DNULLs are always excluded by default
How can you sort the elements inside the array returned by ARRAY_AGG?
AUse ORDER BY outside the query
BSort after query in application code only
CUse ORDER BY inside ARRAY_AGG function
DARRAY_AGG does not support sorting
What type of data does ARRAY_AGG return?
AAn array of values
BA JSON object
CA single scalar value
DA table
Explain how ARRAY_AGG works and give an example query using it.
Think about gathering many values into one list.
You got /3 concepts.
    Describe how to handle NULL values and ordering when using ARRAY_AGG.
    Consider filtering and sorting inside the aggregation.
    You got /3 concepts.