0
0
PostgreSQLquery~10 mins

String aggregation with STRING_AGG in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String aggregation with STRING_AGG
Start Query
Group rows by key
Collect strings per group
Concatenate strings with separator
Return aggregated string per group
End Query
The query groups rows by a key, collects strings from each group, concatenates them with a separator, and returns the result.
Execution Sample
PostgreSQL
SELECT department, STRING_AGG(employee_name, ', ') AS employees
FROM staff
GROUP BY department;
This query groups employees by their department and combines their names into a single comma-separated string per department.
Execution Table
StepActionGroup KeyStrings CollectedConcatenated Result
1Start query execution---
2Group rows by departmentSalesAliceAlice
3Add next employee to Sales groupSalesAlice, BobAlice, Bob
4Group rows by departmentHRCarolCarol
5Add next employee to HR groupHRCarol, DaveCarol, Dave
6Group rows by departmentITEveEve
7Concatenate strings for SalesSalesAlice, BobAlice, Bob
8Concatenate strings for HRHRCarol, DaveCarol, Dave
9Concatenate strings for ITITEveEve
10Return final result set---
💡 All groups processed and strings aggregated, query execution complete.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 9Final
Sales group stringsemptyAlice, BobAlice, BobAlice, BobAlice, Bob
HR group stringsemptyemptyCarol, DaveCarol, DaveCarol, Dave
IT group stringsemptyemptyemptyEveEve
Key Moments - 2 Insights
Why do we need to use GROUP BY with STRING_AGG?
STRING_AGG combines strings per group, so GROUP BY defines which rows belong together. Without GROUP BY, STRING_AGG would combine all rows into one string (see execution_table steps 2 and 4).
What happens if a group has only one string?
STRING_AGG returns that single string without adding the separator (see IT group in execution_table steps 6 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the concatenated result for the HR group after step 5?
ACarol
BCarol, Dave
CDave
DCarol, Bob
💡 Hint
Check the 'Concatenated Result' column for HR group at step 5.
At which step does the Sales group first have two employees collected?
AStep 2
BStep 7
CStep 3
DStep 9
💡 Hint
Look at the 'Strings Collected' column for Sales group in execution_table.
If we remove GROUP BY, how would the final result change?
AAll employee names would be combined into one string
BEach department would have its own aggregated string
CQuery would return an error
DOnly one employee name would be returned
💡 Hint
Recall that GROUP BY defines grouping; without it, STRING_AGG aggregates all rows together.
Concept Snapshot
STRING_AGG(expression, separator) aggregates strings from grouped rows.
Use with GROUP BY to combine values per group.
Returns concatenated string separated by given separator.
If group has one value, returns it as is.
Useful for listing items in one row per group.
Full Transcript
This visual execution traces how STRING_AGG works in PostgreSQL. The query groups rows by department and collects employee names per group. Then it concatenates these names with a comma and space separator. The execution table shows each step: grouping rows, collecting strings, and concatenating them. Variables track the strings collected per group as they grow. Key moments clarify why GROUP BY is needed and what happens with single-item groups. The quiz tests understanding of the aggregation process and effects of removing GROUP BY. The snapshot summarizes syntax and behavior for quick reference.