Date Arithmetic and Intervals
Adding/subtracting periods and computing differences between dates.
Why Date Math Shows Up in Interviews
Date arithmetic is one of the most practical SQL skills, so interviewers lean on it heavily for analyst and backend roles. Almost every business question has a time component: last 30 days, orders per week, days since signup.
The catch is that date functions are the least standardized part of SQL. The same operation has different syntax in PostgreSQL, MySQL, and SQL Server. A strong candidate states the idea clearly, then adapts the syntax to the dialect.
- Adding or subtracting a period
- Computing the difference between two dates
- Working with
INTERVALvalues
The INTERVAL Type
In PostgreSQL and the SQL standard, a period of time is a first-class value called an interval. You add it to a date or timestamp with plain + and - operators.
This is the cleanest way to express "30 days ago" or "3 months from now" and interviewers love it because it reads like English.
SELECT
CURRENT_DATE,
CURRENT_DATE + INTERVAL '7 days' AS next_week,
CURRENT_DATE - INTERVAL '1 month' AS last_month,
NOW() + INTERVAL '90 minutes' AS soon;All lessons in this course
- Date Arithmetic and Intervals
- Truncating and Bucketing Dates
- Parsing and Formatting Strings
- Time Zones and Timestamps