Lesson

MySQL QUARTER() Function

10 min reading
Free Course

MySQL QUARTER() Function

The MySQL QUARTER() function returns the calendar quarter of the year for a DATE or DATETIME value. It outputs an integer from 1 to 4.

Quarter mapping follows standard calendar rules:

  • 1: January through March
  • 2: April through June
  • 3: July through September
  • 4: October through December

Finance teams and SaaS dashboards use QUARTER() to summarize quarterly sales, churn, and hiring targets without writing verbose MONTH() CASE expressions.

Syntax

QUARTER(date)
  • date: A DATE, DATETIME, TIMESTAMP, or valid date string or integer.

Practical Example

Using demo_orders, you can aggregate total orders and revenue by year and quarter for executive reporting. Filtering order_date with explicit bounds lets MySQL keep index performance intact.

SELECT
    YEAR(order_date) AS sales_year,
    QUARTER(order_date) AS sales_quarter,
    COUNT(*) AS order_count,
    SUM(total_amount) AS quarterly_revenue
FROM demo_orders
WHERE order_date >= '2025-01-01'
  AND order_date < '2026-01-01'
GROUP BY YEAR(order_date), QUARTER(order_date)
ORDER BY sales_year, sales_quarter;
sales_year sales_quarter order_count quarterly_revenue
2025 1 312 44890.00
2025 2 358 51240.25
2025 3 341 48910.80
2025 4 419 62350.00

Combining YEAR() and QUARTER() keeps each year's quarters separate and sorted cleanly.

Common Mistakes & NULL Behavior

Grouping solely by QUARTER(order_date) across multi-year data merges Q1 2024 with Q1 2025. Always pair QUARTER() with YEAR() in SELECT and GROUP BY unless your WHERE clause locks the query to one year.

Another pitfall is assuming QUARTER() supports custom fiscal years (such as a fiscal year starting in April or July). QUARTER() strictly uses the January–December calendar year. For custom fiscal quarters, shift the date first using DATE_SUB(order_date, INTERVAL 3 MONTH) before calling QUARTER().

QUARTER() returns NULL when date is NULL or an invalid date like '2026-02-30'. Zero-month dates like '2026-00-00' return 0 when zero dates are permitted.

Frequently Asked Questions

How can I format the output as '2026-Q2' in MySQL?

Combine YEAR() and QUARTER() with CONCAT(): CONCAT(YEAR(order_date), '-Q', QUARTER(order_date)).

Does QUARTER() support fiscal years starting in July?

Not directly. You can calculate a July-start fiscal quarter with QUARTER(DATE_ADD(order_date, INTERVAL 6 MONTH)).

What does QUARTER('2026-02-30') return?

Because February 30 does not exist, MySQL treats it as an invalid date and returns NULL.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum