ARRAY_AGG helps you collect multiple values from rows into a single list. It makes it easy to see related data together.
Array aggregation with ARRAY_AGG in PostgreSQL
ARRAY_AGG(expression [ORDER BY expression])
The expression is the column or value you want to collect into an array.
You can use ORDER BY inside ARRAY_AGG to sort the values in the array.
SELECT department, ARRAY_AGG(employee_name) FROM employees GROUP BY department;
SELECT department, ARRAY_AGG(employee_name ORDER BY employee_name) FROM employees GROUP BY department;
SELECT ARRAY_AGG(product_name) FROM products WHERE category = 'Books';
This example creates an orders table, adds some orders, then groups products bought by each customer into arrays sorted alphabetically.
CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, customer_name TEXT, product_name TEXT ); INSERT INTO orders (customer_name, product_name) VALUES ('Alice', 'Book'), ('Alice', 'Pen'), ('Bob', 'Notebook'), ('Bob', 'Pen'), ('Bob', 'Eraser'); SELECT customer_name, ARRAY_AGG(product_name ORDER BY product_name) AS products_bought FROM orders GROUP BY customer_name ORDER BY customer_name;
ARRAY_AGG runs in O(n) time where n is the number of rows in the group.
It uses extra space to store the array for each group.
Common mistake: forgetting to use GROUP BY when aggregating, which causes errors or unexpected results.
Use ARRAY_AGG when you want to collect multiple values into one list. Use string_agg if you want a single string instead.
ARRAY_AGG collects multiple row values into a single array.
You can sort the array elements using ORDER BY inside ARRAY_AGG.
It is useful for grouping related data together in one row.
Practice
ARRAY_AGG do?Solution
Step 1: Understand the purpose of ARRAY_AGG
ARRAY_AGG is used to gather multiple values from rows into one array value.Step 2: Compare with other options
Counting rows uses COUNT(), deleting duplicates or sorting rows uses other clauses like DISTINCT or ORDER BY, not ARRAY_AGG.Final Answer:
It collects multiple row values into a single array. -> Option BQuick Check:
ARRAY_AGG = collects rows into array [OK]
- Confusing ARRAY_AGG with COUNT or ORDER BY
- Thinking ARRAY_AGG sorts data by default
- Assuming ARRAY_AGG removes duplicates automatically
ARRAY_AGG in PostgreSQL?Solution
Step 1: Recall correct function call syntax
ARRAY_AGG requires parentheses around the column name and a FROM clause.Step 2: Check each option
SELECT ARRAY_AGG(column_name) FROM table_name; uses correct syntax with parentheses and FROM clause; others miss parentheses or have syntax errors.Final Answer:
SELECT ARRAY_AGG(column_name) FROM table_name; -> Option AQuick Check:
Correct ARRAY_AGG syntax uses parentheses and FROM [OK]
- Omitting parentheses around column_name
- Leaving incomplete WHERE clause
- Missing FROM keyword
students with columns class and name, what is the output of this query?SELECT class, ARRAY_AGG(name ORDER BY name) FROM students GROUP BY class ORDER BY class;
Solution
Step 1: Understand GROUP BY and ARRAY_AGG with ORDER BY
The query groups rows by class and collects names into arrays sorted alphabetically.Step 2: Analyze the ordering inside ARRAY_AGG
Names inside each class array are sorted ascending, so 'Alice' comes before 'Bob' in '1A', and 'Charlie' before 'David' in '2B'.Final Answer:
[{class: '1A', array_agg: ['Alice', 'Bob']}, {class: '2B', array_agg: ['Charlie', 'David']}] -> Option DQuick Check:
ARRAY_AGG with ORDER BY sorts array elements [OK]
- Ignoring ORDER BY inside ARRAY_AGG
- Mixing names between groups
- Assuming ARRAY_AGG sorts groups, not elements
SELECT class, ARRAY_AGG(name ORDER BY) FROM students GROUP BY class;
Solution
Step 1: Check syntax of ORDER BY inside ARRAY_AGG
ORDER BY inside ARRAY_AGG requires a column name to specify sorting order.Step 2: Validate other parts of the query
GROUP BY is valid with ARRAY_AGG; FROM clause is present; ORDER BY without column is invalid.Final Answer:
Missing column name after ORDER BY inside ARRAY_AGG. -> Option AQuick Check:
ORDER BY needs a column name inside ARRAY_AGG [OK]
- Leaving ORDER BY empty inside ARRAY_AGG
- Thinking GROUP BY disallows ARRAY_AGG
- Forgetting to specify sorting column
orders with columns customer_id and product. You want to create a query that returns each customer_id with an array of unique products they ordered, sorted alphabetically. Which query achieves this?Solution
Step 1: Use ARRAY_AGG with DISTINCT and ORDER BY
To get unique products sorted, use ARRAY_AGG(DISTINCT product ORDER BY product).Step 2: Group by customer_id to aggregate per customer
GROUP BY customer_id ensures aggregation per customer; ORDER BY inside ARRAY_AGG sorts products alphabetically.Final Answer:
SELECT customer_id, ARRAY_AGG(DISTINCT product ORDER BY product) FROM orders GROUP BY customer_id; -> Option CQuick Check:
Use DISTINCT inside ARRAY_AGG with ORDER BY for unique sorted arrays [OK]
- Omitting DISTINCT causes duplicates in array
- Placing ORDER BY outside ARRAY_AGG affects rows, not array elements
- Not grouping by customer_id
Ready to prove it?
Pass the test and this pattern moves unknown
