Tablux

Tablux › Guides › DynamoDB PartiQL

Guide

PartiQL for DynamoDB: how it works, with examples

PartiQL is a SQL-compatible query language that AWS open-sourced in 2019. DynamoDB accepts it through the ExecuteStatement API, so you can read and write items with SELECT, INSERT, UPDATE and DELETE. It is still DynamoDB underneath: a SELECT is a cheap Query only when the WHERE clause pins the partition key with = or IN. Anything else scans the whole table.

What PartiQL is

AWS announced PartiQL in August 2019 and published the specification, a tutorial and a reference implementation under the Apache 2.0 licence at partiql.org. It extends SQL to nested and schemaless data: dotted paths into maps, brackets into lists, and a distinction between a value that is NULL and an attribute that is MISSING.

Inside AWS it is the query language of DynamoDB and of Amazon Redshift's SUPER type for semi-structured data. Amazon QLDB also used it, but QLDB reached end of support on 31 July 2025. DynamoDB implements a subset of PartiQL: no Ion literals, no joins, no aggregates. Treat it as a SQL-shaped way to call DynamoDB's own operations, not as a SQL database.

The example table

Every example below uses one table, trailhead, a single-table design for an outdoor shop. Customers, orders, order lines, refunds and products share a partition key PK and a sort key SK, and one global secondary index, GSI1, serves two more access patterns.

PK               SK                                 item
CUSTOMER#1042    CUSTOMER#1042                      the customer
CUSTOMER#1042    ORDER#2026-10-14T09:31:00#58213    one of their orders
ORDER#58213      LINE#17                            an order line (product 17)
ORDER#58213      REFUND#912                         a refund on that order
PRODUCT#17       PRODUCT#17                         a product

GSI1PK           GSI1SK                             on
STATUS#pending   2026-10-14T09:31:00                orders, by status and date
CATEGORY#Tents   PRODUCT#Ridgeline 2P tent          products, by category and name

Two quoting rules matter. Single quotes make a string ('ORDER#58213'; write '' for a quote inside it). Double quotes make an identifier ("PK", "trailhead"), which you need for table names with dashes and for index names, and which keeps attribute names clear of reserved words. The single-table design guide explains why the keys look like this.

How DynamoDB runs a statement

Three API operations take PartiQL:

  • ExecuteStatement: one statement, a read or a single-item write.
  • BatchExecuteStatement: up to 25 statements, all reads or all writes. Batched reads use BatchGetItem, so each SELECT must name a full primary key.
  • ExecuteTransaction: up to 100 statements that succeed or fail together, again all reads or all writes; EXISTS(…) adds a condition check.

From the AWS CLI:

aws dynamodb execute-statement \
  --statement "SELECT * FROM \"trailhead\" WHERE \"PK\" = 'CUSTOMER#1042'"

The rule that decides speed and cost comes from the DynamoDB developer guide: a SELECT results in a full table scan unless the WHERE clause has an equality or IN condition on the partition key. DynamoDB will not rewrite a range into lookups for you.

WHERE clauseRuns as
"PK" = 'CUSTOMER#1042'Query
"PK" = 'CUSTOMER#1042' AND begins_with("SK", 'ORDER#')Query
"PK" IN ['CUSTOMER#1042', 'CUSTOMER#1043']Query, one per value
"PK" = 'CUSTOMER#1042' OR "PK" = 'CUSTOMER#1043'Query, one per value
"PK" > 'CUSTOMER#'Scan
"PK" = 'CUSTOMER#1042' OR "status" = 'pending'Scan
"status" = 'pending'Scan
(no WHERE)Scan

An IN list takes at most 50 partition-key values and returns pages of up to 10 items.

SELECT examples

One item by its full primary key:

SELECT * FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042' AND "SK" = 'CUSTOMER#1042'

The whole item collection: the customer and all their orders, in sort-key order, in one Query:

SELECT * FROM "trailhead" WHERE "PK" = 'CUSTOMER#1042'

Only the orders, newest first. Because the sort key starts with an ISO timestamp, sorting by SK sorts by date:

SELECT "orderId", "status", "total", "orderedAt" FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042' AND begins_with("SK", 'ORDER#')
ORDER BY "SK" DESC

Orders in one month, with BETWEEN on the sort key (both ends inclusive):

SELECT * FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042'
  AND "SK" BETWEEN 'ORDER#2026-10' AND 'ORDER#2026-11'

Refunds on one order:

SELECT "productName", "amount", "reason" FROM "trailhead"
WHERE "PK" = 'ORDER#58213' AND begins_with("SK", 'REFUND#')

Query a global secondary index by putting its name after the table, both in double quotes. Pending orders placed before October, oldest first:

SELECT "PK", "orderId", "total", "orderedAt" FROM "trailhead"."GSI1"
WHERE "GSI1PK" = 'STATUS#pending' AND "GSI1SK" < '2026-10-01'

Products in a category, by name:

SELECT "name", "price", "stock" FROM "trailhead"."GSI1"
WHERE "GSI1PK" = 'CATEGORY#Tents'

Index reads are eventually consistent, and they only return attributes projected into the index. GSI1 projects ALL; with KEYS_ONLY you would get the keys back and nothing else.

The functions DynamoDB supports work in WHERE: begins_with, contains (substring, or set member), attribute_type, size, and IS MISSING / IS NOT MISSING. Combined with a partition-key equality they filter one item collection:

SELECT * FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042' AND begins_with("SK", 'ORDER#')
  AND "shipCountry" IS MISSING

Without one, they filter a full scan. This finds every zipper refund, and reads every item in the table to do it:

SELECT "PK", "productName", "amount", "refundedAt" FROM "trailhead"
WHERE begins_with("SK", 'REFUND#') AND contains("reason", 'Zipper')
SELECT "PK", "SK", "total" FROM "trailhead"
WHERE begins_with("SK", 'ORDER#') AND NOT attribute_type("total", 'N')

Nested attributes use dots for maps and brackets for lists. If orders carried a shippingAddress map and a tags list:

SELECT shippingAddress.city, tags[0] FROM "trailhead"
WHERE "PK" = 'CUSTOMER#1042' AND begins_with("SK", 'ORDER#')

INSERT, UPDATE and DELETE

Every write touches exactly one item, and the WHERE clause must resolve to one full primary key (PK and SK here). INSERT never overwrites: if the key exists, DynamoDB returns DuplicateItemException.

INSERT INTO "trailhead" VALUE {
  'PK': 'CUSTOMER#1042', 'SK': 'ORDER#2026-11-25T10:05:00#58990',
  'orderId': 58990, 'status': 'pending', 'total': 129.5,
  'orderedAt': '2026-11-25T10:05:00',
  'GSI1PK': 'STATUS#pending', 'GSI1SK': '2026-11-25T10:05:00'
}

UPDATE takes one or more SET and REMOVE clauses. Extra conditions in the WHERE clause act as a condition check: if the item exists but the condition is false, the update fails with ConditionalCheckFailedException. RETURNING ALL NEW * returns the item after the change.

UPDATE "trailhead"
SET "status" = 'shipped'
SET "GSI1PK" = 'STATUS#shipped'
WHERE "PK" = 'CUSTOMER#1042' AND "SK" = 'ORDER#2026-11-25T10:05:00#58990'
  AND "status" = 'pending'
RETURNING ALL NEW *

Arithmetic, lists and sets:

UPDATE "trailhead" SET "stock" = "stock" - 1
WHERE "PK" = 'PRODUCT#17' AND "SK" = 'PRODUCT#17'
UPDATE "trailhead"
SET tags = list_append(tags, ['gift'])
SET labels = set_add(labels, <<'priority'>>)
WHERE "PK" = 'CUSTOMER#1042' AND "SK" = 'ORDER#2026-11-25T10:05:00#58990'

Removing the index keys drops an item out of the index, which is how a sparse index stays small:

UPDATE "trailhead" REMOVE "GSI1PK" REMOVE "GSI1SK"
WHERE "PK" = 'CUSTOMER#1042' AND "SK" = 'ORDER#2026-11-25T10:05:00#58990'

DELETE also needs the full key. Deleting a key that does not exist succeeds with nothing deleted.

DELETE FROM "trailhead"
WHERE "PK" = 'ORDER#58213' AND "SK" = 'REFUND#912'
RETURNING ALL OLD *

There is no DELETE … WHERE "PK" = 'ORDER#58213' for a whole item collection. Read the keys with a SELECT, then send the deletes in batches of 25 or in a transaction. A transaction file for aws dynamodb execute-transaction --transact-statements file://refund.json that records a refund and flags the order, but only if the order is still marked delivered:

[
  {"Statement": "INSERT INTO \"trailhead\" VALUE {'PK': 'ORDER#58213', 'SK': 'REFUND#913', 'amount': 249, 'reason': 'Zipper failed'}"},
  {"Statement": "UPDATE \"trailhead\" SET \"hasRefund\" = TRUE WHERE \"PK\" = 'CUSTOMER#1042' AND \"SK\" = 'ORDER#2026-10-14T09:31:00#58213' AND \"status\" = 'delivered'"}
]

Use ? placeholders and the Parameters list in application code instead of building strings.

No LIMIT: paging and the 1 MB page

DynamoDB's PartiQL has no LIMIT or OFFSET clause. Paging lives in the API:

  • Limit is the maximum number of items to evaluate, not the number that match. Limit: 25 on a filtered scan can return 3 items, or none, plus a token to continue.
  • A read also stops after 1 MB of data has been processed, before the WHERE filter applies.
  • When the response has a NextToken, send the same statement again with that token. Keep going until no token comes back.
aws dynamodb execute-statement --limit 25 \
  --statement "SELECT * FROM \"trailhead\".\"GSI1\" WHERE \"GSI1PK\" = 'STATUS#pending'"

aws dynamodb execute-statement --limit 25 --next-token "<NextToken from the last response>" \
  --statement "SELECT * FROM \"trailhead\".\"GSI1\" WHERE \"GSI1PK\" = 'STATUS#pending'"

An empty page with a NextToken is normal on a filtered scan. It does not mean you are done.

What PartiQL on DynamoDB does not do

  • No joins, subqueries (outside EXISTS in transactions), GROUP BY, COUNT, SUM or other aggregates. Count with the Scan API's Select: COUNT, or read the approximate ItemCount from DescribeTable.
  • ORDER BY only works when the WHERE clause pins the partition key, and only on key attributes. On a scan DynamoDB returns an error rather than sorting.
  • No multi-item UPDATE or DELETE, and no upsert: INSERT fails on an existing key, UPDATE needs the key.
  • No LIMIT; see paging above. A statement can be at most 8,192 characters.

Cost and capacity

PartiQL costs what the underlying operation costs. AWS states that PartiQL operations have the same availability, latency and performance as the other data plane operations. A Query reads only the matching item collection. A scan reads every item: one read capacity unit covers a strongly consistent read of up to 4 KB, or two eventually consistent ones, and the WHERE filter, like a projection, does not reduce the units consumed. Add --return-consumed-capacity TOTAL to see the bill for a statement.

To stop accidental full scans, deny them in IAM. The dynamodb:FullTableScan condition key blocks scanning SELECTs while still allowing key lookups:

{
  "Effect": "Deny",
  "Action": "dynamodb:PartiQLSelect",
  "Resource": "arn:aws:dynamodb:eu-west-2:123456789012:table/trailhead",
  "Condition": {"Bool": {"dynamodb:FullTableScan": ["true"]}}
}

Pair it with an Allow for dynamodb:PartiQLSelect on the table and its indexes. Writes need PartiQLInsert, PartiQLUpdate and PartiQLDelete, so a read-only IAM user simply never gets those.

Running PartiQL from a client

The DynamoDB console's PartiQL editor, NoSQL Workbench and the AWS CLI all run these statements. Tablux runs PartiQL against DynamoDB from a Mac, iPhone or iPad, one statement at a time. It also accepts a trailing LIMIT n, which DynamoDB's PartiQL does not support: Tablux strips it before sending and keeps following NextToken until it has n matching items, so a filtered scan doesn't stop at an empty first page. On a connection marked production, writes ask first.

Questions

Is PartiQL the same as SQL?

It is SQL-compatible: SELECT, INSERT, UPDATE and DELETE read like SQL. DynamoDB supports a subset, with no joins, aggregates or LIMIT, and each statement still runs as a DynamoDB Query, Scan or single-item write.

When does a PartiQL SELECT scan the whole table?

Whenever the WHERE clause lacks an equality or IN condition on the partition key. A range on the partition key, an OR with a non-key attribute, or no WHERE at all all become a full scan.

How do I query a global secondary index with PartiQL?

Put the index after the table name, both in double quotes: SELECT * FROM "trailhead"."GSI1" WHERE "GSI1PK" = 'STATUS#pending'. Index reads are eventually consistent.

Why did my SELECT return fewer items than the Limit I set?

Limit counts items evaluated, not items matched, and a page also stops at 1 MB. Keep sending the statement with the NextToken from each response until none comes back.

Is PartiQL more expensive than the Query API?

No. You pay for the operation it becomes. A partition-key lookup costs the same as a Query; a statement that scans costs the same as a Scan of the table.

Can I count items with PartiQL in DynamoDB?

No, COUNT is not supported. Use a Scan with Select set to COUNT, or DescribeTable's ItemCount, which DynamoDB updates periodically rather than in real time.

Updated 2026-11-25