> ## Documentation Index
> Fetch the complete documentation index at: https://powersync-wildcard-schemas-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Grammar Reference (Sync Rules)

> Railroad diagram reference for the SQL grammar supported in legacy Sync Rules queries.

This page is a formal grammar reference for Sync Rules: it shows the syntax accepted for parameter queries and data queries using railroad diagrams. This page complements the [Supported SQL](/sync/supported-sql) guide, which explains in prose what you can write, with examples and restrictions.

**When to use this page:** If you need to check whether a construct is valid, see how parameter vs data query syntax differs, or you're used to grammar specs, use the diagrams and the "Used by" / "References" links to navigate. For most users just getting started, see [Supported SQL](/sync/supported-sql) and the [Sync Rules](/sync/rules/overview) docs.

## ParameterQuery

```sql theme={null}
SELECT id FROM users WHERE id = token_parameters.user_id
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--ParameterQuery.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=e3356fea85f1ac1b1518f1ee5828780c" alt="ParameterQuery syntax diagram" width="341" height="122" data-path="sync/grammar/sync-rules/bucket-definitions--ParameterQuery.svg" />

**References:** [StaticParameterQuery](#staticparameterquery), [TableParameterQuery](#tableparameterquery), [TableValuedParameterQuery](#tablevaluedparameterquery)

***

## TableValuedParameterQuery

```sql theme={null}
SELECT value FROM json_each(token_parameters.tags)
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--TableValuedParameterQuery.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=909f11290a237860b0abc20f91d9c245" alt="TableValuedParameterQuery syntax diagram" width="981" height="102" data-path="sync/grammar/sync-rules/bucket-definitions--TableValuedParameterQuery.svg" />

**References:** [Identifier](#lexical-rules), [JsonEachCall](#jsoneachcall), [SelectItem](#selectitem), [WhereClause](#whereclause)

<Accordion title="Used by">
  [ParameterQuery](#parameterquery)
</Accordion>

***

## TableParameterQuery

```sql theme={null}
SELECT id, name FROM users WHERE org_id = token_parameters.org_id
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--TableParameterQuery.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=a328139785478e2cc1859ce58b412e45" alt="TableParameterQuery syntax diagram" width="741" height="102" data-path="sync/grammar/sync-rules/bucket-definitions--TableParameterQuery.svg" />

**References:** [Reference](#reference), [SelectItem](#selectitem), [WhereClause](#whereclause)

<Accordion title="Used by">
  [ParameterQuery](#parameterquery)
</Accordion>

***

## StaticParameterQuery

```sql theme={null}
SELECT token_parameters.user_id AS id
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--StaticParameterQuery.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=bc7f8dde36460495a54d8714219a6bda" alt="StaticParameterQuery syntax diagram" width="557" height="102" data-path="sync/grammar/sync-rules/bucket-definitions--StaticParameterQuery.svg" />

**References:** [SelectItem](#selectitem), [WhereClause](#whereclause)

<Accordion title="Used by">
  [ParameterQuery](#parameterquery)
</Accordion>

***

## DataQuery

```sql theme={null}
SELECT id, name FROM users WHERE users.id = bucket.user_id
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--DataQuery.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=43ce004de512b5bba3a434a1b802f0f2" alt="DataQuery syntax diagram" width="781" height="132" data-path="sync/grammar/sync-rules/bucket-definitions--DataQuery.svg" />

**References:** [Reference](#reference), [SelectItem](#selectitem), [WhereClause](#whereclause)

***

## SelectItem

```sql theme={null}
name AS user_name
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--SelectItem.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=5f395b508a96c3198ec8d3762d42bf2d" alt="SelectItem syntax diagram" width="417" height="72" data-path="sync/grammar/sync-rules/bucket-definitions--SelectItem.svg" />

**References:** [Expression](#expression), [Identifier](#lexical-rules)

<Accordion title="Used by">
  [TableValuedParameterQuery](#tablevaluedparameterquery), [TableParameterQuery](#tableparameterquery), [StaticParameterQuery](#staticparameterquery), [DataQuery](#dataquery)
</Accordion>

***

## JsonEachCall

```sql theme={null}
JSON_EACH(token_parameters.tags)
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--JsonEachCall.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=3c7e50ba32539a0879781e3fa1cffbec" alt="JsonEachCall syntax diagram" width="409" height="62" data-path="sync/grammar/sync-rules/bucket-definitions--JsonEachCall.svg" />

**References:** [Expression](#expression)

<Accordion title="Used by">
  [TableValuedParameterQuery](#tablevaluedparameterquery)
</Accordion>

***

## WhereClause

```sql theme={null}
users.id = bucket.user_id AND active = true
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--WhereClause.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=81cf2b36155598493eec8d3be52fbf5c" alt="WhereClause syntax diagram" width="489" height="162" data-path="sync/grammar/sync-rules/bucket-definitions--WhereClause.svg" />

**References:** [Predicate](#predicate), [WhereClause](#whereclause)

<Accordion title="Used by">
  [TableValuedParameterQuery](#tablevaluedparameterquery), [TableParameterQuery](#tableparameterquery), [StaticParameterQuery](#staticparameterquery), [DataQuery](#dataquery), [WhereClause](#whereclause)
</Accordion>

***

## Predicate

```sql theme={null}
status IS NOT NULL
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--Predicate.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=fafa95e6428f6c97b07660c729b9d63d" alt="Predicate syntax diagram" width="493" height="192" data-path="sync/grammar/sync-rules/bucket-definitions--Predicate.svg" />

**References:** [Expression](#expression)

<Accordion title="Used by">
  [WhereClause](#whereclause)
</Accordion>

***

## Expression

```sql theme={null}
price * quantity + tax
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--Expression.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=ee9f50fadffefefc5efb47aa14a2d110" alt="Expression syntax diagram" width="957" height="91" data-path="sync/grammar/sync-rules/bucket-definitions--Expression.svg" />

### Operators

Binary operators supported in scalar expressions, listed from highest to lowest precedence.

<Note>
  PowerSync evaluates all binary operators with equal precedence (left to right). Use parentheses to control evaluation order.
</Note>

| Precedence | Operators          | Description                       |
| ---------- | ------------------ | --------------------------------- |
| 1          | `\|\|`             | String concatenation              |
| 2          | `*` `/` `%`        | Multiplication, division, modulo  |
| 3          | `+` `-`            | Addition, subtraction             |
| 4          | `&` `\|` `<<` `>>` | Bitwise AND, OR, left/right shift |
| 5          | `<` `>` `<=` `>=`  | Less than, greater than, etc.     |
| 6          | `=` `!=`           | Equal, not equal                  |

**References:** [PrimaryExpression](#primaryexpression), [PropertyAccess](#propertyaccess)

<Accordion title="Used by">
  [SelectItem](#selectitem), [JsonEachCall](#jsoneachcall), [Predicate](#predicate), [CastExpression](#castexpression), [FunctionCall](#functioncall), [PrimaryExpression](#primaryexpression)
</Accordion>

***

## PropertyAccess

```sql theme={null}
->'address'->>'city'
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--PropertyAccess.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=aa9036f9db05da94a0834ceb993649c5" alt="PropertyAccess syntax diagram" width="377" height="100" data-path="sync/grammar/sync-rules/bucket-definitions--PropertyAccess.svg" />

**References:** [IntegerLiteral](#lexical-rules), [StringLiteral](#lexical-rules)

<Accordion title="Used by">
  [Expression](#expression)
</Accordion>

***

## Reference

```sql theme={null}
users.id
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--Reference.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=e41b42d980cae6eab2ec4ad2c7da8992" alt="Reference syntax diagram" width="409" height="72" data-path="sync/grammar/sync-rules/bucket-definitions--Reference.svg" />

**References:** [Identifier](#lexical-rules)

<Accordion title="Used by">
  [TableParameterQuery](#tableparameterquery), [DataQuery](#dataquery), [FunctionCall](#functioncall), [PrimaryExpression](#primaryexpression)
</Accordion>

***

## CastExpression

```sql theme={null}
CAST(age AS TEXT)
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/SNubE5Vw0rxNSlPw/sync/grammar/sync-rules/bucket-definitions--CastExpression.svg?fit=max&auto=format&n=SNubE5Vw0rxNSlPw&q=85&s=fbfdfdee11f9dc60e6459618d1756831" alt="CastExpression syntax diagram" width="541" height="182" data-path="sync/grammar/sync-rules/bucket-definitions--CastExpression.svg" />

**References:** [Expression](#expression)

<Accordion title="Used by">
  [PrimaryExpression](#primaryexpression)
</Accordion>

***

## FunctionCall

```sql theme={null}
upper(name)
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--FunctionCall.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=45e51e06c5b16cd46501b909b9bddba8" alt="FunctionCall syntax diagram" width="449" height="102" data-path="sync/grammar/sync-rules/bucket-definitions--FunctionCall.svg" />

**References:** [Expression](#expression), [Reference](#reference)

<Accordion title="Used by">
  [PrimaryExpression](#primaryexpression)
</Accordion>

***

## PrimaryExpression

```sql theme={null}
(price + tax)
```

<img src="https://mintcdn.com/powersync-wildcard-schemas-docs/lk4Q8D6N8OkZEpSJ/sync/grammar/sync-rules/bucket-definitions--PrimaryExpression.svg?fit=max&auto=format&n=lk4Q8D6N8OkZEpSJ&q=85&s=ac1796f3c1749d1f143ecc883e567501" alt="PrimaryExpression syntax diagram" width="337" height="302" data-path="sync/grammar/sync-rules/bucket-definitions--PrimaryExpression.svg" />

**References:** [CastExpression](#castexpression), [Expression](#expression), [FunctionCall](#functioncall), [NumericLiteral](#lexical-rules), [Reference](#reference), [StringLiteral](#lexical-rules)

<Accordion title="Used by">
  [Expression](#expression)
</Accordion>

***

## Lexical Rules

| Token                             | Examples                                                           | Rule                                    |
| --------------------------------- | ------------------------------------------------------------------ | --------------------------------------- |
| [Identifier](#identifier)         | `user_id`, `MY_TABLE`, `"Column Name"`, `"with ""quotes"" inside"` | `[A-Z_] ([A-Z_0-9])*`                   |
| [StringLiteral](#stringliteral)   | `'hello'`, `'it''s'`, `''`                                         | `"'" ([\x20-\x26] \| [\x28-\x7E])* "'"` |
| [IntegerLiteral](#integerliteral) | `0`, `42`, `12345`                                                 | `([0-9])+`                              |
| [NumericLiteral](#numericliteral) | `3.14`, `42`, `0.5`                                                | `([0-9])+ ("." ([0-9])+)?`              |

### Identifier

Bare identifiers are normalized to lower case and may contain letters, digits, and underscores. Double-quoted identifiers ("name") allow any printable character and support escaped quotes ("").

<Accordion title="Used by">
  [TableValuedParameterQuery](#tablevaluedparameterquery), [SelectItem](#selectitem), [Reference](#reference)
</Accordion>

### StringLiteral

Single-quoted string literal. Embedded single quotes are escaped by doubling them ('').

<Accordion title="Used by">
  [PropertyAccess](#propertyaccess), [PrimaryExpression](#primaryexpression)
</Accordion>

### IntegerLiteral

One or more decimal digits (0-9).

<Accordion title="Used by">
  [PropertyAccess](#propertyaccess)
</Accordion>

### NumericLiteral

Decimal number: one or more digits with an optional fractional part (.digits).

<Accordion title="Used by">
  [PrimaryExpression](#primaryexpression)
</Accordion>
