Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to Get the Type of Columns in SQL
Next article icon

How to Concat Two Columns Into One in MySQL?

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Concatenating columns in MySQL is a key operation for combining data from multiple fields into a single, unified column. This process is essential for generating comprehensive reports and merging data elements like names or addresses.

This article explores two effective methods for column concatenation using the CONCAT() function and the REPLACE() function.

How to Concatenate Two Columns Using MySQL?

Concatenating columns is a common requirement in data manipulation and reporting. We might need to combine first names and last names into a full name or merge address components into a single address field.MySQL provides multiple approaches to achieve this, each with its use cases and benefits.

  1. Using the CONCAT Function
  2. Using the REPLACE Function

Let’s set up an Environemnt

Demo Table

CREATE DATABASE geeks;
USE geeks;
CREATE TABLE demo_table(
FIRSTNAME VARCHAR(20),
LASTNAME VARCHAR(20),
AGE INT);
INSERT INTO demo_table VALUES
('Romy', 'Kumari', 21),
('Pushkar', 'Jha', 22),
('Meenakshi', 'Jha', 19),
('Rinkle', 'Arora', 22),
('Ayushi', 'Choudhary', 21),
('Sujata', 'Jha', 31);

Output:

demo table created

There are two methods to concat columns in MySQL, depending on your use case, you can use any of these techniques to concat two columns.

Method 1: Using the CONCAT Function

This method will not make changes to the original table.

For the demonstration, we will concatenate FIRSTNAME and LASTNAME and will name the column FIRSTNAME.

Query:

SELECT  *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME
FROM demo_table;

Output:

concat function example

Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(‘ ‘) in CONCAT() function.

Query:

SELECT  *, CONCAT(FIRSTNAME,' ', LASTNAME) AS FIRSTNAME
FROM demo_table;

Output:

concat function with white space output

Method 2: Using the REPLACE Function

This method will change the original table.

For the demonstration, we will replace FIRSTNAME with the concatenated value of FIRSTNAME and LASTNAME column. To do this, we can use REPLACE function with CONCAT function.

Query:

UPDATE demo_table  
SET FIRSTNAME = REPLACE(FIRSTNAME,FIRSTNAME, CONCAT(FIRSTNAME,' ', LASTNAME));

Output:

By-replacing-the-existing-column

Conclusion

MySQL offers flexible methods for concatenating columns, with CONCAT() being ideal for temporary views and reports, while REPLACE() is useful for permanent data updates. Understanding these techniques allows you to efficiently manage and manipulate your data according to your specific needs.



Next Article
How to Get the Type of Columns in SQL
author
romy421kumari
Improve
Article Tags :
  • Databases
  • SQL
  • mysql
  • SQL-Query

Similar Reads

  • How to Convert Rows into Columns in MySQL?
    Converting rows into columns, also known as pivoting or transposing, is a common operation in DBMS, and MySQL provides robust functionality for achieving this transformation. This process is useful to reshape data for better analysis or reporting. This guide will explore the syntax, usage, and examp
    3 min read
  • How to Combine Two Columns into One in R dataframe?
    In this article, we will discuss how to combine two columns into one in dataframe in R Programming Language.  Method 1 : Using paste() function This function is used to join the two columns in the dataframe with a separator. Syntax: paste(data$column1, data$column2, sep=" ") where data is the input
    2 min read
  • How to Get Column Names in MySQL?
    To get column names in MySQL use techniques such as the DESCRIBE statement, INFORMATION_SCHEMA.COLUMNS, and SHOW COLUMNS FROM commands. Here will cover these techniques, with explained examples, and help to get a better understanding on how to get column names in MySQL. MySQL Fetch Column Names from
    3 min read
  • How to Get the Type of Columns in SQL
    In SQL, the types of columns in a database table play a fundamental role in data management and query execution. Each column is designed to store specific types of data, such as numbers, text, dates, or binary data. Understanding these column types is essential for effective database design, query o
    4 min read
  • How to Efficiently Convert Rows to Columns in SQL?
    In SQL, rows and columns are the fundamental building blocks of a database. Rows represent individual records, while columns represent the attributes or characteristics of those records. However, there may be instances where we need to convert rows to columns in order to better analyze and manipulat
    5 min read
  • How to Rename a Column in MySQL?
    Renaming columns in MySQL is a frequent task to keep data organized and flexible. It helps adjust database layouts to fit new needs without losing information. This article will show you different ways to rename columns in MySQL, making it easier to manage and update your database structure as your
    4 min read
  • How to Efficiently Convert Rows to Columns in PostgreSQL?
    Converting rows to columns, often referred to as pivoting or transposing, is a crucial aspect of data transformation in SQL. This technique is useful for improving data readability, facilitating analysis, aligning data formats with the requirements of reporting tools, and optimizing queries. In Post
    5 min read
  • How to Convert BLOB into VARCHAR in MySQL?
    In this article, we would be learning a SQL query to convert a column of BLOB Data Type to VARCHAR Data Type. To execute this query we would need to alter the table and subsequently a column's definition. We would first need to use the ALTER TABLE command to change the table. ALTER TABLE: ALTER TABL
    2 min read
  • How to Get the Datatype of Table Columns in MySQL?
    When working with MySQL databases, knowing the datatype of table columns is essential to maintain data integrity and avoid errors. This guide covers methods to check column datatypes, such as using SHOW COLUMNS and querying INFORMATION_SCHEMA.COLUMNS. Understanding column datatypes helps in designin
    4 min read
  • How to Check a Column is Empty or Null in MySQL?
    In the databases, determining whether a column is empty or null is a common task. MySQL provides various techniques to perform this check, allowing users to filter and manipulate data efficiently. This article delves into the methods for checking if a column is empty or null in MySQL, outlining the
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences