Skip to content

How to send a SQL report automatically every week

To send a SQL report every week you need four things: a read-only database login, a query, a scheduler and a way to deliver the result. You can build that with cron, a script and a mailer, or use a tool that already has the scheduler, the formats and the channels. Both work. The difference is who maintains it a year from now.

What a weekly SQL report needs

The job sounds small: every Monday at 8, run a query and send the rows to a few people. It has more parts than that.

  • A database login that can only read, ideally on a replica.
  • The query, written and checked by someone who knows the business.
  • A schedule with a time zone, so 8:00 means 8:00 where the readers are.
  • A format people can open: an e-mail table, CSV, Excel or PDF.
  • A channel: e-mail, a chat tool or a spreadsheet.
  • A record of what ran, and a signal when it fails.

The do-it-yourself way: cron, a script and a mailer

The classic version is a shell or Python script that runs the query, writes a CSV and mails it. A crontab line starts it.

crontab, every Monday at 08:00 server time
0 8 * * 1  /opt/reports/weekly_revenue.sh >> /var/log/weekly_revenue.log 2>&1
weekly_revenue.sh
#!/bin/shpsql "$REPORT_DB_URL" --csv -c "  SELECT region, sum(total) AS revenue  FROM orders  WHERE created_at >= date_trunc('week', now()) - interval '7 days'    AND created_at <  date_trunc('week', now())  GROUP BY 1  ORDER BY 2 DESC" > /tmp/weekly_revenue.csv echo "Weekly revenue attached." | mail -s "Weekly revenue" -A /tmp/weekly_revenue.csv team@example.com

This works on the first day. The trouble comes later, and it is predictable.

What breaksHow it shows up
CredentialsThe password sits in a script or an environment file. It expires or rotates, and the report stops with nobody told.
Silent failureCron only mails errors if mail is set up on the server. A failed run looks the same as a quiet week.
Time zonesCron runs in the server zone. Daylight saving changes or a server move shift the delivery hour.
RetriesIf the database is down at 08:00, the report is simply skipped. Nobody retries.
FormattingCSV opens badly in Excel with the wrong separator or encoding. Building a nice table or PDF means more code.
Empty weeksZero rows still sends a message, and people learn to ignore it.
PeopleThe author leaves, the server is retired, and nobody knows the report existed until someone asks where it went.

None of this is hard to fix one at a time. The cost is that you now own a small piece of infrastructure for every report.

The DataRunner way, step by step

DataRunner takes the scheduler, the formats, the channels, the run history and the failure alert off your hands. You supply the database and the question. These are the steps.

  1. Connect the database. Create the account (the Solo plan is free, with no card), add the connection and test it. If the database is in a private network, install the bridge agent, which opens the connection from inside out, so no inbound port is needed. Use a read-only login.
  2. Write the query, or ask the assistant. You can paste SQL, or describe the report in plain words, such as "revenue by region for the last 7 days, biggest first". The assistant reads only the schema (tables, columns, types and column descriptions). No rows of your data are sent to the model.
  3. Approve the SQL. Every proposal appears on screen and nothing is created until you approve it item by item. Run it once in the workbench to see the result and the row count.
  4. Schedule it. Set a cron expression, for example 0 8 * * 1 for Monday at 08:00, and choose the time zone of the schedule. The time zone belongs to the schedule, so the same report can go at 08:00 in São Paulo and in Lisbon.
  5. Pick the channel and the format. E-mail, Slack, WhatsApp (beta), Telegram, Discord, a signed webhook or Google Sheets. For the file, choose inline HTML, CSV, Excel or PDF.
  6. Skip empty runs. Add the delivery rule that skips the send when the result is empty, or sends only above a row count, so people only get the message when there is something to read.
  7. Check the run history. Each trigger records start time, duration, rows returned and the database error if there was one. A failed run does not switch the schedule off, and you can route a failure alert to a channel.

E-mail sends through the mail provider you configure for the workspace (SMTP or Resend), from your own domain. There is no fallback to a platform sender, so a workspace without a provider fails the delivery instead of sending from a domain you did not choose.

Choosing the format

FormatUse it when
Inline HTMLA few rows that people read on a phone without opening an attachment
CSVThe file feeds another system, or the volume is large
Excel (XLSX)The reader will filter, sum and copy the data
PDFThe document must look the same for everyone

For a manager who reads on a phone, a short inline table beats an attachment. For finance, Excel is usually the file they want. Decide by what the reader does next with the numbers.

Write the query so the report stays correct

Most wrong reports come from the date range, not from the tool. Use half-open ranges that end at a fixed boundary, and do not depend on "now" in the middle of a week.

Last full week, Monday to Monday
SELECT region,       count(*)   AS orders,       sum(total) AS revenueFROM ordersWHERE created_at >= date_trunc('week', now()) - interval '7 days'  AND created_at <  date_trunc('week', now())GROUP BY regionORDER BY revenue DESC;
  • Name the columns the way the reader says them. "revenue" beats "sum".
  • Sort the rows. A report with random order is hard to compare week to week.
  • Limit the rows when only the top matters.
  • Mind the time zone of the database against the time zone of the schedule when you cut the week.

When the script is the better choice

A script is fine if you have one report, one reader, a team that already runs jobs and a place to see failures. It is also the right choice when the output needs custom code, such as a chart rendered by your own library, or a step that changes data.

A tool pays off when the reports multiply, when the reader is not technical, or when the person who wrote the script is not the person who will fix it. DataRunner is also not a BI or ETL replacement: its dashboards are saved SQL drawn as table, number, line, bar or pie, and it does not move data between systems.

Frequently asked questions

How do I send a SQL query result by e-mail every week?
Write the query, run it on a schedule such as every Monday at 08:00, and deliver the result as an e-mail table or an attached CSV, Excel or PDF file. You can do this with cron, a script and a mailer, or with a tool like DataRunner that holds the schedule, the formats and the delivery.
Can I schedule a SQL query without writing code?
Yes. In DataRunner you describe the report in plain words and the assistant proposes the SQL, which you approve before anything runs. You then pick the schedule, the time zone and the channel in the console.
What happens if the database is down when the report runs?
The run is marked failed with the message the database returned, and the schedule stays active for the next time. You can route a failure alert to a channel, and a database monitor opens an incident that lists the reports that depend on it.
Can I stop the report from sending when there is no data?
Yes. A delivery rule skips the send when the result comes back empty, or sends only above a row count. That keeps the weekly message meaningful instead of something people filter out.
Does the AI see my data?
The assistant sees your request and the structure of the database: table names, column names and types, and descriptions you wrote on columns. Rows of your data are not sent to the model. The assistant is optional, and the product works the same if you write the SQL yourself.