SQL
Aggregate Multiple Strings into a Single Column per Group
Discover how to combine multiple string values from a grouped set of rows into a single, comma-separated string using aggregate functions like STRING_AGG or GROUP_CONCAT.
SELECT p.post_id, p.title, STRING_AGG(t.tag_name, ', ') AS tags
FROM Posts p
JOIN PostTags pt ON p.post_id = pt.post_id
JOIN Tags t ON pt.tag_id = t.tag_id
GROUP BY p.post_id, p.title
ORDER BY p.post_id;
How it works: This query joins Posts, PostTags, and Tags tables to link posts with their respective tags. The STRING_AGG function (or GROUP_CONCAT in MySQL) is then used within a GROUP BY clause to concatenate all tag_name values for each post_id into a single, comma-separated string, providing a list of tags per post.