Basic Queries
The simplest stream query syncs all rows from a table:WHERE clause to filter:
Filtering by User
Most apps need to sync different data to different users. Useauth.user_id() to filter by the authenticated user:
sub claim in their JWT token. See Auth Parameters.
On-Demand Data with Subscription Parameters
For data that should only sync when the user navigates to a specific screen, use subscription parameters. The client passes these when subscribing to a stream:Selecting Columns
Select specific columns instead of* to reduce data transfer:
Renaming Columns
UseAS to rename columns in the synced data:
Type Transformations
PowerSync syncs data to SQLite on the client. You may need to transform types for compatibility:Using Subqueries
Subqueries let you filter based on related tables. UseIN (SELECT ...) to sync data where a foreign key matches rows in another table:
Nested Subqueries
Subqueries can be nested to traverse multiple levels of relationships. This is useful for normalized database schemas:Combining Parameters with Subqueries
A common pattern is using subscription parameters to select what data to sync, while using subqueries for authorization:Using Joins
For complex queries that traverse multiple tables, join syntax is often easier to read than nested subqueries. You can useJOIN or INNER JOIN (they’re equivalent). For the exact supported JOIN syntax and restrictions, see Supported SQL — JOIN syntax.
Consider this query:
Multiple Joins
You can chain multiple joins to traverse complex relationships. This example joins four tables to sync checkpoints for assignments the user has access to.Self-Joins
You can join the same table multiple times; aliases are required to distinguish each occurrence (e.g.gm1 and gm2 for the two group_memberships joins). This is useful for finding related records through a shared relationship — for example, finding all users who share a group with the current user:
Join Limitations
When writing stream queries with JOINs, keep in mind: use onlyJOIN or INNER JOIN; select columns from a single table (e.g. comments.*); and use simple equality conditions (table1.column = table2.column). For the full list of supported JOIN syntax and invalid examples, see Supported SQL — JOIN syntax.
Multiple Queries per Stream
You can group multiple queries into a single stream usingqueries instead of query. This is useful when several tables share the same access pattern:
When to Use Multiple Queries
Usequeries when:
- Multiple tables have the same filtering logic (e.g., all filtered by
user_id) - You want to optimize sync by using one stream so the client subscribes once and PowerSync merges the data from all queries, and to reduce bucket count ()
- Related data should sync together