Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a composite primary key in DynamoDB?
A composite primary key in DynamoDB is made of two parts: a partition key and a sort key. Together, they uniquely identify each item in a table.
Click to reveal answer
beginner
Why use a composite primary key instead of a simple primary key?
Using a composite primary key lets you store multiple related items with the same partition key but different sort keys. This helps organize data and query it efficiently.
Click to reveal answer
beginner
In DynamoDB, what does the partition key do in a composite primary key?
The partition key decides which partition (storage unit) the data goes to. It groups items together for fast access.
Click to reveal answer
beginner
What role does the sort key play in a composite primary key?
The sort key orders items within the same partition key. It lets you store and query multiple items with the same partition key but different sort keys.
Click to reveal answer
intermediate
How does a composite primary key help with querying data in DynamoDB?
It allows you to find all items with the same partition key and filter or sort them by the sort key. This makes queries more flexible and efficient.
Click to reveal answer
What two parts make up a composite primary key in DynamoDB?
AIndex key and sort key
BPrimary key and foreign key
CHash key and range key
DPartition key and sort key
✗ Incorrect
A composite primary key consists of a partition key and a sort key. (Note: Hash key and range key are older terms for partition and sort keys.)
What does the partition key determine in DynamoDB?
AWhich partition stores the item
BThe total number of items in the table
CThe order of items within a partition
DThe size of the item
✗ Incorrect
The partition key decides which partition stores the item, helping distribute data.
Can two items in DynamoDB have the same partition key but different sort keys?
AYes, if they have different sort keys
BNo, partition keys must be unique
CYes, but only if they are in different tables
DNo, both keys must be unique together
✗ Incorrect
Items can share the same partition key if their sort keys differ, allowing multiple related items.
What advantage does a composite primary key provide when querying data?
AIt allows filtering by partition key only
BIt speeds up writes but not reads
CIt enables sorting and filtering within a partition
DIt removes the need for indexes
✗ Incorrect
The sort key lets you sort and filter items within the same partition key.
Which of these is NOT true about composite primary keys in DynamoDB?
AThey uniquely identify each item in the table
BThey allow multiple items with the same partition and sort key
CThey consist of a partition key and a sort key
DThey help organize data for efficient queries
✗ Incorrect
Items cannot have the same partition key and sort key combination; that would not be unique.
Explain what a composite primary key is in DynamoDB and why it is useful.
Think about how two keys work together to organize and find data.
You got /4 concepts.
Describe how the partition key and sort key work together in a composite primary key.
Consider their roles in storing and retrieving data.
You got /4 concepts.
Practice
(1/5)
1.
What is a composite primary key in DynamoDB?
easy
A. A key that allows duplicate items
B. A key made of only one attribute
C. A key made of two attributes: PartitionKey and SortKey
A. PartitionKey UserId is missing in KeyConditionExpression
B. SortKey OrderId cannot be used in KeyConditionExpression
C. ExpressionAttributeValues syntax is incorrect
D. TableName is invalid
Solution
Step 1: Recall query requirements for composite keys
Queries must specify PartitionKey equality in KeyConditionExpression.
Step 2: Check the given query
Only SortKey OrderId is used; PartitionKey UserId is missing, causing error.
Final Answer:
PartitionKey UserId is missing in KeyConditionExpression -> Option A
Quick Check:
PartitionKey must be in query condition [OK]
Hint: Always include PartitionKey equality in query [OK]
Common Mistakes:
Using only SortKey in query condition
Assuming SortKey alone can identify items
Ignoring required PartitionKey in queries
5.
You want to store blog posts in DynamoDB. Each post has a AuthorId and a PostDate. You want to quickly find all posts by an author sorted by date. Which composite primary key design is best?
hard
A. PartitionKey: PostDate, SortKey: AuthorId
B. PartitionKey: AuthorId, SortKey: PostDate
C. PartitionKey: AuthorId only, no SortKey
D. PartitionKey: PostDate only, no SortKey
Solution
Step 1: Identify query pattern
You want to find all posts by an author, sorted by date.
Step 2: Choose PartitionKey and SortKey accordingly
PartitionKey should be AuthorId to group posts by author; SortKey should be PostDate to sort posts by date.
Final Answer:
PartitionKey: AuthorId, SortKey: PostDate -> Option B
Quick Check:
Group by author, sort by date = AuthorId + PostDate [OK]
Hint: PartitionKey groups, SortKey sorts related items [OK]
Common Mistakes:
Swapping PartitionKey and SortKey roles
Using only one key losing sorting ability
Choosing SortKey that doesn't support query pattern