> ## Documentation Index
> Fetch the complete documentation index at: https://powersync-document-fixed-booleans-in-json.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Grammar Reference (Sync Streams)

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

This page is a formal grammar reference for Sync Streams: it shows the syntax the compiler accepts 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 rules nest (e.g. subqueries, CTEs, JOINs), 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 [Writing Queries](/sync/streams/queries).

## SelectStatement

```sql theme={null}
SELECT id, name FROM users WHERE active = true
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--SelectStatement.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=1b1f9d623f6a5c8851374d684f7065b7" alt="SelectStatement syntax diagram" width="1017" height="110" data-path="sync/grammar/sync-streams/sync-streams-compiler--SelectStatement.svg" />

**References:** [FromSource](#fromsource), [JoinClause](#joinclause), [SelectItem](#selectitem), [WhereClause](#whereclause)

***

## SelectItem

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--SelectItem.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=cca8fb3ee149b5d376f0da8343f8f2ab" alt="SelectItem syntax diagram" width="457" height="132" data-path="sync/grammar/sync-streams/sync-streams-compiler--SelectItem.svg" />

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

<Accordion title="Used by">
  [SelectStatement](#selectstatement), [Subquery](#subquery)
</Accordion>

***

## Reference

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--Reference.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=bd66e7d0e44b869527e8756a847191ff" alt="Reference syntax diagram" width="409" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--Reference.svg" />

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

<Accordion title="Used by">
  [SelectItem](#selectitem), [TableSource](#tablesource), [PrimaryExpression](#primaryexpression), [FunctionCall](#functioncall)
</Accordion>

***

## FromSource

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--FromSource.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=4cd0a5dfd347414b1c2f3894b3ade88a" alt="FromSource syntax diagram" width="277" height="122" data-path="sync/grammar/sync-streams/sync-streams-compiler--FromSource.svg" />

**References:** [SubquerySource](#subquerysource), [TableSource](#tablesource), [TableValuedSource](#tablevaluedsource)

<Accordion title="Used by">
  [SelectStatement](#selectstatement), [JoinClause](#joinclause), [Subquery](#subquery), [CteDefinition](#ctedefinition)
</Accordion>

***

## TableSource

```sql theme={null}
users AS u
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--TableSource.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=b3f578ebe5ee797fd5e34b46f1f515bb" alt="TableSource syntax diagram" width="409" height="92" data-path="sync/grammar/sync-streams/sync-streams-compiler--TableSource.svg" />

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

<Accordion title="Used by">
  [FromSource](#fromsource)
</Accordion>

***

## TableValuedCall

```sql theme={null}
json_each(data)
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--TableValuedCall.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=78be974309a9ac7651a81f1604634fe2" alt="TableValuedCall syntax diagram" width="457" height="102" data-path="sync/grammar/sync-streams/sync-streams-compiler--TableValuedCall.svg" />

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

<Accordion title="Used by">
  [TableValuedSource](#tablevaluedsource)
</Accordion>

***

## TableValuedSource

```sql theme={null}
json_each(data) AS items
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--TableValuedSource.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=354cdf34bc431740ff23957e7df163a4" alt="TableValuedSource syntax diagram" width="457" height="92" data-path="sync/grammar/sync-streams/sync-streams-compiler--TableValuedSource.svg" />

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

<Accordion title="Used by">
  [FromSource](#fromsource)
</Accordion>

***

## SubquerySource

```sql theme={null}
(SELECT id FROM users) AS u
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--SubquerySource.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=0f2b217fdb8487611950e454ef046843" alt="SubquerySource syntax diagram" width="733" height="102" data-path="sync/grammar/sync-streams/sync-streams-compiler--SubquerySource.svg" />

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

<Accordion title="Used by">
  [FromSource](#fromsource)
</Accordion>

***

## JoinClause

```sql theme={null}
JOIN orders ON users.id = orders.user_id
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--JoinClause.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=7f69b18dbd926e0d4d6847b2b580ab13" alt="JoinClause syntax diagram" width="597" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--JoinClause.svg" />

**References:** [FromSource](#fromsource), [WhereClause](#whereclause)

<Accordion title="Used by">
  [SelectStatement](#selectstatement), [Subquery](#subquery), [CteDefinition](#ctedefinition)
</Accordion>

***

## WhereClause

```sql theme={null}
active = true AND age > 18
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--WhereClause.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=1fdcf3daf0b8317d21c41306abaf8cec" alt="WhereClause syntax diagram" width="337" height="132" data-path="sync/grammar/sync-streams/sync-streams-compiler--WhereClause.svg" />

**References:** [Condition](#condition)

<Accordion title="Used by">
  [SelectStatement](#selectstatement), [JoinClause](#joinclause), [Condition](#condition), [Subquery](#subquery), [CteDefinition](#ctedefinition)
</Accordion>

***

## Condition

```sql theme={null}
age BETWEEN 10 AND 100
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--Condition.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=dab4c108b39d7562befdab87343a8a53" alt="Condition syntax diagram" width="425" height="122" data-path="sync/grammar/sync-streams/sync-streams-compiler--Condition.svg" />

**References:** [Expression](#expression), [PredicateTail](#predicatetail), [WhereClause](#whereclause)

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

***

## PredicateTail

```sql theme={null}
IN (SELECT id FROM users)
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--PredicateTail.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=8351537d157326c867c4d99a1fa3e14d" alt="PredicateTail syntax diagram" width="605" height="232" data-path="sync/grammar/sync-streams/sync-streams-compiler--PredicateTail.svg" />

**References:** [Expression](#expression), [InSource](#insource)

<Accordion title="Used by">
  [Condition](#condition)
</Accordion>

***

## InSource

```sql theme={null}
(SELECT id FROM users)
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--InSource.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=03464416e48f502bad0d455012302209" alt="InSource syntax diagram" width="321" height="122" data-path="sync/grammar/sync-streams/sync-streams-compiler--InSource.svg" />

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

<Accordion title="Used by">
  [PredicateTail](#predicatetail)
</Accordion>

***

## Expression

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--Expression.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=9f18770169ea1fecfdb224bf18450a6b" alt="Expression syntax diagram" width="957" height="91" data-path="sync/grammar/sync-streams/sync-streams-compiler--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), [TableValuedCall](#tablevaluedcall), [Condition](#condition), [PredicateTail](#predicatetail), [InSource](#insource), [PrimaryExpression](#primaryexpression), [SearchedCase](#searchedcase), [WhenClause](#whenclause), [SimpleCase](#simplecase), [WhenValueClause](#whenvalueclause), [CastExpression](#castexpression), [FunctionCall](#functioncall), [CteDefinition](#ctedefinition)
</Accordion>

***

## PropertyAccess

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--PropertyAccess.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=76732727f3f2e1fea1c8b309f931781d" alt="PropertyAccess syntax diagram" width="377" height="100" data-path="sync/grammar/sync-streams/sync-streams-compiler--PropertyAccess.svg" />

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

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

***

## PrimaryExpression

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--PrimaryExpression.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=3f29eec5f60b2ab51ee496a817ab2fa3" alt="PrimaryExpression syntax diagram" width="337" height="332" data-path="sync/grammar/sync-streams/sync-streams-compiler--PrimaryExpression.svg" />

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

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

***

## CaseExpression

```sql theme={null}
CASE WHEN age >= 18 THEN 'adult' ELSE 'minor' END
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--CaseExpression.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=21ac26f78673702021388ba23ded741f" alt="CaseExpression syntax diagram" width="237" height="92" data-path="sync/grammar/sync-streams/sync-streams-compiler--CaseExpression.svg" />

**References:** [SearchedCase](#searchedcase), [SimpleCase](#simplecase)

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

***

## SearchedCase

```sql theme={null}
CASE WHEN x > 0 THEN 'positive' ELSE 'negative' END
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--SearchedCase.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=7b1bd6cbd3c743691af13062de71b2f4" alt="SearchedCase syntax diagram" width="569" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--SearchedCase.svg" />

**References:** [Expression](#expression), [WhenClause](#whenclause)

<Accordion title="Used by">
  [CaseExpression](#caseexpression)
</Accordion>

***

## WhenClause

```sql theme={null}
WHEN age >= 18 THEN 'adult'
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--WhenClause.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=909b1450424b88e126df0f0f7aa4a32c" alt="WhenClause syntax diagram" width="629" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--WhenClause.svg" />

**References:** [CaseCondition](#casecondition), [Expression](#expression), [WhenClause](#whenclause)

<Accordion title="Used by">
  [SearchedCase](#searchedcase), [WhenClause](#whenclause)
</Accordion>

***

## CaseCondition

```sql theme={null}
x > 0 AND y IS NOT NULL
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--CaseCondition.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=563ce98508f1af5ca6b07d548b9d6805" alt="CaseCondition syntax diagram" width="337" height="132" data-path="sync/grammar/sync-streams/sync-streams-compiler--CaseCondition.svg" />

**References:** [Condition](#condition)

<Accordion title="Used by">
  [WhenClause](#whenclause)
</Accordion>

***

## SimpleCase

```sql theme={null}
CASE status WHEN 1 THEN 'active' WHEN 0 THEN 'inactive' END
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--SimpleCase.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=61306c7367821585a6dc45c12503b5c1" alt="SimpleCase syntax diagram" width="729" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--SimpleCase.svg" />

**References:** [Expression](#expression), [WhenValueClause](#whenvalueclause)

<Accordion title="Used by">
  [CaseExpression](#caseexpression)
</Accordion>

***

## WhenValueClause

```sql theme={null}
WHEN 1 THEN 'active'
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--WhenValueClause.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=cc48d3bb38f6f8f0c20ceaf54f274077" alt="WhenValueClause syntax diagram" width="645" height="72" data-path="sync/grammar/sync-streams/sync-streams-compiler--WhenValueClause.svg" />

**References:** [Expression](#expression), [WhenValueClause](#whenvalueclause)

<Accordion title="Used by">
  [SimpleCase](#simplecase), [WhenValueClause](#whenvalueclause)
</Accordion>

***

## CastExpression

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--CastExpression.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=895db30eb214b8337f7f2589f8129c65" alt="CastExpression syntax diagram" width="541" height="182" data-path="sync/grammar/sync-streams/sync-streams-compiler--CastExpression.svg" />

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

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

***

## FunctionCall

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--FunctionCall.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=41e747fec11282db59d3976dde424d3c" alt="FunctionCall syntax diagram" width="449" height="102" data-path="sync/grammar/sync-streams/sync-streams-compiler--FunctionCall.svg" />

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

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

***

## Subquery

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

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--Subquery.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=d816558871e2ef5f244371dfd2fe6fc4" alt="Subquery syntax diagram" width="1017" height="110" data-path="sync/grammar/sync-streams/sync-streams-compiler--Subquery.svg" />

**References:** [FromSource](#fromsource), [JoinClause](#joinclause), [SelectItem](#selectitem), [WhereClause](#whereclause)

<Accordion title="Used by">
  [SubquerySource](#subquerysource), [InSource](#insource)
</Accordion>

***

## CteDefinition

```sql theme={null}
SELECT id, upper(name) AS display_name FROM users WHERE active = true
```

<img src="https://mintcdn.com/powersync-document-fixed-booleans-in-json/FrtTaM2-3T18UZlQ/sync/grammar/sync-streams/sync-streams-compiler--CteDefinition.svg?fit=max&auto=format&n=FrtTaM2-3T18UZlQ&q=85&s=f130db97928ec5785e703ab8005719cb" alt="CteDefinition syntax diagram" width="1253" height="110" data-path="sync/grammar/sync-streams/sync-streams-compiler--CteDefinition.svg" />

**References:** [Expression](#expression), [FromSource](#fromsource), [Identifier](#lexical-rules), [JoinClause](#joinclause), [WhereClause](#whereclause)

***

## 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">
  [SelectItem](#selectitem), [Reference](#reference), [TableSource](#tablesource), [TableValuedCall](#tablevaluedcall), [TableValuedSource](#tablevaluedsource), [SubquerySource](#subquerysource), [InSource](#insource), [CteDefinition](#ctedefinition)
</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>
