Extract Product Discounts
Beginner Mode
Scenario
You are an analyst at a retail company reviewing product listings where some items have discount information embedded in their descriptions.
Task
Write a Snowflake SQL query that:
- Selects all existing columns from
{{ ref("products") }} - Extracts the numeric percentage value from
descriptionwhere the pattern[N% off]appears (inside square brackets) - Converts the extracted number to a decimal by dividing by 100
- Returns
0(notNULL) when no discount pattern is found - Aliases the result as
discount
Schema
products
| Column | Type | Description |
|---|---|---|
| store_id | String | Identifier for the store location |
| product_name | String | Name of the product |
| category | String | Product category |
| units_sold | Integer | Number of units sold |
| description | String | Product description (may contain [N% off] discount tag) |
Example
products:
| store_id | product_name | category | units_sold | description |
|---|---|---|---|---|
| A01 | Rice Crackers | Snacks | 200 | Crunchy Rice Crackers [15% off] |
| A02 | Face Wash | Beauty | 90 | Gentle Face Wash [10% off] |
| A03 | Almonds | Snacks | 175 | Roasted Almonds |
| A01 | Notebook | Stationery | 310 | Spiral Bound Notebook |
| A02 | Yoga Mat | Fitness | 55 | Premium Yoga Mat [25% off] |
Expected Output:
| store_id | product_name | category | units_sold | description | discount |
|---|---|---|---|---|---|
| A01 | Rice Crackers | Snacks | 200 | Crunchy Rice Crackers [15% off] | 0.15 |
| A02 | Face Wash | Beauty | 90 | Gentle Face Wash [10% off] | 0.10 |
| A03 | Almonds | Snacks | 175 | Roasted Almonds | 0.00 |
| A01 | Notebook | Stationery | 310 | Spiral Bound Notebook | 0.00 |
| A02 | Yoga Mat | Fitness | 55 | Premium Yoga Mat [25% off] | 0.25 |
Note: The discount is always an integer percentage inside
[N% off]. Divide the extracted number by 100 to get the decimal. Rows with no bracketed discount return0, notNULL.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Airbnb