String Transformation and Analysis
This challenge focuses on the practical application of common string manipulation functions in SQL: LENGTH, UPPER, and LOWER. You'll work with a dataset of product names and descriptions, performing transformations and deriving insights based on their string properties. Understanding these functions is fundamental for data cleaning, standardization, and analysis in any database environment.
Problem Description
You are tasked with processing a table containing product information. For each product, you need to perform the following operations:
- Calculate the length of the product's
product_name. - Standardize the
product_nameby converting it to uppercase. - Standardize the
product_descriptionby converting it to lowercase. - Identify products where the original
product_name(case-insensitive comparison) is "apple" followed by any characters. This means "Apple", "APPLE", "apple pie", "Apple Juice" should all match.
You will need to output a result set that includes the original product details along with the transformed data and a flag indicating if the product name matches the specific pattern.
Key Requirements:
- Use the
LENGTHfunction to get the number of characters inproduct_name. - Use the
UPPERfunction to convertproduct_nameto uppercase. - Use the
LOWERfunction to convertproduct_descriptionto lowercase. - Implement a condition to check if the
product_namestarts with "apple" (case-insensitively). You can achieve this by converting theproduct_nameto lowercase before comparison.
Expected Behavior:
The output should contain rows for each product in the input table. Each row should include:
product_idproduct_name(original)product_description(original)name_length(integer: length ofproduct_name)uppercase_name(string:product_namein uppercase)lowercase_description(string:product_descriptionin lowercase)is_apple_product(boolean/integer:TRUEor1ifproduct_namestarts with "apple" case-insensitively,FALSEor0otherwise).
Edge Cases:
- Empty
product_nameorproduct_descriptionfields. - Product names with leading/trailing spaces (these should be included in the length calculation unless explicitly handled by trimming, which is not required for this specific challenge).
Examples
Example 1:
Input Table: products
| product_id | product_name | product_description |
|---|---|---|
| 1 | Laptop | High-performance... |
| 2 | APPLE JUICE | Refreshing drink... |
| 3 | Keyboard | Mechanical keys... |
Output:
| product_id | product_name | product_description | name_length | uppercase_name | lowercase_description | is_apple_product |
|---|---|---|---|---|---|---|
| 1 | Laptop | High-performance... | 6 | LAPTOP | high-performance... | 0 |
| 2 | APPLE JUICE | Refreshing drink... | 10 | APPLE JUICE | refreshing drink... | 1 |
| 3 | Keyboard | Mechanical keys... | 8 | KEYBOARD | mechanical keys... | 0 |
Explanation:
- Row 1: "Laptop" has length 6, becomes "LAPTOP" in uppercase. Description is lowercased. "Laptop" does not start with "apple".
- Row 2: "APPLE JUICE" has length 10, becomes "APPLE JUICE". Description is lowercased. "APPLE JUICE" (when lowercased to "apple juice") starts with "apple".
- Row 3: "Keyboard" has length 8, becomes "KEYBOARD". Description is lowercased. "Keyboard" does not start with "apple".
Example 2:
Input Table: products
| product_id | product_name | product_description |
|---|---|---|
| 4 | apple pie | Delicious dessert... |
| 5 | Banana | Tropical fruit... |
| 6 | (empty string) | No description provided. |
Output:
| product_id | product_name | product_description | name_length | uppercase_name | lowercase_description | is_apple_product |
|---|---|---|---|---|---|---|
| 4 | apple pie | Delicious dessert... | 9 | APPLE PIE | delicious dessert... | 1 |
| 5 | Banana | Tropical fruit... | 6 | BANANA | tropical fruit... | 0 |
| 6 | (empty string) | No description provided. | 0 | no description provided. | 0 |
Explanation:
- Row 4: "apple pie" has length 9, becomes "APPLE PIE". Description is lowercased. "apple pie" starts with "apple".
- Row 5: "Banana" has length 6, becomes "BANANA". Description is lowercased. "Banana" does not start with "apple".
- Row 6: An empty
product_namehas length 0, becomes an empty string. Description is lowercased. An empty string does not start with "apple".
Constraints
product_idis a unique identifier.product_nameandproduct_descriptionare strings.- The length of
product_namewill not exceed 255 characters. - The length of
product_descriptionwill not exceed 1000 characters. - The solution should be a single SQL query.
Notes
- Pseudocode for string comparison:
IF LOWER(product_name) LIKE 'apple%' THEN TRUE ELSE FALSE - Consider how your specific SQL dialect handles boolean types for the
is_apple_productcolumn. Some might use1/0, othersTRUE/FALSE. - The
LENGTHfunction typically counts characters. Be mindful of potential differences if dealing with multi-byte character sets, though for this challenge, assume standard character counting.