Last Chance to Join Data Science Interview MasterClass (next week) 🚀 | Just 3 spots remaining...

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

String

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

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

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_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

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_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.

Upgrade to Pro

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.

Upgrade to Pro
👉 Found this lesson helpful?