Stop 05 · Aggregation & GroupingTopics · 8/9

String Aggregation (STRING_AGG, GROUP_CONCAT)

Part of the SQL Roadmap

In short

Sometimes the answer isn't a number but a list: every product in an order, every city a customer has shopped from. String aggregation squeezes many rows into one readable text value.

String aggregation joins values from many rows into one text value per group, such as all products in an order as a comma-separated list. PostgreSQL and SQL Server use STRING_AGG, MySQL uses GROUP_CONCAT.

The jaadu sits cross-legged outside a temple, threading orange and yellow marigolds from a woven basket onto one long garland draped across its lap.

Think of it like this

Stringing a marigold garland

A flower seller outside a temple has a basket of loose marigolds. She threads them one by one onto a single string, with a small knot between each. Many separate flowers become one garland. STRING_AGG threads many row values into one string, with the separator you choose as the knot.

Watch it happen

Three item rows become one list

Order 101 · Toor dal
0
Order 101 · Atta 5kg
1
Order 101 · Ghee
2
Order 102 · Basmati rice
3

1/4Order 101 has three item rows. Order 102 has one.

The big ideas

What you need to know

Many rows, one string

STRING_AGG(name, ', ') is an aggregate like SUM, but it joins text instead of adding numbers.

Order inside the list

Without ORDER BY inside the function, the list order is unpredictable. Add it every time.

DISTINCT works too

STRING_AGG(DISTINCT city, ', ') lists each city once, even if a customer ordered from it ten times.

Different names

PostgreSQL and SQL Server use STRING_AGG. MySQL uses GROUP_CONCAT(name ORDER BY name SEPARATOR ', ').

See it in code

Walk through the code

sql
1SELECT2  o.order_id,3  c.name AS customer,4  COUNT(*) AS item_lines,5  STRING_AGG(p.name, ', ' ORDER BY p.name) AS items6FROM orders o7JOIN customers c    ON c.customer_id = o.customer_id8JOIN order_items oi ON oi.order_id   = o.order_id9JOIN products p     ON p.product_id  = oi.product_id10WHERE o.order_date = DATE '2026-11-14'11GROUP BY o.order_id, c.name12ORDER BY o.order_id;

1/3Join product names with a comma and space, sorted alphabetically.

Every order from Children's Day with its items in one column. SQL Server writes STRING_AGG(p.name, ', ') WITHIN GROUP (ORDER BY p.name). MySQL uses GROUP_CONCAT, which cuts lists at 1,024 characters by default.

Save yourself the pain

Common mistakes

Oops

STRING_AGG(product_id, ', ') on an integer column in PostgreSQL.

Do this instead

STRING_AGG needs text. Cast first: STRING_AGG(product_id::text, ', ').

Oops

Trusting a long GROUP_CONCAT list in MySQL.

Do this instead

It is silently cut at group_concat_max_len (1,024 by default). Raise it with SET SESSION group_concat_max_len = 100000.

Oops

Storing comma lists and filtering them later with LIKE.

Do this instead

String aggregation is for reading and exporting. Keep the data in rows when you still need to analyse it.

Your mission

Customer city trail

~15 min

Quick check

What does STRING_AGG(DISTINCT city, ' | ' ORDER BY city) return for rows Pune, Goa, Pune?

Go deeper

  1. 1List every order with its product names in one column.
  2. 2Control the order of items inside the list with ORDER BY inside the function.
  3. 3Check MySQL's group_concat_max_len limit, which truncates long lists silently.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge