Big SQL Energy Beginner Course: Lesson 20 Date_Add()
Got BDE?⚡️
Hi BDEs! Welcome to Lesson 20 of the free Big SQL Energy Beginner Course⚡️ Today we’re learning about Date_Add() in SQL!
Return to Big SQL Energy Beginner homepage here to access the other lessons:
If you’re new here:
💁🏽♀️ Who Am I?
I’m Jess Ramos, the founder of Big Data Energy and the creator of the BEST SQL course and community: Big SQL Energy⚡️. Check me out on socials: 🔗LinkedIn, 🔗Instagram, 🔗YouTube, and 🔗TikTok. And of course subscribe to my 🔗newsletter here for all my upcoming lessons and updates— all for free!
⚡️ What is Big SQL Energy?
🔗Big SQL Energy Intermediate is an intermediate SQL course designed to solve real-world business problems hands-on using realistic data and a modern tech stack. You’ll walk away with 2 SQL portfolio projects, portfolio building guidance, access to the Big Data Energy Discord community, and lots of confidence for you upcoming coding interviews! You also get access to all monthly events including masterclasses, office hours, and guest speakers. This course is all of the most important things I’ve learned on the job as a Senior Data Analyst in tech who grew from a $72K salary to over a $150K salary. And now I’m sharing it with YOU! It’s more than just another course— it’s a challenging program designed to upskill your SQL to the intermediate level, get ready to ace your live coding interviews, and build connections in the data community through the Discord and events.
Lesson 20: Date_Add()
Date_Add() is really useful for manipulating dates and creating new dates from ones you already have. What if you want to create an estimated delivery date based on the original order date? Or if you want to come up with a date to follow up with customers after their orders for reviews? Here’s the basic syntax and an example of how to use date_add:
date_add(date, interval interval_value interval_unit)
date_add(order_date, interval 7 day)
Note that there are different syntaxes for date_add(), so the ordering of the arguments may vary across platforms and dialects. Google or ChatGPT if you have syntax issues!
We can use date_add() to create an estimated delivery date that’s 7 days after each order’s order date:
SELECT
order_id,
order_date,
date_add(order_date, interval 7 day) as estimated_delivery_date
FROM
customer_orders_with_coupon;
Notice that we can also do different time units as well. For example, we can create a new date 3 months after the order date to follow up with customers for a review. We can even convert the output to a true date using cast (or double colons like :: ) if the output appears with a blank timestamp:
select
order_id,
customer_id,
order_date,
DATE_ADD(order_date, interval 3 month)::date AS follow_up_date
FROM
orders
Of course we can also use date_add() calculations to filter an output or result set as well. Below, we’re filtering for only orders that have a follow up date in the upcoming week based on the currentdate (which we learned in a past lesson!).
select
order_id,
customer_id,
order_date,
DATE_ADD(order_date, interval 3 month)::date AS follow_up_date
FROM
orders
WHERE
follow_up_date between current_date() and date_add(current_date(), interval 7 day)
Of course, this output of this is null with our sample dataset since we don’t have live, recent data, but you can see how this is a useful functionality when it comes to weekly reporting. Imagine having this dynamic date filter for a query you need to update and pull once a week! It would be way better than hardcoding the date and manually updating it each time.
If you’ve enjoyed the free BEGINNER Big SQL Energy course, grab my intermediate course. There are no deadlines or timelines for this course— you start and take as long as you want. It will take your beginner skills to the next level and get you ready to ace your interviews. DM me or email me at courses@bdeanalytics.com if you have questions on the intermediate course!
Return to Big SQL Energy Beginner homepage here to access the other lessons: