MySQL
String Concatenation
SQL Queries
Database Management
SQL Functions

String concatenation in MySQL

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

String concatenation in MySQL is commonly used for display labels, generated identifiers, and reporting output. MySQL offers several functions with different null-handling behavior, so choosing the right one is important. This guide covers CONCAT, CONCAT_WS, practical query patterns, and performance considerations.

Basic Concatenation with CONCAT

CONCAT joins multiple values in order.

sql
SELECT CONCAT('first=', first_name, ' last=', last_name) AS full_text
FROM users;

If any argument is null, the entire result becomes null.

sql
SELECT CONCAT('A', NULL, 'B') AS result;
-- result is NULL

Use this behavior intentionally, especially in data quality checks.

Null-Safe Concatenation with CONCAT_WS

CONCAT_WS means concat with separator. It skips null arguments and places separator between non-null parts.

sql
SELECT CONCAT_WS(' ', first_name, middle_name, last_name) AS full_name
FROM users;

If middle_name is null, output still contains first and last names cleanly.

Combine with COALESCE for Explicit Defaults

When you want stable output even with missing fields, coalesce values before concatenation.

sql
1SELECT CONCAT(
2    COALESCE(city, 'Unknown city'),
3    ', ',
4    COALESCE(country, 'Unknown country')
5) AS location_text
6FROM addresses;

This avoids null output and makes fallback behavior explicit.

Concatenate Aggregated Values by Group

For one-to-many relations, use GROUP_CONCAT to join values into one row.

sql
1SELECT
2    order_id,
3    GROUP_CONCAT(product_name ORDER BY product_name SEPARATOR ', ') AS products
4FROM order_items
5GROUP BY order_id;

This is useful in exports and summaries, but monitor maximum output length settings.

Updating Columns with Concatenation

You can use concatenation in UPDATE statements for generated labels.

sql
UPDATE users
SET display_name = CONCAT(first_name, ' ', last_name)
WHERE display_name IS NULL;

Always validate output length against column size constraints.

Performance and Indexing Notes

Concatenation in WHERE clauses can prevent index usage if you apply functions to indexed columns.

For example, this pattern can be expensive on large tables:

sql
SELECT *
FROM users
WHERE CONCAT(first_name, ' ', last_name) = 'Alice Jones';

A better approach is searching normalized columns directly, or adding generated columns with indexes when full-name matching is frequent.

Formatting Numeric and Date Values in Concatenation

Concatenation often mixes strings with numeric or date fields. Convert values explicitly so output format is predictable.

sql
1SELECT CONCAT(
2    'Order ', order_id,
3    ' placed on ', DATE_FORMAT(created_at, '%Y-%m-%d'),
4    ' total=', FORMAT(total_amount, 2)
5) AS summary
6FROM orders;

Explicit formatting prevents locale surprises and inconsistent report output. In application-facing APIs, keep display formatting requirements clear so database and service layers do not duplicate conflicting formatting logic. A common pattern is storing raw values in the database and formatting near presentation edges.

Migration and Compatibility Notes

If you migrate from other SQL engines, verify concatenation behavior differences around null handling. Query logic that worked in one system may yield null-heavy output in MySQL if CONCAT is used without defensive null handling. Add compatibility tests for reporting queries during migration phases.

Keep concatenation logic covered by query tests so null and separator behavior remains stable after schema updates.

Common Pitfalls

A common pitfall is forgetting that CONCAT returns null when any argument is null.

Another issue is using concatenated expressions heavily in filters without considering index impact.

Developers also overlook string length limits and receive truncated results in target columns.

A final mistake is relying on locale-specific formatting inside SQL text assembly that should be handled in application code.

Summary

  • Use CONCAT for straightforward string joining when null behavior is acceptable.
  • Use CONCAT_WS when you need separator-aware null skipping.
  • Combine with COALESCE for explicit defaults and stable outputs.
  • Use GROUP_CONCAT for grouped many-to-one string aggregation.
  • Avoid heavy concatenation in filters when index performance matters.

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.