0
0
DynamoDBquery~20 mins

Query result ordering (ascending, descending) in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DynamoDB Query Ordering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the order of results returned by this DynamoDB query?

Consider a DynamoDB table with a partition key UserID and a sort key Timestamp. You run this query to get all items for UserID = 'user123':

QueryRequest request = new QueryRequest()
    .withTableName("UserActivity")
    .withKeyConditionExpression("UserID = :uid")
    .withExpressionAttributeValues(Map.of(":uid", new AttributeValue().withS("user123")))
    .withScanIndexForward(true);

What order will the items be returned in?

AItems are returned in ascending order by the sort key (Timestamp).
BItems are returned in descending order by the sort key (Timestamp).
CItems are returned in random order.
DItems are returned in ascending order by the partition key (UserID).
Attempts:
2 left
💡 Hint

Look at the withScanIndexForward(true) parameter.

query_result
intermediate
2:00remaining
How to get descending order results from a DynamoDB query?

You want to get the latest activity first for a user in a DynamoDB table with UserID as partition key and Timestamp as sort key. Which parameter should you set in the query?

ASet <code>ScanIndexForward</code> to <code>true</code>.
BSet <code>ReturnConsumedCapacity</code> to <code>INDEXES</code>.
CSet <code>ConsistentRead</code> to <code>true</code>.
DSet <code>ScanIndexForward</code> to <code>false</code>.
Attempts:
2 left
💡 Hint

Think about reversing the order of the sort key.

🧠 Conceptual
advanced
2:00remaining
Why can't you order DynamoDB query results by attributes other than the sort key?

DynamoDB queries allow ordering results by the sort key only. Why is it not possible to order by other attributes?

ABecause DynamoDB stores data sorted only by partition key and sort key, so ordering by other attributes would require scanning all data.
BBecause DynamoDB automatically orders by all attributes internally, so specifying others is redundant.
CBecause DynamoDB does not support any ordering of query results.
DBecause ordering by other attributes is only possible with a Scan operation.
Attempts:
2 left
💡 Hint

Think about how DynamoDB stores and indexes data physically.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this DynamoDB query for descending order

Which option contains a syntax error in setting the query to return results in descending order?

QueryRequest request = new QueryRequest()
    .withTableName("UserActivity")
    .withKeyConditionExpression("UserID = :uid")
    .withExpressionAttributeValues(Map.of(":uid", new AttributeValue().withS("user123")))
    .withScanIndexForward(???);
AwithScanIndexForward(Boolean.FALSE)
BwithScanIndexForward(false)
CwithScanIndexForward("false")
DwithScanIndexForward(0)
Attempts:
2 left
💡 Hint

Check the expected data type for withScanIndexForward.

optimization
expert
3:00remaining
How to efficiently get the last 5 items in descending order from a DynamoDB table?

You want to get the 5 most recent activities for a user from a DynamoDB table with UserID as partition key and Timestamp as sort key. Which approach is best?

AQuery with <code>ScanIndexForward(true)</code> and <code>Limit(5)</code> to get first 5 items in ascending order.
BQuery with <code>ScanIndexForward(false)</code> and <code>Limit(5)</code> to get last 5 items in descending order.
CUse a Global Secondary Index on Timestamp and scan it to get last 5 items.
DScan the whole table and sort results in application code to get last 5 items.
Attempts:
2 left
💡 Hint

Think about how to limit data read and get results in desired order.