ML Engineer MasterClass (April) | 4 seats left

String

String

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_idproduct_namecategorypricestock_quantity
1“Wireless Mouse”“Electronics”25.99100
2“Bluetooth Headphones”“Electronics”59.9950
3“Yoga Mat”“Sports”19.99200
4“Smartphone”“Electronics”699.9930
5“Laptop Stand”“Office Supplies”39.9975

amazon_orders table:

order_idcustomer_idproduct_idorder_datequantitytotal_pricestatusdelivery_date
112“2024-01-10T14:30:00”159.99“Delivered”“2024-01-15T00:00:00”
223“2024-01-12T09:20:00”239.98“Shipped”“NaT”
335“2024-01-15T18:10:00”139.99“Pending”“NaT”
441“2024-01-18T20:45:00”377.97“Delivered”“2024-01-22T00:00:00”
554“2024-01-20T10:05:00”1699.99“Canceled”“NaT”
611“2024-01-25T16:15:00”251.98“Shipped”“NaT”
724“2024-01-27T12:40:00”1699.99“Pending”“NaT”
832“2024-01-29T08:55:00”2119.98“Delivered”“2024-02-02T00:00:00”
945“2024-02-01T14:00:00”139.99“Shipped”“NaT”
1053“2024-02-05T11:30:00”359.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

SQL
SELECT 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

SQL
SELECT customer_id, CONCAT(name, ' (', country, ')') AS formatted_name  
FROM amazon_customers  
ORDER BY customer_id;
customer_idformatted_name
1Alice Johnson (USA)
2Bob Smith (Canada)
3Charlie Lee (UK)
4David Kim (Germany)
5Emma 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

SQL
SELECT 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

SQL
1SELECT product_id, product_name,  
2       SUBSTRING(product_name FROM 1 FOR 5) AS short_name  
3FROM amazon_products  
4ORDER BY product_id;
product_idproduct_nameshort_name
1Wireless MouseWire
2Bluetooth HeadphonesBlue
3Yoga MatYoga
4SmartphoneSmart
5Laptop StandLapto

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_idformatted_name
1Alice Johnson (USA)
2Bob Smith (Canada)
3Charlie Lee (UK)
4David Kim (Germany)
5Emma 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_idproduct_nameshort_name
1Wireless MouseWire
2Bluetooth HeadphonesBlue
3Yoga MatYoga
4SmartphoneSmart
5Laptop StandLapto

Write an SQL query to return the requested data.