Querydrill

SQL date function practice problems (PostgreSQL)

Grouping by month, counting days between two orders, filtering a date range without losing the last day: date questions are in every analyst interview and every monthly report.

The problems run on PostgreSQL, so they use to_char, EXTRACT, date_trunc and plain date arithmetic. Each prompt names the function when it is PostgreSQL-specific.

Try one here: Full years of service (age)

Try it yourself

Return each employee's name and the number of full years between their hire date and 1 January 2026.

employees (11 rows)

idnamedept_idsalaryhire_datemanager_id
1Ava21300002019-03-15NULL
2Ben21050002020-07-011
3Cleo1820002021-01-107
4Dan1760002022-05-237
5Eve3540002022-09-1211
6Finn2980002021-11-301
7Gia1990002018-06-04NULL
8Hugo3610002020-02-18NULL
9Iris4880002019-10-07NULL
10Jack4720002023-04-179
11KaiNULL500002024-01-088

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

Basics

Aggregation

  • Revenue by monthReturn each month as text like 2025-01 together with the total of its paid orders. In PostgreSQL, to_char(order_date, 'YYYY-MM') gives the month.
  • Hires per yearReturn each hire year (like 2021) and how many employees were hired that year. EXTRACT(YEAR FROM hire_date) gives the year.
  • Active in several monthsFor customers who ordered in at least two different months, return the customer_id and the number of different months they ordered in. Count every order, whatever the status.
  • Paid revenue per quarterReturn each quarter as text like 2025-Q1 together with its paid revenue. January to March is Q1, April to June is Q2, and so on. In PostgreSQL, to_char(order_date, 'Q') gives the quarter number.
  • Paid revenue per week (date_trunc)Return the first day (Monday) of each week that has paid orders, as a date, together with the paid revenue of that week.
  • Orders per weekday (EXTRACT)Return the ISO weekday number (1 for Monday to 7 for Sunday) and the number of orders placed on that weekday, whatever the status. Only weekdays that have orders.

Advanced

  • Days until the next orderFor every order, return the order id and the number of days until the same customer's next order (by date, then id), as a whole number. A customer's last order shows NULL. Count every order, whatever the status. In PostgreSQL, subtracting one date from another gives the number of days.
  • Month over monthReturn each month (like 2025-01), its paid revenue, and the change from the previous month's paid revenue. The first month shows NULL for the change.
  • Year-over-year growthFor every month of 2025 that is in the table, return the month, its revenue, the revenue of the same month one year earlier, and the growth as a percentage rounded to 1 decimal. When there is no figure for the same month a year earlier, the last two columns are NULL. When that figure is 0, the growth is NULL.

Interview classics

  • Orders in consecutive monthsReturn the customer_id of every customer who placed orders in two calendar months in a row, for example March and April, whatever the status. Each customer appears once.
  • Cohort retentionGroup customers by the month of their first order, like 2025-01, counting every order whatever the status. For each such month return the month, the number of customers who first ordered in it, and how many of those customers ordered again in a later month.
  • A month with no sales still shows up (generate_series)Return every month from January to June 2025 as text like 2025-01, with its paid revenue. A month without paid orders must still appear, with 0.
  • Monthly active usersReturn every month that has events, as text in the form YYYY-MM, with the number of different users who were active in that month.
  • Sessions per user (30-minute rule)A new session starts with a user's first event, and again whenever more than 30 minutes have passed since that user's previous event. Return each user_id with their number of sessions.
  • Reactivated subscribersThe end date is the first day without the subscription, so a new subscription that starts on the previous end date is a plan change with no gap. A reactivation is a subscription whose start date is later than the end date of the same user's previous subscription. A user's subscriptions never overlap. Return the user_id, the start date of each reactivation, and the gap in days: that start date minus the previous end date.

Case studies

  • Revenue report, step 1: revenue per monthYou are building the monthly revenue report. Start with the base: for each month that has paid orders, return the month as YYYY-MM and the total paid amount, in columns month and revenue.
  • Revenue report, step 2: change on the previous monthExtend the monthly report. For each month return month, revenue, the previous month's revenue as prev_revenue, and the difference as change (revenue minus prev_revenue). The first month has no previous month, so both extra columns are NULL there.
  • Revenue report, step 3: running totalAdd a year-to-date column. For each month return month, revenue and running_total: the sum of paid revenue from the first month up to and including that month.
  • Revenue report, step 4: share of the totalFinish the report. For each month return month, revenue and share_pct: that month's percentage of all paid revenue, rounded to 1 decimal.
  • Refund review, step 1: refund rate per monthThe head of sales thinks refunds are getting out of hand and wants facts by Friday. Start with the trend: for every month that has orders, return the month as text like 2025-01, the number of orders, the number of refunded orders and the refund rate in percent, rounded to 1 decimal. Count every order, whatever its status, in the number of orders.
  • Payroll review, step 4: hires per year and running headcountHow did the team grow? For every year in which someone was hired, return the year as a number, the number of hires in that year and the running total of hires up to and including that year.

Read first if you are stuck

Other practice sets

Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.