MySQL Group Concat Function

The group_concat() function in MySQL is really nifty. It concatenates all rows that match your group by statement into a single, comma separated string.

Essentially, it transposes rows to columns.

Take this example. Say you have a table that stores a set of locations by country, state and city.

You could then write SQL as follows:

group_concat(city) as cities
from locations
group by country, state

What this is saying is bring back 3 columns, country, state, and for each combination of country and state, a list of all of the cities in that country and state as the third column (called cities).

This is actually a phenomenally handy thing to be able to do and something you’ll use a lot once you know it exists!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Benefits of G Suite for Your Business (part 1)

NewNews: Postman API Hackathon The revolutionary news application

Directed Graph

Why IaC is a one-way journey?

Querying JSON using SQL — A comparison of Synapse, BigQuery, Databricks and Snowflake

AWS Cross Account Access — SSO

100 Days of Code: 5 To-Do Items to Set Yourself Up For Success

Introduction to OOP

Dell Inspiron 5370 — Developer Review (After 6 months of Usage)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praj Basnet

Praj Basnet

More from Medium

Version Controlling

Push multiple files under a single commit through GitHub API

Git and NoSQL