Skip to content

Write a dataset's SQL

By the end of this page you will have set a dataset up from a query you wrote, checked what it answers, and saved it. You need an author’s, admin’s or owner’s role. An owner or admin adds the connection first (Connections), and its latest test must show that it refuses a write.

BInbox runs your query in the client’s warehouse, so the warehouse itself must stop a query that would change data. When an owner or admin tests a connection, BInbox tries to delete no rows from one table. The test report lists “Refuse a write, so SQL an author writes can run” as granted only when the warehouse refuses that delete with its own refusal. On PostgreSQL and Redshift, BInbox runs every query in a read-only transaction, and the delete is tried inside one. On BigQuery, BInbox’s token has the read-only scope, which refuses the delete. On Snowflake, Databricks, Azure SQL Database, Azure Synapse and Microsoft Fabric no read-only session exists, so the role’s grants are the control: the test shows the first table refused, and a role that may write to another table can still write there. Each warehouse’s page under Connect says how its test works. A connection whose latest test did not show a refused write says so on the screen, Preview stays off, and its datasets are picked as tables (Pick a table).

  1. On the Data tab, choose Connect, then choose the connection.
  2. Choose Write SQL.
  3. Type one query that starts with SELECT or WITH in The dataset’s SQL. As you type, BInbox lists anything it will not run, in words that say what to change: a semicolon (more than one statement), a word that writes such as DELETE or INSERT, a parenthesis that never closes, or a placeholder such as $1. A column named like one of those words goes in quotes.
  4. Choose Preview. BInbox runs the query inside its own query and shows how many rows it answers, its columns sorted by their types as Pick a table describes, and its first 20 rows.

When you change the query, the screen says the preview is out of date. Choose Preview again.

A long query reads more easily as named steps. Each step is one SELECT with a name. The steps below it and The dataset’s SQL read it by its name, as a table. BInbox runs the steps and the dataset’s SQL as one query, so the figures are the same as the same SQL written as one query.

  1. Under Steps, choose Write it in steps. A step opens above the dataset’s SQL.
  2. Type its Name: lower-case letters, digits and _, starting with a letter, such as lines or store_segments.
  3. Type the step’s SQL in Its SQL. It starts with SELECT and reads tables, or steps above it by their names.
  4. Choose Add a step for the next one. Move up, Move down and Remove change the steps’ order.
  5. In The dataset’s SQL, read the steps by their names, such as SELECT * FROM joined.

Under each step, the screen says which steps it reads. It lists, in words, what to change before the step can run:

  • A step that reads a step below it, or its own name. A step reads only the steps above it.
  • A step that reads a step you renamed or removed. The words name the step’s new name when it has one.
  • A step that no step below it reads, and the dataset’s SQL does not read either. Read it, or remove it.
  • A name used twice, or one SQL reads as its own word, such as select.

A dataset keeps up to 12 steps. On Azure SQL Database, Azure Synapse and Microsoft Fabric a dataset’s query runs inside another query and cannot start with WITH, so it cannot be written in steps. Write each step as a subquery there, or save it as a view.

Preview this step runs the steps above it and the step itself, and shows how many rows the step answers, its columns and its first 10 rows.

After a preview, choose the step’s grain: the columns that name one of its rows, such as a line, or a store and a month. Check the grain counts the values that come more than once. When a join repeats rows, such as a store list that holds a store twice, the check names the repeated values, so you can find the step where the rows were repeated. Leave the grain empty where a value is meant to repeat, such as an order’s id on its lines.

A change to any step is a new version of the dataset’s SQL. Preview the whole SQL again, and mark its join again if it had a mark.

Under the query, Ask Claude writes the query from your words, or changes the query that is there. It is there when setup help from a model is on for the client account. With that setting off, the box says so, and nothing is sent.

  1. With the query box empty, type what the query should answer in What the SQL should answer, such as “Monthly revenue by region with the client’s segment from the segments table”. With a query in the box, type what to change in What to change in the SQL, such as “Only the Online channel”.
  2. When the connection has more than one schema, choose the one whose tables Claude should use in Tables from schema.
  3. Choose Draft with Claude, or Ask Claude to change it. Claude answers in up to a minute. Stop ends the wait.

Claude’s answer carries the Suggested mark until you use it. Nothing in the query box changes until then.

  • A new query shows under Claude’s draft, as it would be saved. Choose Use this SQL to put it in the box, or Discard.
  • A change shows side by side: Your SQL on the left, After the change on the right, with the lines taken out and put in marked. Choose Use the change, or Keep mine. If you type in the query box after asking, the change is not offered, because it was made on the older text. Ask again.

Claude is told what BInbox will refuse, so most queries it writes pass the check for the warehouse. When one does not, the answer shows the check’s words and cannot be used or saved. Ask Claude to fix it asks again with those words.

Every query Claude writes or changes starts with this line:

-- Generated with Claude Code. You can delete this line.

It is a comment, so the warehouse and BInbox’s check read past it. You can delete it. Deleting it is an edit like any other: it makes a new version of the query, so preview it again, and a mark on its join clears. When Claude changes a query that has the line, the answer keeps one line, not two.

  • The names of the tables and views in the schema you chose, up to 400.
  • Their columns by name and type. In a schema of more than 20 tables, only the columns of the tables your words or your query name, up to 20 tables.
  • The warehouse’s kind, such as PostgreSQL, so the query is written in its own SQL.
  • Your words, with email addresses taken out, and for a change, your query as written without the line above, with any value you typed in it.

BInbox reads no row from the data to send. A query that holds an email address is not sent, and neither is a query longer than the 20,000 characters a dataset keeps. When Send 20 sample rows and each column’s values is cleared for the account, a query that writes rows with VALUES is not sent either. Take those out, or change the query by hand. Each ask counts toward the account’s daily limits, and Activity records it with the counts of tables and columns sent, never your words, your query or a table’s name.

Two checks sit under Check it. Choose Check to run them.

  • Columns that name one row: choose the columns that together name one row, such as a line number, or an order and its line. BInbox counts the values that come more than once. A join to a list that holds a key twice repeats every amount it meets, so a repeated key is the first sign of a wrong join. An order can hold the same product twice, so an order and a product are often not a key.
  • A figure you trust: choose a number column. BInbox shows its total over every row, so you can hold it against a number you know, such as last year’s revenue from the client’s own books.

A report sent with recipient rules gives each recipient a copy cut to their own rows, such as their region. The rule keeps the rows your query answers for that recipient. It cannot take another recipient’s share out of a figure the query already worked out, so some queries cannot be sent with recipient rules:

  • A window (OVER), GROUP BY, HAVING, DISTINCT, a subquery, WITH, UNION, a row limit such as LIMIT, CONNECT BY, a function BInbox does not know to work one row at a time (including any function called by a name in quotes), and CROSS, FULL, RIGHT, NATURAL or lateral joins and a join ON TRUE are always refused under recipient rules. Do that step in a view in the warehouse, or send the report to everyone alike.
  • An INNER or LEFT join with ON or USING is allowed only when you mark it. The screen shows The join, under recipient rules when your query has one.

With steps, each step is judged as if it were written inline. Steps that each work one row at a time can be sent with recipient rules, though BInbox joins them with a WITH. Anything in the list above, such as a window or GROUP BY, refuses the dataset in whichever step it is, and a join in any step needs a mark.

To bring a second table’s figures in with no join, add that table beside this one in the setup instead.

A mark needs two things on the query as it is now:

  1. One recipient’s slice. Choose The column a rule cuts on, type One recipient’s value, and choose Preview the slice. BInbox shows how many rows that recipient’s copy is built from, and its first rows.
  2. A key check that found no value twice, under Check it.

Then choose one mark. A join takes one mark or the other, so setting one clears the other.

  • Mark the join as a lookup when the join only lays labels on the rows, such as a client’s segments, a region’s name, or the client’s own dimensions laid onto syndicated rows. No figure comes from the other table.
  • Mark the join as bringing in figures when the joined table adds figures. Choose each figure column the join brings in, choose The date that sets the period, and choose Check the joined figures. BInbox flags a figure that had the same value for 2 or more recipients in a month, such as a region’s budget joined onto each store’s rows, because such a figure was probably measured over a group of them or all of them, and each recipient would then see the others’ amounts inside it. The flag says which: “one value for all 4 recipients”, or “the same value for 3 of 12 recipients”. A value two recipients share by chance, such as 0, flags too. To mark the join past a flag, write why in Why it is right to show.

A notice beside the marks says to confirm the output before you distribute a report that uses the client’s own data. With joined figures, you are responsible for what each recipient’s output shows. Clear the mark removes a mark.

A mark belongs to one version of the query on its connection. When the query or its columns change, or the same query is saved on another connection, the mark is cleared, and the screen says so. Preview a slice, check the keys and mark the join again.

A mark stands for copies cut by the column of its slice. Preview the slice by the column of the level you send by, such as the store column for copies by store.

Choose Open on the Dashboard. BInbox reads the first 10,000 rows for the setup, as it does for a table, and adds the figures up in the warehouse. Save it as you save any dataset.

  • The saved version keeps the query exactly as you wrote it and the columns it answered then. If the query later answers other columns, such as a table that gained one under SELECT *, the dataset shows “Not available for this data” and asks you to save the dataset’s SQL again.
  • A dataset from SQL is read live. It has no extract schedule and no check for new data, because both read a table by its name.
  • Each save is a new version. The account’s log names the version, who saved it and when, and each mark with who set it, when, and the reason given for going past a flag. The log never holds the query’s text.
  • In the Data view, Source shows the saved query, or each step and the dataset’s SQL, with its version and its marks. Change the SQL opens the query again on its connection.

A report whose dataset has a refused query, or a join with no mark, is refused when you publish it with recipient rules or send a test of it, with the reason. So is a report whose query on screen is not the saved version, such as a query changed and opened on the Dashboard without Save: save it first. Sending it to everyone alike is not affected.