SQL-Engineering-Handbook

Module 09 — Date Functions

status: complete engine: MySQL 8.0+ cross-engine notes 5 files 5 diagrams 15 interview questions license: MIT

Part of the SQL Engineering Handbook


Table of Contents

  1. Why This Module Exists
  2. Who This Is For
  3. Prerequisites
  4. What This Module Covers
  5. The Diagrams
  6. Functions Covered
  7. Learning Roadmap
  8. Business Domains
  9. Difficulty & Estimated Time
  10. Real Dashboards This Module Powers
  11. Performance Tips
  12. Best Practices
  13. Common Mistakes
  14. Build Status
  15. Module Checklist
  16. How to Use This Module
  17. Interview Preparation
  18. Career Relevance
  19. Further Reading
  20. Module Navigation

Why This Module Exists

Every mature analytics organization runs on a calendar. Finance closes the books monthly. Sales reports quarterly. HR tracks tenure in days. Marketing measures campaign lift over a 7-day or 30-day window. None of this is possible without a working, production-grade command of SQL date and time functions.

This is the point in the handbook where you stop writing queries that merely filter data and start writing queries that reason about time. You will learn not just the syntax of DATEDIFF() or DATE_FORMAT(), but the engineering judgment behind when to compute a date in SQL versus in the application layer, why storing derived date columns is sometimes the correct architectural choice, and how naive date logic silently corrupts dashboards in production.

Consider what breaks if date logic is wrong:

Dates are deceptively simple and operationally dangerous. This module exists to close that gap before it costs you in production — or in an interview.

Who This Is For

Aspiring Data Analysts and Analytics Engineers who have finished Modules 00–08 and are ready to move from “queries that filter data” to “queries that reason about time.” No prior scheduling or calendar-math background is assumed — every pattern (rolling windows, fiscal periods, tenure math) is built up from the raw extraction and arithmetic functions first.

Prerequisites

Before starting this module, you should be comfortable with:

If any of these feel shaky, revisit the relevant module first. Date functions are frequently combined with window functions and CTEs in this module’s later scenarios.

What This Module Covers

# File Focus Size Diagram
01 Current Date Functions · .sql Session-safe retrieval of “now”; NOW() vs. CURDATE() vs. SYSDATE() evaluation timing 9.3 KB · 7.3 KB now vs. sysdate
02 Date Extraction · .sql Decomposing a date into year, quarter, month, week, weekday, day-of-year for grouping 8.5 KB · 8.2 KB part extraction
03 Date Calculations · .sql Interval-based addition, subtraction, and differencing — day-count vs. calendar-unit 10.0 KB · 10.6 KB arithmetic timeline
04 Date Formatting · .sql Converting between internal date types and human/system string representations 9.9 KB · 8.1 KB format/parse cycle
05 Business Date Analytics · .sql MTD/QTD/YTD, rolling windows, fiscal periods, tenure, SLA monitoring, cohort foundations 10.2 KB · 12.3 KB MTD/QTD/YTD windows
README.md (this file) Module map, diagrams, roadmap, and navigation hero banner

Each numbered pair (.md + .sql) is self-contained: read the concept file first, then work through the paired SQL file scenario by scenario. Sizes above are the current on-disk file sizes, listed so you know what you’re opening before you click.

The Diagrams

Every diagram in this module (5 total, one per concept file) is rendered as a standalone SVG in assets/diagrams/ and embedded directly in its corresponding file — no external image hosting, so they render correctly whether you’re reading on GitHub, cloned locally, in any markdown viewer, or on the handbook’s GitHub Pages site.

NOW() vs SYSDATE() evaluation timing Date part extraction

Date arithmetic timeline Format and parse cycle

MTD QTD YTD rolling windows

assets/DIAGRAM_SPECS.md documents exactly what exists, what each diagram shows, and what was deliberately left out (and why) — kept accurate against the actual asset folder, not aspirational.

Functions Covered

Current Date / Time

CURRENT_DATE · CURRENT_TIME · CURRENT_TIMESTAMP · NOW() · SYSDATE() · CURDATE()

Extraction

YEAR() · MONTH() · DAY() · DAYNAME() · MONTHNAME() · QUARTER() · WEEK() · WEEKDAY() · DAYOFYEAR() · DAYOFWEEK()

Arithmetic

DATE_ADD() · DATE_SUB() · DATEDIFF() · TIMESTAMPDIFF() · ADDDATE() · SUBDATE()

Formatting & Conversion

DATE_FORMAT() · STR_TO_DATE() · CAST() · CONVERT()

Business & Composite Patterns

Rolling windows (trailing 7/30/90 days) · MTD · QTD · YTD · fiscal-period calculations · tenure/age/duration math · SLA delay measurement

Cross-dialect note: This handbook is written and tested against MySQL 8. Wherever a function is MySQL-specific, the corresponding Markdown file includes a callout with the PostgreSQL and SQL Server (T-SQL) equivalent, since production teams rarely work in a single dialect for their entire career.

Learning Roadmap

 01_CURRENT_DATE_FUNCTIONS
        │   "What is right now, and how do I ask for it safely?"
        ▼
 02_DATE_EXTRACTION
        │   "How do I break a date into reportable parts?"
        ▼
 03_DATE_CALCULATIONS
        │   "How do I move forward/backward in time and measure gaps?"
        ▼
 04_DATE_FORMATTING
        │   "How do I present dates to humans and parse dates from them?"
        ▼
 05_BUSINESS_DATE_ANALYTICS
        │   "How do real companies combine all of the above into reports?"
        ▼
   Module 10 — String Functions →

Each file builds on the last. Extraction depends on knowing what “current date” even means in a session; calculations depend on extraction; formatting depends on calculations; and business analytics is the synthesis of all four.

Business Domains

Domain Representative Problems in This Module
HR Tenure calculation, promotion eligibility windows, attrition timing, hiring trend analysis, payroll period boundaries
Sales Daily/weekly/monthly/quarterly revenue, moving averages, period-over-period growth
Finance Budget periods, fiscal quarters, invoice due dates, accounting cycle boundaries
E-commerce Delivery delay tracking, customer lifetime, repeat-purchase windows, order aging
Banking Transaction aging, statement generation periods, interest accrual windows
Healthcare Length of stay, appointment scheduling gaps, admission trend analysis
Manufacturing Production schedule adherence, downtime duration, quality-check intervals
Marketing Campaign window analysis, attribution lookback periods

Difficulty & Estimated Time

File Difficulty Estimated Time
01_CURRENT_DATE_FUNCTIONS Beginner 30–40 min
02_DATE_EXTRACTION Beginner–Intermediate 45–60 min
03_DATE_CALCULATIONS Intermediate 60–75 min
04_DATE_FORMATTING Intermediate 45–60 min
05_BUSINESS_DATE_ANALYTICS Intermediate–Advanced 90–120 min
Module Total Intermediate ~4.5–6 hours

Real Dashboards This Module Powers

Performance Tips

Best Practices

Common Mistakes

Mistake Why It’s Wrong Correct Approach
WHERE order_date = '2024-05-01' on a DATETIME column Matches only exact midnight; silently drops same-day rows with a time component Use a half-open range: >= '2024-05-01' AND < '2024-05-02'
hire_date + 30 for “30 days later” Works only if the dialect supports implicit day arithmetic; unclear and non-portable DATE_ADD(hire_date, INTERVAL 30 DAY)
Using DATEDIFF() for hour-level SLA checks DATEDIFF() only counts whole days, hiding same-day delays TIMESTAMPDIFF(HOUR, start, end)
YEAR(col) = 2024 in WHERE Non-sargable — disables index usage on col Range filter on the raw column
Assuming every month has 30 days Breaks at month boundaries (28/29/30/31-day months) Let INTERVAL ... MONTH arithmetic handle it
Ignoring time zones on TIMESTAMP columns Produces off-by-one-day errors across regions Normalize to UTC in storage; convert at the presentation layer
Confusing calendar quarter with fiscal quarter Produces incorrect quarter labels for non-January fiscal years Compute fiscal quarter explicitly relative to the fiscal year start

Build Status

Complete and diagram-reviewed. All 5 content files, their paired SQL, and every diagram referenced from those files are published and embedded. Module navigation links have been verified against the live repository folder names (not an assumed or outdated structure), and file sizes in the table above are read directly off disk rather than estimated.

Module Checklist

How to Use This Module

  1. Read the .md file for a topic before opening its .sql file — the concepts (why a date function behaves the way it does) matter more than memorizing syntax.
  2. Work through each .sql file scenario by scenario; don’t just read the solution — attempt the stated business question yourself first.
  3. Complete the Practice Challenges at the end of every Markdown file before moving to the next numbered file.
  4. Repeat for files 02 through 05, in order — each file assumes mastery of the previous one (see the Learning Roadmap).
  5. After finishing 05_BUSINESS_DATE_ANALYTICS, attempt to build one dashboard-style query from scratch using only the business scenario, without referencing the solutions.

Interview Preparation

Date-function questions are a favorite in SQL technical screens because they reveal whether a candidate understands edge cases, not just syntax. Expect questions such as:

Each Markdown file in this module includes a dedicated Interview Questions section (3 questions per file, 15 total) addressing patterns like these in depth.

Career Relevance

Date logic appears in essentially every analytics, data engineering, and backend engineering role:

Fluency here is one of the fastest ways to distinguish a candidate who has “learned SQL syntax” from one who has “engineered with SQL in production.”

Further Reading

Module Navigation

Previous Current Next
← Module 08: Window Business Cases Module 09: Date Functions Module 10: String Functions →

Part of the SQL Engineering Handbook — a production-grade curriculum for engineering SQL the way real companies use it.