SQL Extract Day
Returns the day-of-month number — 1 through 31, depending on the month.
What & Why
EXTRACT(DAY FROM date_column) returns just the day-of-month portion — useful for questions like "which campaigns joined in the first half of the month."
See How It Works
BUSINESS QUESTION
Marketing wants lead volume by day of month.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
EXAMPLE QUERY
SELECT
EXTRACT(DAY FROM created_at)::int AS month_day,
COUNT(*) AS leads
FROM marketing.leads
GROUP BY month_day
ORDER BY month_day;RESULT — exact output from the displayed Queryflo rows
| month_day | leads |
|---|---|
| 16 | 1 |
| 20 | 1 |
| 21 | 1 |
| 24 | 1 |
EXTRACT returns each lead timestamp's day of month.
Now You Try
Practice this concept
Marketing wants lead volume by day of month.
Available schema
marketingPrefix tables with marketing.table_name.
month_dayleadsmarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario