Variables
A variable binds a value to a name with let and is referenced as $name anywhere in the request - in expressions, in pipelines, in later statements. Variables are what turn a one-shot query into a script: compute a value once, then filter, branch, or write with it, all without leaving RQL.
Bind once, use anywhere
A let statement ends with a semicolon like any other statement, and the binding is visible to everything after it in the same request. Here the threshold is named once instead of being repeated through the pipeline. Run the snippets on this page in order:
Reassignment
After a let, plain assignment ($x = ...) replaces the value. This is what loop bodies use to accumulate - see for and its siblings:
Assignment does not declare. Writing to a name that was never bound with let is an error, so a typo in a variable name fails loudly instead of silently creating a second variable:
Frame variables
A variable can hold a whole query result, not just a scalar. Binding let $items = from cf_var::items; captures the frame; a later from $items reads it like any other source, with the usual transforms applied after it:
When the frame has a single row, a field access reads one value out of it. This is the idiom for configuration tables - fetch the row once, then use its fields as scalars:
from source and apply transforms after from $var. In the current build, putting transforms inside the let itself (let $x = from t filter { ... }) is not supported.Scoping
Every block ({ ... }) is a scope. A let inside an if or loop body declares a new variable that shadows the outer one until the block ends - it does not overwrite it. To change an outer variable from inside a block, assign ($x = ...) instead of re-declaring:
Closures interact with scopes too: they capture the variables visible where they are defined.
let to the end of the request - there is no session state to clean up, and nothing leaks between requests. For the rest of the scripting toolkit, start at the Control Flow overview.