Write SQL

1 min read

Queries use read-only PostgreSQL SQL on the workspace tables.

Rules

  • Use each table's and column's slug: SELECT cliente FROM despachos.
  • Read only: INSERT, UPDATE, DELETE, CREATE, DROP, ALTER and other statements that change data or structure aren't allowed.
  • You can use JOIN, GROUP BY, subqueries and WITH (a CTE can't have the same name as a table).
  • The Tables panel lists the tables and columns you can use; click to insert them.

Variables

Type {{ to insert variables:

Variable Value
{{workspace.config_var.<slug>}} A configuration variable (not secret).
{{current_user.id}}, .name, .email, .role Who runs it.
{{current_user.attributes.<key>}} A member attribute.
{{params.<name>}} A parameter from a workflow's Run query block or the test value.
{{visitor.email}}, {{visitor.<field>}} The visitor of a dashboard shared by link.

KoData adds the quotes: write estado = {{params.estado}}. See Guide: query with variables.

Common errors

Message What to do
Column "…" doesn't exist in "…" Use the right slug; KoData suggests the closest one.
"…" is in more than one table Name the table: alias.column.
"…" must be grouped Add it to GROUP BY or use it inside SUM, COUNT…
Syntax error near "…" Check commas, parentheses and quotes near that spot.
Only read-only SELECT statements are allowed. Remove the statement that writes data.
Secret variable "…" can't be used in a query Use non-secret variables in SQL.

With AI on, Ask Query AI to fix it proposes the fix.