How to query DynamoDB with same partition key and different sort key?
Short answer
B Add a local secondary index (LSI) during table creation. Query the LSI by using strongly consistent reads.
Question
A developer is writing an application, which stores data in an Amazon DynamoDB table. The developer wants to query the DynamoDB table by using the partition key and a different sort key value. The developer needs the latest data with all recent write operations. How should the developer write the DynamoDB query?
Key Takeaways
This question tests the distinction between LSI and GSI partition key requirements, as well as the difference between eventually and strongly consistent reads in DynamoDB.
To query a DynamoDB table using the same partition key but a different sort key, a Local Secondary Index (LSI) is required. For the latest data with all recent writes, strongly consistent reads must be used.
Common mistake: Many candidates choose D (GSI with strongly consistent reads) because they confuse GSI and LSI, or forget that GSIs do not support strongly consistent reads.
Expert Analysis
Understanding the Requirements
The question has two distinct requirements:
1. Query using the same partition key but a different sort key value. 2. Retrieve the latest data with all recent write operations.
Why a Local Secondary Index (LSI) is Required
A Local Secondary Index (LSI) in DynamoDB allows you to create an alternate sort key while keeping the same partition key as the base table. This is exactly what the developer needs. An LSI must be created at the time of table creation and cannot be added later.
In contrast, a Global Secondary Index (GSI) allows both a different partition key and a different sort key. Since the developer wants to keep the same partition key, an LSI is the correct choice.
Why Strongly Consistent Reads are Required
DynamoDB offers two types of reads:
- Eventually Consistent Reads: May not reflect the most recent write operations. The data returned might be stale.
- Strongly Consistent Reads: Guarantee that the most recent data is returned, reflecting all successful write operations.
Why the Other Options are Incorrect
- Option A: Uses an LSI (correct) but with eventually consistent reads (incorrect for the "latest data" requirement).
- Option C: Uses a GSI (incorrect partition key requirement) with eventually consistent reads (also incorrect for consistency).
- Option D: Uses a GSI (incorrect) with strongly consistent reads. Additionally, GSIs only support eventually consistent reads, making this option doubly wrong.
Community Consensus
The community overwhelmingly agrees with Option B (82 votes), with only a small fraction (18 votes) choosing D. Experienced candidates correctly identify that LSIs share the same partition key and that strongly consistent reads are necessary for the latest data.
Official Reference
Array
Exam Strategy
When a DynamoDB question mentions 'same partition key, different sort key,' immediately think LSI. When it mentions 'latest data' or 'all recent writes,' think strongly consistent reads. Eliminate options that mix these concepts incorrectly.
Community Votes
82% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 5 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded.
From public community threads 5
-
Anandesh 👍 7 Answered BGSI - diff part key and sort key | LSI - same part key and diff sort key Strongly consistent - most recent data
-
tomchandler077 👍 3The correct answer is: B. Local Secondary Index (LSI): An LSI allows you to create an index with a different sort key for the same partition key as the base table. This lets you query the table using the same partition key but with a different sort key. LSIs are created at the same time as the table and cannot be added to an existing table. Strongly consistent reads ensure that you always receive the most up-to-date data after all previous write operations are acknowledged. This is important when the developer needs to ensure they are getting the latest data, as stated in the question.
-
[Removed] 👍 2 Answered BConsidering developer wants to query by using the same partition key and a different sort key value, so B it's the correct answer
-
cachac 👍 1 Answered DStrongly Consistent Reads
-
Alagong 👍 1 Answered DANSWER IS D
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →