Skip to content

SQL editor

In the SQL editor you write a query over the report views your connection can read, preview the result and save the query you like as a dataset. Queries run read-only; you cannot change any data.

If you do not know SQL, skip this page and generate a dataset from a form: Generating a dataset from a form.

Reports → SQL editor, or Open in the SQL editor on a connection’s card. You need the permission to create datasets.

If you have no connection yet, the page says Add a connection first; Go to connections takes you to the connections page.

  • Connection: At the top you choose which connection the query runs on. Each option shows the connection’s name and status. If you pick a connection that is not ready, the warning “This connection has not passed its readiness check. Check it on the connections page before running a query.” appears.
  • Views (left): The report views the connection can read. Search them with the Search views or columns box.
    • Clicking a view’s name adds it to the query.
    • Opening a view shows its columns and column count; clicking a column adds it to the query.
  • SQL query (middle): The text of the query.
  • Result (bottom): The rows, once you run the query.
  1. Click a view in the list on the left, or type the query.
  2. Click Run or press Ctrl+Enter (⌘+Enter on a Mac).
  3. If the query takes long, stop it with Cancel query (“The query was cancelled.”).
  4. The Result section shows:
    • the row count and time (“250 rows · 84 ms”),
    • the Columns list (each column’s name and type),
    • the table of rows. Empty values read “null”.
  5. You can download the result table as a file with Download CSV.

The preview shows at most 1,000 rows. If the query returns more, it says “Only the first 1000 rows are shown; the query returns more.” This limit applies only to the preview; charts group and total the data themselves.

The The query that ran (as the server rewrote it) section shows the query as Defthand reformatted it for safety.

Gets the inspections together with location names:

SELECT i.inspection_id,
l.name_tr AS lokasyon,
i.status,
i.submitted_at,
i.score_percent,
i.passed
FROM reporting.inspections i
JOIN reporting.locations l ON l.location_id = i.location_id
WHERE i.submitted_at IS NOT NULL
  • Only one SELECT query runs. Queries starting with WITH work too, but a WITH part that changes data is not allowed.
  • At most 100,000 characters.
  • SELECT … INTO, FOR UPDATE / FOR SHARE and parameters such as $1 are not allowed; write the value into the query.
  • Some functions cannot be used in reports; if you use one, Defthand tells you which.
  • A query runs for at most 30 seconds.
  • When many report queries are running at once, the message “Too many report queries are running right now. Try again in a moment.” appears.
Message What to do
“The query is empty.” Write a query.
“Only one SELECT query can run.” Remove extra statements or INSERT/UPDATE.
“Syntax error: …” / “The query has a syntax error.” Fix the spelling at the place shown.
“A table or view in the query does not exist or is not open to this user.” Check the name in the list on the left; if the view is not listed, your connection cannot read it.
“A column in the query does not exist.” Check the column name among the view’s columns.
“The connection’s database user may not read this data.” Ask the Defthand team for access to this topic.
“The query ran out of time. Try a query that reads less data.” Narrow the rows with WHERE.
“The query divides by zero.” Use NULLIF(…, 0) in the divisor.
“Two columns have the same name: …. Rename one with AS.” Give one of the columns a new name with AS.
  1. When the query gives the result you want, click Save as dataset.
  2. The dialog says “The server checks the query again and reads its columns from it.” Fill in:
    • Name (required, at most 100 characters), e.g. “Inspections and locations”.
    • Description (at most 1000 characters).
    • Cache time (seconds): 0 to 86,400; default 300.
  3. Click Save. The message “Dataset saved.” appears.

Next step: Datasets.