CONCAT()
Published Dec 3, 2024
Contribute to Docs
The CONCAT()
function in SQL is used to join two or more strings into a single string.
It is widely used for combining columns, literals, or expressions into meaningful data, such as full names or formatted outputs.
Syntax
CONCAT(string1, string2, ..., stringN)
string1, string2, ..., stringN
: The strings or expressions to concatenate. These can be columns, string literals, or other expressions. A minimum of two strings must be provided.
Note: If any argument is
NULL
, the result will also beNULL
.
Example
Suppose there is a table employees
with the following data:
first_name | last_name |
---|---|
John | Doe |
Alice | Smith |
Mark | Johnson |
To create a column with full names, the following query including CONCAT()
can be used:
SELECT CONCAT(first_name, ' ', last_name) AS full_nameFROM employees;
The output of the above example will be as follows:
| full_name ||------------------|| John Doe || Alice Smith || Mark Johnson |
Let’s say that there’s a need to create email addresses from the first_name
and last_name
. In this case, the CONCAT()
function can combine these columns with a domain name as follows:
SELECT CONCAT(LOWER(first_name), '.', LOWER(last_name), '@company.com') AS emailFROM employees;
The output of the above example will be as follows:
| email ||-----------------------|
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Skill path
Analyze Data with SQL
Learn to analyze data with SQL and prepare for technical interviews.Includes 9 CoursesWith CertificateBeginner Friendly17 hours - Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly5 hours