Learn SQL/Intermediate/Date & Time/SQL Extract Quarter

SQL Extract Quarter

Returns which quarter of the year a date falls in — 1 through 4, three months each.

What & Why

EXTRACT(QUARTER FROM date_column) returns a number from 1 to 4: Q1 is January-March, Q2 is April-June, Q3 is July-September, Q4 is October-December. Common for financial and business reporting that groups by fiscal quarter.

See How It Works

BUSINESS QUESTION

Marketing wants campaign counts by quarter.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  EXTRACT(QUARTER FROM start_date)::int AS start_quarter,
  COUNT(*) AS campaign_count
FROM marketing.campaigns
GROUP BY start_quarter
ORDER BY start_quarter;
RESULT — exact output from the displayed Queryflo rows
start_quartercampaign_count
13
21

January through March form Q1; the April campaign begins Q2.

Now You Try

Practice this concept

Marketing wants campaign counts by quarter.

Available schema
marketing

Prefix tables with marketing.table_name.

start_quartercampaign_count
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario