📖 Scenario: You are managing a small bookstore database. You want to collect all book titles written by each author into a list.
🎯 Goal: Build a SQL query that groups books by author and uses ARRAY_AGG to collect all book titles for each author into an array.
📋 What You'll Learn
Create a table called books with columns id (integer), author (text), and title (text).
Insert exactly these rows into books: (1, 'Alice Walker', 'The Color Purple'), (2, 'George Orwell', '1984'), (3, 'Alice Walker', 'Meridian'), (4, 'George Orwell', 'Animal Farm').
Write a SQL query that selects author and an array of their title values using ARRAY_AGG.
Group the results by author.
💡 Why This Matters
🌍 Real World
Collecting related items into arrays is useful for reports, summaries, and data analysis in many business applications.
💼 Career
Database developers and analysts often use ARRAY_AGG to aggregate data efficiently for dashboards and applications.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with columns id as integer, author as text, and title as text.
PostgreSQL
Hint
Use CREATE TABLE books (id INTEGER, author TEXT, title TEXT);
2
Insert book records into books
Insert these exact rows into books: (1, 'Alice Walker', 'The Color Purple'), (2, 'George Orwell', '1984'), (3, 'Alice Walker', 'Meridian'), (4, 'George Orwell', 'Animal Farm'). Use four separate INSERT INTO books statements.
PostgreSQL
Hint
Use four INSERT INTO books VALUES (...); statements with the exact values.
3
Write the query using ARRAY_AGG
Write a SQL SELECT statement that selects author and an array of their title values using ARRAY_AGG(title). Use GROUP BY author to group the results by author.
PostgreSQL
Hint
Use SELECT author, ARRAY_AGG(title) AS titles FROM books GROUP BY author;
4
Complete the query with ordering inside ARRAY_AGG
Modify the ARRAY_AGG(title) in your query to order the titles alphabetically by adding ORDER BY title inside the parentheses. The query should still select author and the ordered array of title values grouped by author.
PostgreSQL
Hint
Add ORDER BY title inside ARRAY_AGG() to sort the titles alphabetically.
Practice
(1/5)
1. What does the PostgreSQL function ARRAY_AGG do?
easy
A. It counts the number of rows in a table.
B. It collects multiple row values into a single array.
C. It sorts rows in ascending order.
D. It deletes duplicate rows from a table.
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 B
Hint: ARRAY_AGG(... ORDER BY ...) sorts array elements [OK]
Common Mistakes:
Ignoring ORDER BY inside ARRAY_AGG
Mixing names between groups
Assuming ARRAY_AGG sorts groups, not elements
4. Identify the error in this query:
SELECT class, ARRAY_AGG(name ORDER BY) FROM students GROUP BY class;
medium
A. Missing column name after ORDER BY inside ARRAY_AGG.
B. GROUP BY clause is not allowed with ARRAY_AGG.
C. ARRAY_AGG cannot be used with ORDER BY.
D. Missing FROM keyword before table name.
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 A
Quick Check:
ORDER BY needs a column name inside ARRAY_AGG [OK]
Hint: ORDER BY inside ARRAY_AGG needs a column name [OK]
Common Mistakes:
Leaving ORDER BY empty inside ARRAY_AGG
Thinking GROUP BY disallows ARRAY_AGG
Forgetting to specify sorting column
5. You have a table 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?
hard
A. SELECT customer_id, ARRAY_AGG(product ORDER BY product) FROM orders GROUP BY customer_id;
B. SELECT customer_id, ARRAY_AGG(DISTINCT product) FROM orders GROUP BY customer_id ORDER BY product;
C. SELECT customer_id, ARRAY_AGG(DISTINCT product ORDER BY product) FROM orders GROUP BY customer_id;
D. SELECT customer_id, ARRAY_AGG(product) FROM orders ORDER BY customer_id;
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 C
Quick Check:
Use DISTINCT inside ARRAY_AGG with ORDER BY for unique sorted arrays [OK]
Hint: Use DISTINCT inside ARRAY_AGG with ORDER BY for unique sorted arrays [OK]
Common Mistakes:
Omitting DISTINCT causes duplicates in array
Placing ORDER BY outside ARRAY_AGG affects rows, not array elements
Not grouping by customer_id
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.