BBampaa

Lessons › SQL › Deep Dive: Text, Date & Number Functions

SQL lesson

CONCAT() or || concatenation

Concatenation joins separate text pieces into one combined value.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Combining Retail Text Fields with CONCAT() or ||

SELECT customer_id, CONCAT(first_name, ' ', last_name) AS full_name, CONCAT(region, '-', channel) AS segment_label
FROM customers

Result:

customer_idfull_namesegment_label
C1001Maya BrooksNortheast-Online
C1002Noah PatelMidwest-In-Store
C1003Emma ChenSoutheast-Online
C1004Lucas MartinWest-Mobile
C1005Ava SinghNortheast-In-Store

Step by step

  1. Run the query without CONCAT so you can see what the unfiltered values look like.
    SELECT customer_id, first_name, CONCAT(region, '-', channel) AS segment_label
    FROM customers;
  2. CONCAT joins all its arguments into one string.
    SELECT customer_id, CONCAT(first_name, ' ', last_name), CONCAT(region, '-', channel) AS segment_label
    FROM customers;
  3. AS gives the CONCAT result a friendly column name so the output reads cleanly.
    SELECT customer_id, CONCAT(first_name, ' ', last_name) AS full_name, CONCAT(region, '-', channel) AS segment_label
    FROM customers;

Practice

Try each one in your head first, then open the answer.

1. Using customers, write a query that joins the customer_id and full_name columns with a space between them.
SELECT CONCAT(customer_id, ' ', full_name) FROM customers
2. Using products, write a query that joins the product_id and brand columns with a space between them.
SELECT product_id || ' ' || brand FROM products
3. Using suppliers, write a query that joins the supplier_id and supplier_name columns with a space between them.
SELECT CONCAT(supplier_id, ' ', supplier_name) FROM suppliers
4. Using employees, write a query that joins the employee_id and full_name columns with a space between them.
SELECT CONCAT(employee_id, ' ', full_name) FROM employees
5. Using reviews, write a query that joins the review_id and product_id columns with a space between them.
SELECT CONCAT(review_id, ' ', product_id) FROM reviews

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed