Workflow recipe
From business question to SQL dashboard with AI
The assistant writes the SQL, you define the metrics and check every number before anyone acts.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
To build an AI SQL dashboard you can trust, turn the business question into written metric definitions, let the assistant read the schema and profile the tables, and write one query per metric. Check every number before it goes on a chart, build the dashboard from the checked results, review it with the question owner and save it for re-runs.
AI assistants write SQL quickly and mostly correctly. “Mostly” is the problem. A query that double-counts customers because of a join, includes test accounts, or uses order date where finance uses payment date returns a perfectly plausible number. It runs without errors and it goes into the board deck.
This recipe uses the assistant for what it is good at (reading a schema, drafting queries, building the page) and puts checks where it is weak: definitions, joins and reconciliation. The example question is about revenue and customers, but the steps are the same for any metric.
DocumentAbout 20 min
1. Turn the question into metric definitions
“How are we doing with enterprise customers?” is not a query. Sit with the person who asked and write down exactly what they need:
# Question
Is revenue from enterprise customers growing faster than from other customers this year?
# Metrics
- Revenue: sum of paid invoice amounts, excluding tax and refunds,
by month of payment date (not order date). Currency: GBP.
- Enterprise customer: account with plan = 'enterprise' at the time of the invoice.
- Active customer: an account with at least one paid invoice in the month.
# Exclusions
Test and internal accounts (is_internal = true). Invoices with status 'void'.
# Reconcile against
Finance's total revenue for October 2026: £[AMOUNT FROM FINANCE].
# Output
Monthly revenue and active customers, enterprise vs other, January to October 2026.Every disagreement you settle here saves an argument about the dashboard later. Get the question owner to approve this file.
Claude CodeAbout 20 min
2. Let the assistant read the schema, then profile the tables
Give the assistant the schema (table definitions, not data) and your definitions. With a coding agent connected to the database through a read-only role, it can read the schema itself. The sql-queries skill covers dialect differences and common patterns such as deduplication and cohorts.
Read the profiling results carefully. This is where you find that plan only holds the current plan, so “enterprise at the time of the invoice” needs a plan history table, or that some invoices are in euros. Take those findings back to the question owner and update the definitions file.
Claude CodeAbout 30 min
3. Write the queries, one metric at a time
Ask for one query per metric, saved as files, with comments that tie each line back to the definitions:
A query in this style is easy to review:
-- Revenue: paid invoices, excluding tax and refunds, by payment month (GBP)
with paid as (
select i.account_id,
i.paid_at,
date_trunc('month', i.paid_at)::date as month,
i.amount_excl_tax - coalesce(i.refunded_amount, 0) as revenue
from invoices i
where i.status = 'paid'
and i.currency = 'GBP'
and i.paid_at >= date '2026-01-01' and i.paid_at < date '2026-11-01'
),
-- Enterprise = plan at the time of payment, from plan history
segment as (
select p.account_id, p.month,
case when exists (
select 1 from plan_history h
where h.account_id = p.account_id
and h.plan = 'enterprise'
and h.valid_from <= p.paid_at
and (h.valid_to is null or h.valid_to > p.paid_at)
) then 'enterprise' else 'other' end as segment,
p.revenue
from paid p
join accounts a on a.id = p.account_id
where not a.is_internal -- exclude test and internal accounts
)
select month, segment,
sum(revenue) as revenue,
count(distinct account_id) as active_customers
from segment
group by month, segment
order by month, segment;Your tables will differ, so treat this as a shape to aim for rather than code to paste.
Claude CodeAbout 30 min
4. Check every number before it goes on a chart
This is the step that makes the dashboard trustworthy. Run these checks, and ask the assistant to run them too:
- Reconcile with a trusted number. Total revenue for October across both segments should match finance’s figure. If it is off, find out why before going further: currency, refunds, tax and timing are the usual causes.
- Compute it a second way. Calculate the same total with a separate, simpler query (no joins, just the invoices table with the same filters). If the two disagree, a join is multiplying or dropping rows.
- Check the grain.
count(*)grouped by month and segment should be exactly one row each. - Spot-check rows. Pick three accounts, look up their invoices by hand, and compare with what the query attributes to them.
- Check the edges. First and last month complete? Any month with suspiciously low numbers?
Write the results into the analysis file. A dashboard is only as good as the check you can point to when someone asks “where does this number come from?”.
Claude CodeAbout 30 min
5. Build the dashboard from the checked results
Keep it to what answers the question: two lines (enterprise vs other revenue by month), the same for active customers, and one headline comparison such as year-to-date growth for each segment. Put the definitions right on the page.
If your team has a BI tool (Looker, Metabase, Power BI, Tableau and the like), paste the saved queries in and build the charts there, so the numbers refresh with the data. For a one-off, a coding agent can build a single HTML page from the query results: Anthropic’s build-dashboard skill produces a self-contained HTML dashboard with KPI cards, charts, filters and a table. Check where it loads its chart library from if your company restricts external scripts. For a spreadsheet audience, the xlsx skill writes the results and charts into an Excel file.
Whatever the tool, add a footer with the data source, the query file names, the refresh date and a link to the definitions.
MeetingAbout 15 min
6. Review the dashboard with the question owner
Walk the question owner through it, starting with the definitions and the reconciliation, then the charts. Ask them to say what decision they would make from it. If their reading of a chart differs from yours, the chart or the definition needs changing.
RepositoryAbout 5 min
7. Save the analysis so it can be re-run
Commit the definitions file, the queries and the verification results to a repository or shared folder. Next quarter, someone (or an agent) can re-run the same queries and checks instead of starting from a blank prompt and getting slightly different numbers.
Check it worked
Verification checklist
0 of 8 done
When it goes wrong
If totals are higher than finance’s, look for joins that multiply rows (an account joined to many users, an invoice joined to many line items) and for refunds or tax counted as revenue. If they are lower, look for filters that drop rows, such as an inner join to a table with missing records, or timezone boundaries at month ends.
If the assistant writes SQL for the wrong dialect (date functions are the usual giveaway), name the database and version in every prompt.
If the assistant “fixes” a failed check by changing the query until the numbers match, stop it. A reconciliation check that the query is tuned to pass proves nothing. Find the reason for the gap first.
If definitions keep shifting, the question is not settled. Pause the dashboard and get a decision from the question owner.
Variations
Without database access for the assistant, paste the schema and run the queries yourself, then paste back aggregated results. It is slower but keeps raw data out of the conversation.
For a first look at an unfamiliar table, the explore-data skill profiles row counts, null rates, distributions and duplicates before you write any metric queries.
For a recurring dashboard, move the checks into the pipeline: a scheduled query that compares segment totals with the independent total and alerts when they diverge.
If the dashboard reports an experiment, do not read the winner off a chart: read an A/B test result with AI checks the split first and computes the interval in code.
Related data analysis recipes
For files rather than databases, clean a messy spreadsheet with AI uses the same reconcile-before-charting habit. The data analysis with AI skill hub covers the wider topic. To get numbers out of PDFs first, extract structured data from PDFs. More recipes are in the AI workflow examples hub.
Outcome and tools
- Outcome
- A one-page dashboard that answers a written business question, with each number backed by a saved query, a metric definition and a reconciliation check.
- For
- Data analysts, Product managers
- Variants for
- Claude Code, Codex, Claude apps
Related
- Read an A/B test result with AI without fooling yourself
Analyse A/B test results with AI safely: set the rule first, check for sample ratio mismatch, compute the interval in code, then write a decision note.
- Clean and analyse a messy spreadsheet with AI
Clean a messy spreadsheet with ChatGPT or Claude without losing rows: profile it, agree the rules, keep a cleaning log and reconcile totals before any chart.
- Draft a weekly status update from your tools
Draft an AI weekly status report from merged work, tickets and notes in your team's format, with a source for every claim and the judgement calls left to you.
- Data analysis with AI
Cleaning, exploring and charting data with an AI assistant and a repeatable method.