Learning SQL

Focused on helping you learn SQL

Member-only story

5 Ways You Misuse SQL Date Functions. And How To Be Better.

From hard coding dynamic variables to timezone mistakes, it’s time you master the subtle art of filtering SQL queries by date.

Zach Quinn
Learning SQL
Published in
7 min readJul 27, 2023

--

One of the first rules that was drilled into me during my first week at work was: “Whenever you query a table ALWAYS use a date filter.”

Even though this direction might seem like overkill on a smaller table not bigger than a few hundred megabytes, when you scale to terabytes or petabytes it becomes a requirement.

Since embracing that rule I’ve developed an appreciation for the power of date functions, filters and the way they can distill insights and reduce wide-scale resource consumption.

Along the way, I’ve developed my own set of best practices. I intend to share those with you to correct or prevent misguided approaches when it comes to filtering a data source by date.

Fragile package on doorstep.
Handle your SQL date filters with care. Photo by Nathan Dumlao on Unsplash.

1. Stop Hard Coding

Before we dive into more nuances of date filtering, I want to say up front:

Stop. Hard. Coding. Date variables.

Really this rule applies to nearly every variable you define programmatically. You should never hard code something you expect to change. That’s…

--

--

Zach Quinn
Zach Quinn

Written by Zach Quinn

Journalist—>Sr. Data Engineer; new stories weekly.

Responses (2)