Katkat

Find Payroll Bookings in the Latest Active Month by Country

RipplingData Analyst · Technical Screen

Last updated

Tests PostgreSQL date manipulation, filtering, and aggregation by determining the most recent month with active payroll bookings and summarizing totals by country.

Problem Statement

At Rippling, payroll runs are processed across thousands of businesses worldwide. Finance needs a report showing the distribution of payroll bookings across different countries for the most recent active month.

Write a PostgreSQL query to identify the latest calendar month (based on booking_date) that contains at least one successful booking (status = 'completed'), and return the aggregated booking metrics for each company headquarters country (hq_country) during that specific month.

Input Schema

companies

ColumnTypeDescription
company_idINTUnique identifier for the company
company_nameVARCHARName of the company
hq_countryVARCHARHeadquarters country (ISO 2-letter code or country name)

payroll_bookings

ColumnTypeDescription
booking_idINTUnique identifier for the payroll booking
company_idINTForeign key referencing companies(company_id)
booking_dateDATEDate the payroll was booked
booking_amountNUMERIC(12, 2)Gross payroll amount booked
statusVARCHARBooking status ('completed', 'failed', 'pending')

Output Format

Return the following columns:

  • hq_country: The headquarters country.
  • total_amount: Total sum of booking_amount for completed bookings in that month, rounded to 2 decimal places.
  • booking_count: Total number of completed bookings in that month.

Order the results by total_amount descending, then by hq_country ascending.

Example

companies

company_idcompany_namehq_country
1Acme CorpUS
2Beta LtdUK
3Gamma GmbHDE

payroll_bookings

booking_idcompany_idbooking_datebooking_amountstatus
10112024-02-1510000.00completed
10222024-02-2815000.50completed
10312024-03-0512000.00completed
10422024-03-128000.00failed
10532024-03-205000.25completed
10612024-04-024000.00failed

Explanation

The latest date with a 'completed' booking is in March 2024 (2024-03-20). The April booking failed, so April is not an active month. Looking only at completed bookings in March 2024:

  • US (Company 1): 12000.00 (1 booking)
  • DE (Company 3): 5000.25 (1 booking)
  • UK has no completed bookings in March.

Output

hq_countrytotal_amountbooking_count
US12000.001
DE5000.251

Hints

Hint 1

Use DATE_TRUNC('month', booking_date) to truncate dates to the start of each calendar month.

Hint 2

Find the maximum truncated month among records where status = 'completed' using a subquery or Common Table Expression (CTE).

Hint 3

Join companies and payroll_bookings, filter by both the target month and status = 'completed', and use GROUP BY hq_country with proper ORDER BY logic.

Follow-up questions

  • How would you optimize this query if `payroll_bookings` contains over 100 million rows?
  • How would you extend this query to also return the month-over-month growth rate compared to the immediately preceding month for each country?
  • If `booking_date` were a UTC timestamp and countries needed reporting in their local time zones, how would you adjust the date truncation logic?