Detect Significant Salary Declines
SQL coding challenge · Difficulty: medium · Topic: Window Functions · +150 XP
Problem
A bank monitors payroll credits to spot customers whose income has fallen
sharply, because a sudden drop is an early signal of repayment risk.
account_transactions holds every transaction on an account. A **salary
credit** is any row where txn_type = 'CREDIT'.
For each account, compare the latest salary credit against the average of
the three salary credits immediately before it. Report the account when
that fall is more than 40%.
Table
Table: account_transactions
| Column | Type | Description | | --- | --- | --- | | `account_id` | INT | account identifier | | `txn_date` | DATE | date of the transaction | | `txn_type` | VARCHAR | `CREDIT` or `DEBIT` | | `amount` | DECIMAL(12,2) | transaction amount |
Output
| Column | Meaning | | --- | --- | | `account_id` | the account | | `avg_previous_3_salary` | mean of the three credits before the latest, to 2 decimals | | `current_salary` | the latest salary credit, to 2 decimals | | `salary_drop_percentage` | the fall, to 2 decimals | | `risk_flag` | the literal `FLAGGED` |
Formula
`
average_previous_3 = (salary_2 + salary_3 + salary_4) / 3
salary_drop_percentage = ((average_previous_3 - latest_salary)
/ average_previous_3) * 100
`
salary_1 is the newest credit, salary_2 the one before it, and so on.
Rules
1. Only txn_type = 'CREDIT' rows are salary. **DEBIT rows are ignored
entirely**, including when deciding which credit is the latest.
2. Order credits by txn_date. salary_1 is the most recent.
3. Use exactly the three credits before the latest. If an account has more
than four credits, the older ones are not part of the average.
4. An account needs at least four salary credits. Fewer than four is
excluded, however steep the fall.
5. Flag only when salary_drop_percentage > 40. Exactly 40 is not
flagged.
6. Round avg_previous_3_salary, current_salary and
salary_drop_percentage to 2 decimals.
7. Do not hardcode account ids.
8. Sort by account_id ascending. Return no rows if nobody qualifies.
Example
account_transactions:
| account_id | txn_date | txn_type | amount | | --- | --- | --- | --- | | 1001 | 2024-01-05 | CREDIT | 100000.00 | | 1001 | 2024-02-05 | CREDIT | 105000.00 | | 1001 | 2024-03-05 | CREDIT | 98000.00 | | 1001 | 2024-04-05 | CREDIT | 50000.00 | | 1002 | 2024-01-05 | CREDIT | 70000.00 | | 1002 | 2024-02-05 | CREDIT | 72000.00 | | 1002 | 2024-03-05 | CREDIT | 71000.00 | | 1002 | 2024-04-05 | CREDIT | 70000.00 |
Expected output:
| account_id | avg_previous_3_salary | current_salary | salary_drop_percentage | risk_flag | | --- | --- | --- | --- | --- | | 1001 | 101000.00 | 50000.00 | 50.50 | FLAGGED |
Explanation
Account 1001: the latest credit is 50000. The three before it are
100000, 105000 and 98000, averaging 101000.
`
((101000 - 50000) / 101000) * 100 = 50.4950... -> 50.50
`
50.50 > 40, so 1001 is flagged.
Account 1002 falls from an average of 71000 to 70000, about 1.41%, so
it does not appear.
Constraints
1 <= rows <= 100000- no column is NULL, and
amount > 0 - an account has at most one salary credit on any given date
- an account may have any number of DEBIT rows, including none
Concepts
ROW_NUMBER(), PARTITION BY, CTEs, conditional aggregation, and a HAVING
style count guard. In PySpark: Window, row_number(), groupBy, agg,
filter and round.
What this SQL challenge teaches you
“Detect Significant Salary Declines” is a medium-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with ROW_NUMBER, PARTITION BY, CTE, window functions, conditional aggregation, HAVING — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- ROW_NUMBER
- PARTITION BY
- CTE
- window functions
- conditional aggregation
- HAVING
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- Rank each account's CREDIT rows newest-first with ROW_NUMBER() OVER (PARTITION BY account_id ORDER BY txn_date DESC). rn = 1 is the current salary; rn 2, 3 and 4 are the history you average.
- Filter the ranked set to rn BETWEEN 2 AND 4 and aggregate per account. COUNT(*) in that slice tells you whether the account really has three prior credits - if it is not exactly 3, the account has fewer than four credits and must be dropped.
- Join the rn = 1 row to that aggregate, compute ((avg_prev - current) / avg_prev) * 100, and keep only rows where it is strictly greater than 40. Round the three numeric columns to 2 decimals at the end.
Where this comes up
Variations of this problem have been reported in interviews at RBS. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Window Functions.