INTERVAL

SQL · Date Functions

Theory

An INTERVAL is a value that represents a length of time, such as 30 days, 6 months or 2 hours, rather than a point on the calendar. You add it to or subtract it from a date or timestamp: hire_date + INTERVAL '1 year' gives the work anniversary, and login_at - INTERVAL '15 minutes' gives a moment just before login.

In PostgreSQL, intervals are a real data type and you can even store them in columns or get one back by subtracting two timestamps. MySQL uses INTERVAL only inside expressions, written without quotes: d + INTERVAL 1 YEAR. SQL Server and Snowflake mostly use DATEADD instead, and SQLite expresses the same idea with modifier strings such as date(d, '+6 months') or datetime(t, '+2 hours'). Note that months and years are not fixed lengths: one month after 15 January is 15 February (31 days), but after 15 February it is 15 March (28 or 29 days).

Why we use it

Time spans appear everywhere: subscriptions last 1 year, sessions expire after 30 minutes, SLAs allow 48 hours. INTERVAL lets you write these spans directly and readably, and the database applies calendar rules correctly.

When to use it

Use INTERVAL arithmetic in PostgreSQL, MySQL and Oracle for shifting dates and building time windows. In SQL Server or Snowflake, use DATEADD; in SQLite, use date()/datetime() modifiers. Use a plain number of days only when your dialect documents it.

How it works

  1. Start with a date or timestamp
  2. Write the span: INTERVAL '6 months', '2 hours'
  3. Span in months or years? Yes: Move on the calendar (same day number). No: Add an exact length.
  4. Return the shifted date or timestamp
  5. Use it in SELECT, WHERE or a default

Syntax

date + INTERVAL 'n unit'

Example

SELECT hire_date + INTERVAL '1 year' FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic