MySQL
SQL query
GROUP_CONCAT
DISTINCT
database operations

MySQL DISTINCT on a GROUP_CONCAT

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In SQL, the GROUP_CONCAT() function is a handy tool in MySQL for concatenating values from multiple rows into a single string. However, when dealing with duplicate values within the rows to be concatenated, distinctiveness becomes a critical requirement. The DISTINCT keyword plays a vital role here, ensuring that only unique values are concatenated. This article dives into the technical intricacies of using DISTINCT with GROUP_CONCAT() and provides illustrated examples to solidify these concepts.

Understanding GROUP_CONCAT()

The GROUP_CONCAT() function in MySQL facilitates the aggregation of multiple input strings from various rows into a single string. By default, the concatenated values are separated by commas, but this separator can be modified as needed. The basic syntax is as follows:

sql
GROUP_CONCAT([DISTINCT] expression
    [ORDER BY expression [ASC|DESC]]
    [SEPARATOR string_val])
  • expression: The column or calculation whose values you wish to concatenate.
  • DISTINCT: Ensures only unique values are concatenated.
  • ORDER BY: Allows sorting of the values before concatenation.
  • SEPARATOR: Defines a custom separator string between concatenated values.

Why Use DISTINCT with GROUP_CONCAT()?

When multiple rows have identical values in the specified column(s), concatenating these without DISTINCT would result in redundant data within your concatenated string. Using DISTINCT removes duplicates, producing a streamlined, unique list of values. Consider the following scenario:

Example

Suppose you have a table employees with employee names and departments:

sql
1CREATE TABLE employees (
2    id INT AUTO_INCREMENT PRIMARY KEY,
3    name VARCHAR(255),
4    department VARCHAR(255)
5);
6
7INSERT INTO employees (name, department) VALUES 
8('Alice', 'HR'),
9('Bob', 'IT'),
10('Charlie', 'HR'),
11('Alice', 'HR'),
12('Eve', 'IT');

If you want a list of unique names in a string per department, you would use:

sql
SELECT department, GROUP_CONCAT(DISTINCT name) AS unique_names
FROM employees
GROUP BY department;

Explanation

  • DISTINCT name: Ensures that duplicate names are not repeated in the concatenated result.
  • GROUP BY department: Groups the result set by department, aggregating names within each group.

Expected Result:

departmentunique_names
HRAlice, Charlie
ITBob, Eve

Advanced Usage and Considerations

Custom Separators

The default separator for GROUP_CONCAT() is a comma. However, you can specify any separator of your choice using the SEPARATOR clause. For example, using a semicolon as a separator:

sql
SELECT department, GROUP_CONCAT(DISTINCT name SEPARATOR '; ') AS unique_names
FROM employees
GROUP BY department;

Ordering Concatenated Values

You may sort the values using the ORDER BY clause inside the GROUP_CONCAT() function. This is especially useful if the order of concatenated values is significant:

sql
SELECT department, GROUP_CONCAT(DISTINCT name ORDER BY name ASC) AS unique_names
FROM employees
GROUP BY department;

Table: Key Points on Using DISTINCT with GROUP_CONCAT()

ComponentDescription
Basic SyntaxGROUP_CONCAT([DISTINCT] expression [ORDER BY expression] [SEPARATOR sep])
DISTINCTEliminates duplicate values in the concatenation process
Default SeparatorComma (changeable via the SEPARATOR clause)
OrderingValues can be ordered using ORDER BY before concatenation
Use CasesConcatenating unique values, streamline data representation
Group By RequirementUsed with GROUP BY to aggregate rows based on a criterion

Conclusion

MySQL's GROUP_CONCAT() with DISTINCT is a powerful feature when you need a clear, unique aggregation of data, especially in report generation and data exports. With customization options such as separators and order, it provides flexibility to suit different needs while ensuring data uniqueness and integrity. Whether you're summarizing data from a small table or integrating into complex business logic, mastering GROUP_CONCAT(DISTINCT ...) can enhance your SQL capabilities significantly.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.