Weekend Order Detection
Beginner Mode
Scenario
You work for an e-commerce company and have been given two tables containing order records (with dates in MM/DD/YYYY format) and product details.
Task
Write a Snowflake SQL query that:
- Joins
{{ ref("orders") }}with{{ ref("products") }}onproduct_idusing anINNER JOIN - Parses
order_datefromMM/DD/YYYYformat usingTRY_TO_DATEto handle invalid dates gracefully - Drops rows where
order_dateis invalid (whereTRY_TO_DATEreturnsNULL) - Adds an
is_weekendcolumn that isTRUEwhen the parsed date falls on Saturday or Sunday, usingDAYOFWEEK - Returns
user_id,product_name,category, the originalorder_datestring, andis_weekend
Schema
orders
| Column | Type | Description |
|---|---|---|
| order_id | Integer | Unique identifier for the order |
| product_id | String | Identifier for the product ordered |
| user_id | String | Identifier for the user who placed the order |
| order_date | String | Date of order placement in MM/DD/YYYY format |
products
| Column | Type | Description |
|---|---|---|
| product_id | String | Unique identifier for the product |
| product_name | String | Name of the product |
| category | String | Product category |
Example
orders:
| order_id | product_id | user_id | order_date |
|---|---|---|---|
| 1 | P001 | U101 | 01/07/2024 |
| 2 | P002 | U101 | 01/10/2024 |
| 3 | P001 | U102 | 01/13/2024 |
| 4 | P003 | U102 | 01/15/2024 |
| 5 | P004 | U103 | 01/21/2024 |
| 6 | P002 | U103 | 99/99/9999 |
products:
| product_id | product_name | category |
|---|---|---|
| P001 | Wireless Mouse | Electronics |
| P002 | Cotton T-Shirt | Clothing |
| P003 | Scented Candle | Home Goods |
| P004 | Python Cookbook | Books |
Expected Output:
| user_id | product_name | category | order_date | is_weekend |
|---|---|---|---|---|
| U101 | Wireless Mouse | Electronics | 01/07/2024 | true |
| U101 | Cotton T-Shirt | Clothing | 01/10/2024 | false |
| U102 | Wireless Mouse | Electronics | 01/13/2024 | true |
| U102 | Scented Candle | Home Goods | 01/15/2024 | false |
| U103 | Python Cookbook | Books | 01/21/2024 | true |
Note: Row with order_date
99/99/9999is dropped because it is not a valid date. 01/07/2024 (Sunday), 01/13/2024 (Saturday), and 01/21/2024 (Sunday) are weekends. 01/10/2024 (Wednesday) and 01/15/2024 (Monday) are weekdays.
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 →
IBM