From “It Runs” to “I’ll Sign Off on It”: SQL You Can Actually Audit
What are you signing?
Now that AI can write SQL directly, the barrier to writing it has dropped a lot. Describe a requirement, and in a few minutes, you have code that runs. But the scrutiny hasn’t kept pace, and the review time hasn’t shrunk.
To change a rule, the SQL runs a few dozen lines, the reviewer approves it, and it merges and deploys. Two weeks later a report doesn’t reconcile, and only then would anyone go back to the code. Trace it far enough and the error is often a single line. To find that one line, you read the whole thing again. Nobody was careless. Everyone read the code, and everyone thought it looked fine.
The person signing off usually has only two things to go on: sample results from a test database before release, and a programmer’s “this logic should be fine”. Post-release results don’t exist yet, and the whole process lives in the code. Ad hoc queries and one-off reports are worse: they don’t even have regression tests. The only thing left to judge right from wrong is the logic itself.
Those two things can reassure, but they aren’t enough to sign off on. A signer can only sign off on what they can audit. If someone asks why this logic computes things this way, they should give an answer. Also, they should be able to point at one step and say, “I’ve looked at this one”.
The standard comes down to one thing: the signature has to land on something auditable. Auditable means checking the business rule against the logic, step by step. Different logic gives you different things to check: where the rule is written, whether the boundary was drawn according to it, whether the intermediate results are visible. When you can see none of them, signing off is no different from nodding.
In regulated industries, the standard is harder still. If the logic can’t be explained, the report fails review. There is no negotiating.
Four pain points in one piece of SQL
Start with an interval-merging example. Each account has several time ranges, and some overlap. Merge them into non-overlapping ranges. The rule in one sentence: sort by account, then by start date. If the current interval’s start date is later than the maximum end date of all previous intervals, start a new group. Within a group, take the earliest start and the latest end.
A common way to write it (MySQL):
SELECT account_id
, MIN(start_date) AS start_date
, MAX(end_date) AS end_date
FROM (
SELECT t.account_id, t.start_date, t.end_date
, SUM(CASE WHEN t.start_date > (
SELECT MAX(p.end_date)
FROM overlapping_intervals p
WHERE p.account_id = t.account_id
AND p.start_date < t.start_date
) THEN 1 ELSE 0 END)
OVER (PARTITION BY t.account_id
ORDER BY t.start_date
ROWS UNBOUNDED PRECEDING) AS gid
FROM overlapping_intervals t
) g
GROUP BY account_id, gid
ORDER BY account_id, start_date
I count four things that make it hard to audit. All of them come from the nature of the SQL language.
Inverted order. SELECT is written first, but it’s the last thing to run, after FROM, WHERE, and GROUP BY have all finished. In this SQL, the outermost layer only does the grouping. The grouping logic is hidden in a window inside FROM, and that window’s CASE nests a subquery that fetches the previous maximum end date. You write from SELECT down, but it computes from the innermost subquery out.
No visible steps. The whole thing delivers a single result. SQL later added CTEs, which can split a statement into pieces. But what gets split is the statement. Each piece is still one expression, and the intermediate results are still invisible. Here, prev_max comes from the subquery and gid comes from the window, and neither layer is materialized. To see how they’re derived, you have to take apart the statement layer by layer and run each piece on its own.
Doesn’t fit together. Window functions were added later, and syntactically they’re a patch. The PARTITION BY and ROWS BETWEEN clauses don’t connect with the older syntax, so you have to accommodate both sides. Conceptually they’re a patch too. When you reach a window, you have to set the previous line aside and think separately about what it’s accumulating within that partition. Here the window’s CASE has a scalar subquery stuffed inside it, so two mechanisms are stitched together.
Verbose. Each SQL clause is written on its own. Preconditions like partitioning, sorting, and filtering don’t carry forward automatically, so you repeat them every place you need them. In this query, account_id is written in the subquery’s WHERE, the window’s PARTITION BY, and the outer GROUP BY. start_date is written in the subquery’s comparison and the window’s ORDER BY. The same table appears twice, and the subquery has to run row by row.
Add the four together and you can still validate it if you take it apart slowly. But a single review means unpacking the subquery, running it twice, and personally guaranteeing that what you unpacked matches the original. Understanding it depends on memory. If someone else takes over, they read it from scratch. Nobody wants to spend that effort, so the check turns into a formality.
The same rule, broken into steps
SQL makes you read a piece of logic from the innermost layer out. In steps, you read top to bottom, and that is the order of computation. Here is the same rule, written as SQLazy steps:
overlapping_intervals:sort account_id, start_date
compute max end_date[:-1] as prev_max; partition account_id
segment condition start_date>prev_max as gid;partition account_id
summarize min start_date as start_date, max end_date as end_date; group account_id, gid
derive delete gid
Step 1 sorts by account, then by start date. Step 2 takes the maximum end date of all previous intervals.
The key is step 3. If the current interval’s start date is later than prev_max, a new group starts, and the group ID is written to gid.
Step 4 aggregates by account and group ID, taking the earliest start and the latest end. Step 5 drops gid, and what’s left is the result.
Each step runs on its own and produces a table. Click a step, and that step’s intermediate result appears beside it. If the logic is wrong, you see it before you run the whole thing, without waiting for a report full of absurd numbers to land on your desk.

When reviewing, look at three things first: which step the rule lives in, which statement holds the boundary, and whether the results are visible.
Check the rule against the condition in step 3 and confirm the boundary is exactly that statement. Account A’s first group is merged from three intervals. Why don’t the middle two start new groups? The answer is in the condition: both start before the previous maximum end date. Then look at the interval that sits far away. Its start date is later than the maximum end date, so it starts a new group. Finally, check the result of step 4: four rows for account A, one row for account B. Both match.
If the rule changes, only step 3 changes. Say you relax “start a new group when later than the maximum end date” to “start a new group only when more than 30 days later”. The two groups that were separate in account A now merge into one, and the result goes from four rows to three. Change it back, and it returns to four. Exactly this one place changes, and that version is one you can sign off on.
One more check before signing: run the original SQL and the script side by side. If the results match, it passes.
What you sign is the condition in step 3, plus the rows that step 5 hands off. Not the whole report, and not the whole system.
The sample has only a dozen or so records. A real report has hundreds or thousands of rows, with several more layers of structure.
After the five steps, the compiler generates native SQL. The window machinery is still in there. The signer just no longer reads it. The same steps with the same input are always compiled to the same output. Select a target database and compile with one click. It’s the same set of steps for MySQL, PostgreSQL, and Oracle. Humans read the steps, and the database runs the SQL.

Want to run it yourself: https://www.sqlazy.com/?4Bs
AI can join this workflow too, but it owns a different stage. AI plans a plain-language requirement into steps and spells out what each step does. The compiler then generates SQL from the steps. Humans review the steps, and the compiler generates the SQL from them.
SQLazy provides a set of skills for AI, and the model produces steps following that spec, so nobody has to assemble the format by hand.
The compiler guarantees one thing: the steps you wrote and the SQL that runs match. Whether the rule itself is right is not its concern. That’s a business judgment. The signer checks that the steps follow the rule.
AI writes the logic. The compiler writes the SQL. Whether the logic is right is still a human’s call.
Back to that review
Someone approved it. That shows the process was followed. It doesn’t mean anyone checked.
If what they’d been handed was a few steps, the first thing the signer opened would be different. The rule is in step 3, the boundary is written in that one statement, and the final result is in step 5. Check them against each other, and wherever something’s wrong, fix it at that step. No need to start the whole thing over.
It runs, and the machine is satisfied. But signing off is a step the machine can’t take for you.
AI writes the logic. A compiler writes the SQL.
The GitHub examples repo works as an index: https://github.com/SPLWare/SQLazy/tree/master/examples. Click into any example and you get the detail view in the Playground. You can run it directly and see every step.
Got a piece of SQL you’d least want to sign off on? Break it into steps and take a look. Which one is it?
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/sxd59A8F2W
Youtube 👉 https://www.youtube.com/@esProc_SPL
chinese version https://c.raqsoft.com.cn/article/1795407831698