Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Use string_agg() in PostgreSQL
The `string_agg()` function in PostgreSQL is a powerful tool for concatenating values from multiple rows into a single string. It is often used when you need to combine values from different rows of a table into a single string result.
Let's dive into how to use this function with some real-world examples.
Syntax
string_agg(expression, delimiter)
The `string_agg()` function takes two arguments:
- expression: The column or expression whose values you want to aggregate into a string.
- delimiter: The separator string that will be placed between the aggregated values.
Example 1: Basic Usage
Let's say we have a table called employees
with the following data:
+----+------------+
| id | name |
+----+------------+
| 1 | John |
| 2 | Jane |
| 3 | Bob |
+----+------------+
We can use string_agg()
to concatenate all the employee names into a single string:
SELECT string_agg(name, ', ') FROM employees;
The result will be:
John, Jane, Bob
Example 2: Using string_agg() with GROUP BY
In some cases, you might want to group the results by another column. For example, if we had a table of orders and wanted to list all the items for each order, we could use the following query:
SELECT order_id, string_agg(item_name, ', ')
FROM orders
GROUP BY order_id;
This would give us a concatenated list of items for each order.
Advanced Usage: Ordering the Aggregated Values
You can also specify the order in which the values are concatenated. This can be done by adding an ORDER BY
clause within the string_agg()
function.
SELECT string_agg(name, ', ' ORDER BY name ASC) FROM employees;
The result will be:
Bob, Jane, John
Conclusion
The string_agg()
function is an excellent tool for aggregating and concatenating values in PostgreSQL. Whether you're working with simple data or complex groupings, this function can help you consolidate your results into a single string, making your queries more effective.