Join ML Engineer Interview MasterClass (April Cohort) led by FAANG Data Scientists | Just 6 seats remaining...
ML Engineer MasterClass (April) | 6 seats left
By the end of this lesson, you will:
Your manager at Amazon, Sophia, needs insights on order processing times and delivery schedules.
“Can you extract key details from order dates and calculate estimated delivery dates?”
Your task is to use date functions like EXTRACT() to pull parts of a date and DATE_ADD() to manipulate delivery timelines. Use the following tables to answer the questions.
amazon_products table:
| product_id | product_name | category | price | stock_quantity |
|---|---|---|---|---|
| 1 | “Wireless Mouse” | “Electronics” | 25.99 | 100 |
| 2 | “Bluetooth Headphones” | “Electronics” | 59.99 | 50 |
| 3 | “Yoga Mat” | “Sports” | 19.99 | 200 |
| 4 | “Smartphone” | “Electronics” | 699.99 | 30 |
| 5 | “Laptop Stand” | “Office Supplies” | 39.99 | 75 |
amazon_orders table:
| order_id | customer_id | product_id | order_date | quantity | total_price | status | delivery_date |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 2 | “2024-01-10T14:30:00” | 1 | 59.99 | “Delivered” | “2024-01-15T00:00:00” |
| 2 | 2 | 3 | “2024-01-12T09:20:00” | 2 | 39.98 | “Shipped” | “NaT” |
| 3 | 3 | 5 | “2024-01-15T18:10:00” | 1 | 39.99 | “Pending” | “NaT” |
| 4 | 4 | 1 | “2024-01-18T20:45:00” | 3 | 77.97 | “Delivered” | “2024-01-22T00:00:00” |
| 5 | 5 | 4 | “2024-01-20T10:05:00” | 1 | 699.99 | “Canceled” | “NaT” |
| 6 | 1 | 1 | “2024-01-25T16:15:00” | 2 | 51.98 | “Shipped” | “NaT” |
| 7 | 2 | 4 | “2024-01-27T12:40:00” | 1 | 699.99 | “Pending” | “NaT” |
| 8 | 3 | 2 | “2024-01-29T08:55:00” | 2 | 119.98 | “Delivered” | “2024-02-02T00:00:00” |
| 9 | 4 | 5 | “2024-02-01T14:00:00” | 1 | 39.99 | “Shipped” | “NaT” |
| 10 | 5 | 3 | “2024-02-05T11:30:00” | 3 | 59.97 | “Pending” | “NaT” |
The EXTRACT() function retrieves specific components of a date (year, month, day, etc.).
| Why use EXTRACT()? |
|---|
| Analyze order trends by year or month |
| Extract specific parts of a timestamp |
| Create custom time-based reports |
SELECT EXTRACT(part FROM date_column) FROM table_name;part can be YEAR, MONTH, DAY, HOUR, etc.1SELECT order_id, order_date,
2 EXTRACT(YEAR FROM order_date) AS order_year,
3 EXTRACT(MONTH FROM order_date) AS order_month
4FROM amazon_orders
5ORDER BY order_date;| order_id | order_date | order_year | order_month |
|---|---|---|---|
| 1 | 2024-01-10 14:30:00 | 2024 | 1 |
| 2 | 2024-01-12 09:20:00 | 2024 | 1 |
| 3 | 2024-01-15 18:10:00 | 2024 | 1 |
| 4 | 2024-01-18 20:45:00 | 2024 | 1 |
| 5 | 2024-01-20 10:05:00 | 2024 | 1 |
What’s Happening?
EXTRACT(YEAR FROM order_date) pulls the year from each order date.EXTRACT(MONTH FROM order_date) pulls the month.The DATE_ADD() function adds a time interval to a date.
| Why use DATE_ADD()? |
|---|
| Estimate future dates (e.g., delivery timelines) |
| Add weeks, months, or years to a date |
| Calculate subscription renewal dates |
SELECT DATE_ADD(date_column, INTERVAL value unit) FROM table_name;value is the number of time units to add.unit can be DAY, MONTH, YEAR, etc.1SELECT order_id, order_date,
2 DATE_ADD(order_date, INTERVAL 5 DAY) AS estimated_delivery
3FROM amazon_orders
4WHERE status = 'Shipped'
5ORDER BY order_date;| order_id | order_date | estimated_delivery |
|---|---|---|
| 2 | 2024-01-12 09:20:00 | 2024-01-17 |
| 6 | 2024-01-25 16:15:00 | 2024-01-30 |
| 9 | 2024-02-01 14:00:00 | 2024-02-06 |
What’s Happening?
DATE_ADD(order_date, INTERVAL 5 DAY) adds 5 days to each shipped order’s date to estimate delivery.Sophia needs a report breaking down order trends by year and month.
Filter condition:
order_date in ascending order.| order_id | order_date | order_year | order_month |
|---|---|---|---|
| 1 | “2024-01-10T14:30:00” | 2024 | 1 |
| 2 | “2024-01-12T09:20:00” | 2024 | 1 |
| 3 | “2024-01-15T18:10:00” | 2024 | 1 |
| 4 | “2024-01-18T20:45:00” | 2024 | 1 |
| 5 | “2024-01-20T10:05:00” | 2024 | 1 |
| 6 | “2024-01-25T16:15:00” | 2024 | 1 |
| 7 | “2024-01-27T12:40:00” | 2024 | 1 |
| 8 | “2024-01-29T08:55:00” | 2024 | 1 |
| 9 | “2024-02-01T14:00:00” | 2024 | 2 |
| 10 | “2024-02-05T11:30:00” | 2024 | 2 |
Write an SQL query to return the requested data.
Sophia wants to estimate delivery dates by adding 5 days to shipped orders.
Filter condition:
order_date in ascending order.| order_id | order_date | estimated_delivery |
|---|---|---|
| 2 | “2024-01-12T09:20:00” | “2024-01-17T09:20:00” |
| 6 | “2024-01-25T16:15:00” | “2024-01-30T16:15:00” |
| 9 | “2024-02-01T14:00:00” | “2024-02-06T14:00:00” |
Write an SQL query to return the requested data.