TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: MySQL CONCAT to Concatenate Strings
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

MySQL CONCAT to Concatenate Strings

Last updated: May 22, 2023 12:52 pm
By Meenakshi Agarwal
Share
5 Min Read
MySQL CONCAT to Concatenate Strings
SHARE

This tutorial explains MySQL CONCAT() which is a built-in String function. It takes variable no. of strings as input and concatenates them together. We’ll describe the usages of this method with the help of simple examples.

Contents
SyntaxMySQL CONCAT() ExamplesMySQL CONCAT() with Tables

MySQL string concatenation is more user-friendly than other databases such as PostgreSQL or Oracle. They provide a string concatenation operator “||” instead of a proper function. However, MS SQL server does the same job using the addition arithmetic operator (+).

1. CONCAT() Syntax
2. CONCAT() Simple Examples
4. CONCAT() with Tables

Let’s now go through each of the section one by one.

MySQL CONCAT() Function

As stated initially, CONCAT() is a built-in MySQL function which accepts one more quoted strings as input and joins them together. It combines them one by one in the given order.

So, let’s now see the details of MySQL CONCAT and check out how can we use it.

Syntax

Below is the signature of this method:

CONCAT('string1', 'string2', ... );

Below are the descriptions of the parameters in the above function.

+----------+------------------------------+
| # Param  | # Description                |
+----------+------------------------------+
| string   |  A string for concatenation. |
+----------+------------------------------+

The CONCAT function first converts every argument to the string type before it does the concatenation. If any parameter is NULL, then it returns a NULL value.

Let’s now understand more about it with examples.

MySQL CONCAT() Examples

The following command concatenates two quoted words: Python and MySQL. However, we also added a space to separate two string.

SELECT CONCAT('Python', ' ', 'MySQL');

After execution, it gives the following result:

Python MySQL

If we supply a NULL value, then the CONCAT() function would provide us with a NULL in return. See below example:

SELECT CONCAT('Python', NULL, 'MySQL');

The outcome:

NULL

MySQL CONCAT() with Tables

Let’s now create a table, populate some data, and see how does MySQL concatenates strings.

CREATE TABLE EMPL
    (
        empl_id int NOT NULL,
        empl_first_name VARCHAR(20),
        empl_middle_name VARCHAR(20),
        empl_last_name VARCHAR(20),
        empl_address VARCHAR(40),
        empl_pin VARCHAR(10)
    );

INSERT INTO EMPL
    (
        empl_id, empl_first_name, empl_middle_name, empl_last_name, empl_address, empl_pin
    )
VALUES
    (1, 'Amit', 'Kumar', "Singh", "Sec-62, Noida", "201301"),
    (2, 'Jyoti', 'Rani', "Saini", "Sec-21, Noida", "201301"),
    (3, 'Vimal', 'Kumar', "Sood", "Sec-11, Noida", "201301"),
    (4, 'Neelam', 'Jai', "Singh", "Sec-23, Noida", "201301");

SELECT * FROM EMPL;

When you would run the given SQL commands, it gets you the following output:

1	Amit	Kumar	Singh	Sec-62, Noida	201301
2	Jyoti	Rani	Saini	Sec-21, Noida	201301
3	Vimal	Kumar	Sood	Sec-11, Noida	201301
4	Neelam	Jai	Singh	Sec-23, Noida	201301

You can observe that to see the full name of employees, you have to use the CONCAT function to combine first, middle, and last names separated with spaces.

Here’s the MySQL query to print the full name of all employees:

SELECT 
    CONCAT(empl_first_name, ' ', empl_middle_name, ' ', empl_last_name) Fullname
FROM
    EMPL;

After running it, you can list down all names in full. Check below output.

Amit Kumar Singh
Jyoti Rani Saini
Vimal Kumar Sood
Neelam Jai Singh

Similarly, if you wish to display the complete address, then also, you have to concatenate the Address and the PIN fields. We’ve got you the query for that as well. Check one below.

SELECT 
    CONCAT(empl_first_name, ' ', empl_middle_name, ' ', empl_last_name) Fullname,
    CONCAT(empl_address, ' ', empl_pin) Fulladdress
FROM
    EMPL;

Once you run this query, you will see the list of all employees with their full names and addresses. Here comes the final output:

Amit Kumar Singh	Sec-62, Noida 201301
Jyoti Rani Saini	Sec-21, Noida 201301
Vimal Kumar Sood	Sec-11, Noida 201301
Neelam Jai Singh	Sec-23, Noida 201301

We hope that after wrapping up this tutorial, you should feel comfortable in using the MySQL CONCAT() method. However, you may practice more with examples to gain confidence.

Also, to learn SQL from scratch to depth, do read our step by step MySQL tutorial.

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article MySQL DROP TABLE with Simple Examples MySQL DROP TABLE with Simple Examples
Next Article MySQL OPTIMIZE TABLE Statement with Examples MySQL OPTIMIZE TABLE Statement

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x