ML Engineer MasterClass (April) | 6 seats left

Date Manipulation

Date Manipulation

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to use EXTRACT() to retrieve parts of a date
  • Use DATE_ADD() and DATE_SUB() to perform date arithmetic
  • Write SQL queries to analyze Amazon orders and delivery timelines

🎬 Scenario: Analyzing Order and Delivery Dates

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_idproduct_namecategorypricestock_quantity
1“Wireless Mouse”“Electronics”25.99100
2“Bluetooth Headphones”“Electronics”59.9950
3“Yoga Mat”“Sports”19.99200
4“Smartphone”“Electronics”699.9930
5“Laptop Stand”“Office Supplies”39.9975

amazon_orders table:

order_idcustomer_idproduct_idorder_datequantitytotal_pricestatusdelivery_date
112“2024-01-10T14:30:00”159.99“Delivered”“2024-01-15T00:00:00”
223“2024-01-12T09:20:00”239.98“Shipped”“NaT”
335“2024-01-15T18:10:00”139.99“Pending”“NaT”
441“2024-01-18T20:45:00”377.97“Delivered”“2024-01-22T00:00:00”
554“2024-01-20T10:05:00”1699.99“Canceled”“NaT”
611“2024-01-25T16:15:00”251.98“Shipped”“NaT”
724“2024-01-27T12:40:00”1699.99“Pending”“NaT”
832“2024-01-29T08:55:00”2119.98“Delivered”“2024-02-02T00:00:00”
945“2024-02-01T14:00:00”139.99“Shipped”“NaT”
1053“2024-02-05T11:30:00”359.97“Pending”“NaT”

1. Using EXTRACT() to Retrieve Parts of a Date

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

Syntax

SQL
SELECT EXTRACT(part FROM date_column) FROM table_name;
  • part can be YEAR, MONTH, DAY, HOUR, etc.

Example Query: Extracting Order Year and Month

SQL
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_idorder_dateorder_yearorder_month
12024-01-10 14:30:0020241
22024-01-12 09:20:0020241
32024-01-15 18:10:0020241
42024-01-18 20:45:0020241
52024-01-20 10:05:0020241

What’s Happening?

  • EXTRACT(YEAR FROM order_date) pulls the year from each order date.
  • EXTRACT(MONTH FROM order_date) pulls the month.

2. Using DATE_ADD() to Calculate Estimated Delivery Dates

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

Syntax

SQL
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.

Example Query: Adding Estimated Delivery Days

SQL
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_idorder_dateestimated_delivery
22024-01-12 09:20:002024-01-17
62024-01-25 16:15:002024-01-30
92024-02-01 14:00:002024-02-06

What’s Happening?

  • DATE_ADD(order_date, INTERVAL 5 DAY) adds 5 days to each shipped order’s date to estimate delivery.


✍️ SQL Exercises

Exercise 1: Extracting Order Year and Month

Sophia needs a report breaking down order trends by year and month.

Filter condition:

  • Retrieve order ID, order date, order year, and order month.
  • Sort by order_date in ascending order.
order_idorder_dateorder_yearorder_month
1“2024-01-10T14:30:00”20241
2“2024-01-12T09:20:00”20241
3“2024-01-15T18:10:00”20241
4“2024-01-18T20:45:00”20241
5“2024-01-20T10:05:00”20241
6“2024-01-25T16:15:00”20241
7“2024-01-27T12:40:00”20241
8“2024-01-29T08:55:00”20241
9“2024-02-01T14:00:00”20242
10“2024-02-05T11:30:00”20242

Write an SQL query to return the requested data.


Exercise 2: Estimating Delivery Dates

Sophia wants to estimate delivery dates by adding 5 days to shipped orders.

Filter condition:

  • Retrieve order ID, order date, and estimated delivery date.
  • Only include shipped orders.
  • Sort by order_date in ascending order.
order_idorder_dateestimated_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.