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 →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| customer_id | full_name | segment_label |
|---|---|---|
| C1001 | Maya Brooks | Northeast-Online |
| C1002 | Noah Patel | Midwest-In-Store |
| C1003 | Emma Chen | Southeast-Online |
| C1004 | Lucas Martin | West-Mobile |
| C1005 | Ava Singh | Northeast-In-Store |
Step by step
- 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; - CONCAT joins all its arguments into one string.
SELECT customer_id, CONCAT(first_name, ' ', last_name), CONCAT(region, '-', channel) AS segment_label FROM customers; - 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
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 customers2. Using products, write a query that joins the product_id and brand columns with a space between them.
SELECT product_id || ' ' || brand FROM products3. 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 suppliers4. 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 employees5. 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 reviewsLearn 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 →