Learn / Using AI
How to check AI-written credit-risk SQL before you trust it
An AI tool can write a DPD query in seconds. It will usually run. The risk is that it runs and gives a number that looks fine and is wrong. Wrong joins and date logic do not throw errors. These checks take ten minutes and catch most of the problems.
The ten checks
- Row count. Rows out should equal loans in the loan master, one row per loan, unless you intend otherwise.
- Duplicates. Group by loan id and look for a count above one. A join to payments is the usual cause.
- Negative DPD. There should be none. Early payers should show 0.
- As-of date. Every loan is measured on the same date, and that date is where you expect it.
- Bucket totals. The sum of all buckets equals the active book.
- Boundary days. Test loans at DPD 0, 1, 30, 31, 90 and 91 and check which bucket each lands in.
- Part payments. Find a loan with a part payment and trace how the query treated it.
- Nulls. Look for missing due dates or payment dates and see what the query does with them.
- Hand trace. Pick ten loans and work out the answer from the raw schedule and payments. Compare.
- Known number. Reconcile the result with a figure your MIS or finance team already trusts.
Why AI gets these wrong
The AI does not know your tables. If you do not tell it which table holds the schedule, which holds payments, how a part payment is applied, and what 'active' means, it guesses. A guess that joins one loan to nine payment rows multiplies the loan by nine and inflates every total.
How to reduce the problem at the source
- Give the AI your table and column names and your definitions before it writes anything.
- Ask it to explain its logic in plain steps so you can review them.
- Ask for the checks to run along with the query.
- Run the query on a small slice first.
Try it on your own tables
The free tools give you the prompt, the checks and how to present the result. You enter table names only.
See the free toolsFrequently asked questions
Can I trust SQL written by ChatGPT or another AI tool?
Treat it like code from a new analyst. It may run correctly, but you must check it against your own tables, definitions and a hand-traced sample before using the result.
What is the most common error in AI-written DPD queries?
Duplicated rows from joining the repayment schedule to the payments table, followed by counting DPD from the wrong due date.
Do I need to share customer data to get a query written?
No. Table and column names are enough for the AI to write the query. You run it on your own data inside your own systems.
Keep reading
What is DPD (days past due) and how to calculate it
DPD counts how many days a loan is overdue. Learn the rule, a worked example with several instalments, and the mistakes that make DPD wrong.
Using AIUsing AI for credit-risk work without sharing customer data
You can get useful credit-risk SQL and analysis from an AI tool by sharing only table and column names. How the method works and what to keep out of the chat.