Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

May 16, 2023

13:00 UTC   Start Times Around the World

Subscribe to be notified of changes to sessions and give us feedback!

Having trouble watching the video on this page? Open the video in your browser.


How to avoid gaps and overlaps in consecutive start-end dates
It's common for tables to store start and end dates to record a time period. But this brings a challenge:

How do you ensure the end of one period is the start of the next? i.e. there are no gaps between periods and no periods overlap?

This session contrasted having start and end dates columns versus a single event date column and generating the periods.

Highlights include:

00:00:40 - Using foreign keys to ensure consecutive start-end dates
00:03:00 - Single change date column and generate start-end period
00:05:00 - Definition of consecutive periods
00:09:20 - Discussion & vote for one column vs start/end dates
00:13:25 - Data changes with one column vs start/end dates
00:19:30 - Demo of data changes with one column vs start/end dates
00:31:30 - Null vs magic dates for the end date for the current row
00:43:10 - Temporal validity to simplify start-end date queries
00:43:50 - Demo of null vs magic dates for the end date for the current row
00:50:30 - Performance tuning date queries
00:51:00 - Index column order for start date, end date columns
00:56:00 - Performance of queries to find the active row with one column
00:58:00 - Demo of query performance
01:07:00 - Session recap

Your Experts

    Chris Saxon

    Chris Saxon   

    Chris Saxon is an Oracle Developer Advocate and quizmaster on Dev Gym. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.