Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. When the Answer Lives in a Database, Not a Document
AI Integration

When the Answer Lives in a Database, Not a Document

Querying structured data with AI safely: the model writes the query, it runs read-only on a reporting copy via views, and the SQL is shown with results.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

The model should translate a question into a query, not invent an answer. Run the query read-only against a reporting copy, show the query alongside the result, and never let generated SQL touch production directly.

Documents and databases need different handling

Retrieval works for prose. It does not work for “how many orders over five hundred pounds shipped late last month”, because that answer does not exist in any document.

For those, the model's job is to write the query, and the database's job is to produce the number.

The safe pattern

  1. The question is translated into a query against a documented schema
  2. The query is validated — read-only, permitted tables, row limits
  3. It runs against a reporting replica, never production
  4. The result is returned as data, and the model explains it
  5. The query is shown, so the user can check the logic
Showing the query is what makes this trustworthy. A number with no visible derivation is a number nobody should act on.

What to restrict

  • Read-only credentials, always
  • An allow-list of tables and views, not the whole schema
  • Row and time limits, to prevent an accidental full scan
  • Permission filters applied in the view, not in the generated query
  • No writes, ever, from generated queries

Views are your friend

Expose curated views rather than raw tables: correctly joined, permission-filtered, with sensible column names. Query quality improves dramatically and the risk surface shrinks.

A dozen well-named views usually beats access to two hundred raw tables, for both accuracy and safety.

Where it works and where it does not

Question typeFit
Counts, sums and simple filtersStrong
Trends over timeStrong
Joins across two or three tablesGood with views
Complex financial calculationsUse a defined report instead
Anything a regulator will readUse a defined report instead

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

Is this safe against our production database?

Only against a read replica with restricted credentials. Never against production with write access, regardless of the safeguards in front of it.

How accurate is it?

Good on well-named schemas with curated views, poor on sprawling legacy ones. The schema quality decides it more than the model does.

What about ambiguous questions?

It should ask — “do you mean order date or ship date?” Guessing produces confident answers to the wrong question.

Should this replace our reporting?

No. It handles ad hoc questions well. Defined reports remain the right answer for anything recurring or consequential.

Keep reading

More on AI Integration

Start here

Ad hoc data questions queueing on one analyst?

A safe query layer over curated views takes the routine ones off their desk. Tell us what your data looks like.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →