Extracting Substrings from Product Codes
📖 Scenario: You work in a retail company database. Each product has a code that contains a category prefix followed by a numeric ID. You want to extract the category prefix from each product code.
🎯 Goal: Build a SQL query that extracts the first three characters (category prefix) from each product code in the products table.
📋 What You'll Learn
Create a table called
products with columns product_id (integer) and product_code (string).Insert exactly these rows into
products: (1, 'ABC12345'), (2, 'XYZ98765'), (3, 'DEF45678').Write a SQL query that selects
product_id and the first three characters of product_code as category.Use the
SUBSTRING function to extract the category prefix.💡 Why This Matters
🌍 Real World
Extracting parts of strings like product codes is common in databases to categorize or filter data.
💼 Career
Database developers and analysts often use substring extraction to clean and analyze data stored in text fields.
Progress0 / 4 steps