PRO SQL Modules
SQL Basics1h 20m • 4 lessons
Multiple Tables1h 40m • 5 lessons
Query Restructuring40m • 2 lessons
Data Transformation1h 40m • 5 lessons
Analytical SQL2h • 6 lessons
String

Dan Lee
Lesson Objectives
By the end of this lesson, you will:
- Learn how to use CONCAT() to combine text values
- Use SUBSTRING() to extract parts of a string
- Write SQL queries to analyze and format Amazon customer and product data
🎬 Scenario: Formatting Customer and Product Data
Your manager at Amazon, Sophia, needs cleaner and more structured customer and product reports.
Can you modify the customer names for better readability and extract key details from product names?
Your task is to use string functions like CONCAT() to format customer names and SUBSTRING() to extract meaningful parts of product descriptions. Use the following tables to answer the questions.
amazon_products table:
product_id | product_name | category | price | stock_quantity |
---|---|---|---|---|
1 | “Wireless Mouse” | “Electronics” | 25.99 | 100 |
2 | “Bluetooth Headphones” | “Electronics” | 59.99 | 50 |
3 | “Yoga Mat” | “Sports” | 19.99 | 200 |
4 | “Smartphone” | “Electronics” | 699.99 | 30 |
5 | “Laptop Stand” | “Office Supplies” | 39.99 | 75 |
amazon_orders table:
order_id | customer_id | product_id | order_date | quantity | total_price | status | delivery_date |
---|---|---|---|---|---|---|---|
1 | 1 | 2 | “2024-01-10T14:30:00” | 1 | 59.99 | “Delivered” | “2024-01-15T00:00:00” |
2 | 2 | 3 | “2024-01-12T09:20:00” | 2 | 39.98 | “Shipped” | “NaT” |
3 | 3 | 5 | “2024-01-15T18:10:00” | 1 | 39.99 | “Pending” | “NaT” |
4 | 4 | 1 | “2024-01-18T20:45:00” | 3 | 77.97 | “Delivered” | “2024-01-22T00:00:00” |
5 | 5 | 4 | “2024-01-20T10:05:00” | 1 | 699.99 | “Canceled” | “NaT” |
6 | 1 | 1 | “2024-01-25T16:15:00” | 2 | 51.98 | “Shipped” | “NaT” |
7 | 2 | 4 | “2024-01-27T12:40:00” | 1 | 699.99 | “Pending” | “NaT” |
8 | 3 | 2 | “2024-01-29T08:55:00” | 2 | 119.98 | “Delivered” | “2024-02-02T00:00:00” |
9 | 4 | 5 | “2024-02-01T14:00:00” | 1 | 39.99 | “Shipped” | “NaT” |
10 | 5 | 3 | “2024-02-05T11:30:00” | 3 | 59.97 | “Pending” | “NaT” |
1. Using CONCAT() to Combine Text
The CONCAT() function joins two or more text values into one.
Why use CONCAT()? |
---|
Combine first and last names into full names |
Format text-based reports |
Create user-friendly descriptions |
Syntax
1SELECT CONCAT(string1, string2) AS new_string FROM table_name;
- Joins multiple string columns or values.
- Automatically removes NULL values (returns NULL if any input is NULL).
Example Query: Formatting Customer Full Names
1SELECT customer_id, CONCAT(name, ' (', country, ')') AS formatted_name
2FROM amazon_customers
3ORDER BY customer_id;
customer_id | formatted_name |
---|---|
1 | Alice Johnson (USA) |
2 | Bob Smith (Canada) |
3 | Charlie Lee (UK) |
4 | David Kim (Germany) |
5 | Emma Garcia (Mexico) |
What’s Happening?
- CONCAT(name, ' (', country, ')') adds the country in parentheses next to each name.
- This makes customer reports more informative.
2. Using SUBSTRING() to Extract Parts of a String
The SUBSTRING() function extracts a portion of a text value.
Why use SUBSTRING()? |
---|
Extract specific characters from text |
Shorten long product descriptions |
Format structured IDs |
Syntax
1SELECT SUBSTRING(string_column FROM start_position FOR length) FROM table_name;
- Start position determines where extraction begins (1-based index).
- Length defines how many characters to extract.
Example Query: Extracting Product Categories from Product Names
1SELECT product_id, product_name,
2 SUBSTRING(product_name FROM 1 FOR 5) AS short_name
3FROM amazon_products
4ORDER BY product_id;
product_id | product_name | short_name |
---|---|---|
1 | Wireless Mouse | Wire |
2 | Bluetooth Headphones | Blue |
3 | Yoga Mat | Yoga |
4 | Smartphone | Smart |
5 | Laptop Stand | Lapto |
What’s Happening?
- The SUBSTRING(product_name FROM 1 FOR 5) extracts the first 5 characters of each product name.
- Useful for creating shorter product codes or abbreviations.
✍️ SQL Exercises
Exercise 1: Formatting Customer Names
Sophia wants a formatted customer name report, showing customer names alongside their country in parentheses.
Filter condition:
- Retrieve customer ID and formatted name (e.g., "Alice Johnson (USA)").
- Sort by customer_id in ascending order.
customer_id | formatted_name |
---|---|
1 | Alice Johnson (USA) |
2 | Bob Smith (Canada) |
3 | Charlie Lee (UK) |
4 | David Kim (Germany) |
5 | Emma Garcia (Mexico) |
Write an SQL query to return the requested data.
Exercise 2: Extracting Short Product Names
Sophia also wants to generate product abbreviations by extracting the first five characters of each product name.
Filter condition:
- Retrieve product ID, product name, and a new column short_name containing only the first five characters.
- Sort by product_id in ascending order.
product_id | product_name | short_name |
---|---|---|
1 | Wireless Mouse | Wire |
2 | Bluetooth Headphones | Blue |
3 | Yoga Mat | Yoga |
4 | Smartphone | Smart |
5 | Laptop Stand | Lapto |
Write an SQL query to return the requested data.