Join ML Engineer Interview MasterClass (April Cohort) led by FAANG Data Scientists | Just 4 seats remaining...
ML Engineer MasterClass (April) | 4 seats left
By the end of this lesson, you will:
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” |
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 |
SELECT CONCAT(string1, string2) AS new_string FROM table_name;SELECT customer_id, CONCAT(name, ' (', country, ')') AS formatted_name
FROM amazon_customers
ORDER 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.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 |
SELECT SUBSTRING(string_column FROM start_position FOR length) FROM table_name;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?
SUBSTRING(product_name FROM 1 FOR 5) extracts the first 5 characters of each product name.Sophia wants a formatted customer name report, showing customer names alongside their country in parentheses.
Filter condition:
"Alice Johnson (USA)").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.
Sophia also wants to generate product abbreviations by extracting the first five characters of each product name.
Filter condition:
short_name containing only the first five characters.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.