How to Query DynamoDB in Descending Order
A DynamoDB Query returns items in ascending sort-key order. Most "give me the
latest" screens want the opposite, and the switch is one boolean on the request:
ScanIndexForward.
Its name reads backwards from what it does, and it's easy to mistake for sorting the results after the fact, which DynamoDB never does.
How do I query DynamoDB in descending order?
Set ScanIndexForward=false on the Query. DynamoDB walks the partition from the
highest sort key down, so a timestamp sort key gives you newest first.
The key condition still decides which items match; with Limit, the direction also
decides which end you get (the newest N instead of the oldest N). A reverse read costs the
same as a forward one.
ScanIndexForward=true(the default): ascending sort-key order.ScanIndexForward=false: descending order, newest first on a time-ordered key.- Add
Limitto get "the N most recent" in one small read. - In PartiQL, write
ORDER BY <sort key> DESCwith the partition key inWHERE.
The problem: "show me the latest first"
Say you run a multiplayer game and store a game's score events under one partition key, sorted by an increasing timestamp:
PK: GAME#42 SK: SCORE#2026-06-27T10:00:00Z points
PK: GAME#42 SK: SCORE#2026-06-27T10:05:00Z points
PK: GAME#42 SK: SCORE#2026-06-27T10:09:00Z points
The dashboard's score feed needs the most recent events, but a plain Query on
GAME#42 returns them oldest first. Reversing them in your app means reading everything,
and it breaks the moment you add Limit.
How ScanIndexForward works
Items in a partition are stored ordered by sort key. A Query walks that order, and
ScanIndexForward picks the direction of the walk:
true(default): start at the lowest sort key and walk up.false: start at the highest sort key and walk down.
It's a property of the read, not the table: same items, same key condition, reversed.
DynamoDB charges by the size of the items it reads, not by direction, per the Query API reference.
Pair it with Limit=10 and you get the ten most recent events. Ten 2 KB items are 20 KB,
five 4 KB blocks, so the read costs 2.5 RCU eventually consistent (5 strongly
consistent), forward or reverse.
Reading a 10,000-event partition (~20 MB) to reverse it in code costs about 2,500 RCU (eventually consistent) for the same widget.
Limit counts items evaluated, not items returned after a filter; the
pagination guide covers what that does to short pages.
What "descending" means for each key type
Descending follows the sort key's type rules, not your idea of "latest":
| Sort key stored as | Descending gives you | Gotcha |
|---|---|---|
ISO-8601 UTC string 2026-06-27T10:09:00Z | Newest timestamp first | Every value needs the same precision and Z offset: …:00.5Z sorts before …:00Z |
Zero-padded number string 00000000001009 | Highest number first | Unpadded numbers sort wrong ("9" > "10"); see zero-padding |
Number type N | Largest numeric value first | Numeric order, not string order |
Status prefix STATUS#open#... | Reverse lexicographic on full SK | Not "most recently opened" unless the suffix encodes time |
If "latest" means something other than the sort key, for example ranking by points
inside the game, you need that metric in a sort key, usually on a
GSI.
A post-query sort in application code only sorts the page you happened to read.
Paging a descending query
Pass each page's LastEvaluatedKey as the next request's ExclusiveStartKey, and send
the same ScanIndexForward=false on every page.
Flip the flag between pages and the cursor walks back the other way, so you get rows you already showed.
For token formats and "load more" APIs, see pagination.
Descending order in PartiQL
PartiQL has no ScanIndexForward. You write the order in the statement:
SELECT * FROM "Scores"
WHERE PK = 'GAME#42'
ORDER BY SK DESCDynamoDB rejects ORDER BY without a WHERE clause.
Pin the partition key with = or IN in that WHERE, or the statement runs as a full
table scan, per the
PartiQL SELECT reference.
Access patterns that use descending order
- Activity feeds:
SKis an ISO timestamp; descending +Limitgives the recent window. - Audit tail: append-only
EVENT#<ts>sort keys; descending shows the newest events without a GSI. - Leaderboards: a raw score can't be the base-table sort key, because two players with
the same score in one game share PK + SK and the second write replaces the first. Put
scoreon a GSI sort key (GSI keys need not be unique), or encode a padded score plus a tiebreaker in a string key.
When the UI also needs oldest first, the same query with ScanIndexForward=true serves
it. You don't need a second copy of the data or a second index.
Build it and try it
Assemble the key condition and its attribute-name/value maps in the expression builder.
For the whole request (index, Limit and ScanIndexForward), the
query builder emits a runnable SDK v3, CLI, boto3 or
other-language program.
To compare what a forward and a reverse read cost, add --return-consumed-capacity TOTAL
in the CLI (or ReturnConsumedCapacity: 'TOTAL' in the SDK): on the same Limit they
match.
In DynoTable, a query tab has an ascending/descending toggle on the sort key, so you can
flip a partition between oldest first and newest first without writing ScanIndexForward
yourself.

Related: sort-key strategies and pagination. Download DynoTable to flip the order on your own tables.


