-- Created on savesnippets.com · https://savesnippets.com/0tkpAMxyybjEvp -- PostgreSQL / SQL Server (string_agg) SELECT user_id, STRING_AGG(name, ', ' ORDER BY name) AS tag_list FROM user_tags GROUP BY user_id; -- MySQL (group_concat) — same idea, slightly different syntax SELECT user_id, GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') AS tag_list FROM user_tags GROUP BY user_id; -- DISTINCT + custom separator + sort SELECT order_id, STRING_AGG(DISTINCT product_name, ' | ' ORDER BY product_name) FROM order_items GROUP BY order_id; -- ⚠️ MySQL truncates at group_concat_max_len (default 1024). -- For long lists: SET SESSION group_concat_max_len = 1000000;