Menu

How to concatenate multiple rows into one field in MySQL?

Written by Selva Prabhakaran | 2 min read

Problem

You have a table with multiple rows of data for each unique identifier and you want to concatenate the values of these rows into a single field for each identifier.

Input

user_id interest
1 Reading
1 Writing
2 Painting
2 Singing
3 Traveling

Try Hands-On: Fiddle

Create Table: Gist

Desired Output

user_id concatenated_interests
1 Reading,Writing
2 Painting,Singing
3 Traveling

Solution 1:

Using Group_Concat and Group By

sql
    SELECT user_id, 
    GROUP_CONCAT(interest ORDER BY interest ASC) AS concatenated_interests
    FROM user_interests
    GROUP BY user_id;

Explanation:

The GROUP_CONCAT() function concatenates values from multiple rows into a single string.

If you want the concatenated string to be in a specific order, you can use the ORDER BY clause within the GROUP_CONCAT() function as well.

We are grouping by the user_id column to ensure that we get a single row for each useommas.

Solution 2:

Using Variables Inside a Derived Table

sql
SET @prev_user_id = NULL, @concat_interests = '';

SELECT 
    user_id, 
    MAX(concatenated_interests) AS concatenated_interests
FROM (
    SELECT 
        user_id,
        (CASE 
            WHEN @prev_user_id = user_id THEN @concat_interests := CONCAT(@concat_interests, ',', interest)
            ELSE @concat_interests := interest
         END) AS concatenated_interests,
        @prev_user_id := user_id
    FROM 
        user_interests
    ORDER BY 
        user_id, interest
) AS derived_table
GROUP BY 
    user_id;

Explanation:

We initialize two session variables, @prev_user_id and @concat_interests, to keep track of the current user we’re processing and the accumulated interests for that user.

The derived table (subquery) processes the user_interests table row-by-row in the order of user_id and interest. As it processes each row:

If the user_id matches @prev_user_id, it appends the interest to @concat_interests.

If the user_id is different, it resets @concat_interests to the current row’s interest.

The outer query then groups by user_id and uses the MAX() function to get the fully concatenated string for each user.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to efficiently convert rows to columns in SQL?
  2. How to transpose columns to rows in SQL?
  3. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science