cswork / libraryOpen practice workspace

Coding questions · Gusto

Calculate Net Pay Change for Q1 2023 Decreases

This question evaluates a candidate's data-manipulation competency in SQL and Python, focusing on computing net pay, aggregating over a calendar quarter, and measuring percentage change between recent pay periods.

EMPLOYEES

+-------------+------------+-------+
| employee_id | hire_date  | state |
+-------------+------------+-------+
| 101         | 2022-04-01 | CA    |
| 102         | 2021-11-12 | NY    |
| 103         | 2020-07-20 | TX    |
+-------------+------------+-------+

PAYROLL_TRANSACTIONS

+----------------+-------------+------------+-----------+------------+
| transaction_id | employee_id | paid_at    | gross_pay | deductions |
+----------------+-------------+------------+-----------+------------+
| 1              | 101         | 2023-01-15 | 5000      | 500        |
| 2              | 101         | 2023-02-15 | 5000      | 550        |
| 3              | 102         | 2023-01-15 | 4500      | 400        |
| 4              | 103         | 2023-01-15 | 6000      | 600        |
| 5              | 102         | 2023-02-15 | 4500      | 450        |
+----------------+-------------+------------+-----------+------------+
Scenario

Online SQL screen evaluating day-to-day data-manipulation skills on Gusto payroll data

Question

Write a SQL query that returns each employee’s total net pay (gross_pay – deductions) for the first calendar quarter of 2023. For every employee, show the percentage change in net pay between their two most recent pay periods and filter to those whose net pay decreased.

Hints

Window functions or self-joins can help with pay-period comparisons.

Employees with Decreasing Q1 2023 Net Pay

Write a SQL query that, for the first calendar quarter of 2023 (2023-01-01 to 2023-03-31), returns each employee’s total net pay (gross_pay − deductions). For those employees who have at least two pay periods in Q1 2023, compute the percentage change in net pay between their two most recent Q1 pay periods and return only the employees whose most recent net pay decreased compared to the previous one.

Table EMPLOYEES

columntypeconstraints
employee_idINTEGERPRIMARY KEY, NOT NULL
hire_dateDATENOT NULL
stateVARCHAR(2)NOT NULL

Sample data:

employee_idhire_datestate
1012022-04-01CA
1022021-11-12NY
1032020-07-20TX

Table PAYROLL_TRANSACTIONS

columntypeconstraints
transaction_idINTEGERPRIMARY KEY, NOT NULL
employee_idINTEGERNOT NULL, FOREIGN KEY REFERENCES EMPLOYEES(employee_id)
paid_atDATENOT NULL
gross_payDECIMAL(10,2)NOT NULL
deductionsDECIMAL(10,2)NOT NULL

Sample data:

deductionsemployee_idgross_paypaid_attransaction_id
50010150002023-01-151
55010150002023-02-152
40010245002023-01-153
60010360002023-01-154
45010245002023-02-155

Expected output

Employees who have at least two pay periods in Q1 2023 (2023-01-01 to 2023-03-31) and whose most recent Q1 net pay decreased compared to the previous Q1 pay period. For each such employee, return their employee_id, total Q1 net pay, the previous and current (most recent) net pay amounts, and the percentage change between them.

curr_net_payemployee_idpct_changeprev_net_paytotal_q1_net_pay
4450101-1.1145008950
4050102-1.2241008150

Hints

  1. Filter paid_at to Q1 2023 (2023-01-01 through 2023-03-31).
  2. Compute net pay as gross_pay minus deductions before aggregating.
  3. Use window functions such as ROW_NUMBER or LAG, or a self-join, to compare each employee’s two most recent Q1 pay periods.