What is the difference between a partition key and a sort key in DynamoDB?
The difference between a DynamoDB partition key and sort key, how each works, and how they combine to power fast range queries and one-to-many models.
Expected Interview Answer
The partition key decides which physical partition an item lives on by hashing, while the sort key orders items within that partition and lets you query a range of related items under the same partition key.
A partition key alone gives one item per key, but adding a sort key means many items can share a partition key and be stored sorted by the sort key value. This unlocks rich queries: fetch all items for a partition key, narrow to a sort-key range, or use begins_with and comparison operators. The partition key must always be supplied exactly for a query, whereas the sort key is optional and supports conditions, enabling one-to-many access patterns like a user's orders sorted by date.
- Partition key spreads data; sort key organizes it within a partition
- Enables efficient range queries under a single partition key
- Supports one-to-many relationships in a single table
- Allows begins_with, between, and comparison filters on the sort key
- Keeps related items physically co-located for fast retrieval
AI Mentor Explanation
The partition key is like the team a player belongs to — it decides which dressing room they sit in. The sort key is the batting order inside that room, listing openers first down to the tail. You find the team, then read players in their set order, just as DynamoDB locates a partition then reads items by sort key.
Step-by-Step Explanation
Step 1
Supply the partition key
A query always requires the exact partition key value to locate the owning partition.
Step 2
Locate the item collection
All items sharing that partition key form an item collection stored together, sorted by sort key.
Step 3
Optionally add a sort-key condition
Narrow results with equals, begins_with, between, or comparison operators on the sort key.
Step 4
Read in order
DynamoDB returns matching items sorted by the sort key, ascending by default or descending on request.
Step 5
Retrieve one or many
Without a sort-key condition you get the whole collection; with one you get a precise range.
What Interviewer Expects
- Explains the partition key hashes to a partition, sort key orders within it
- Knows queries must supply the exact partition key but the sort key is optional
- Can describe range operators like begins_with and between on the sort key
- Understands one-to-many modeling via shared partition keys
- Distinguishes Query (uses keys) from Scan (reads everything)
Common Mistakes
- Believing the sort key alone can be queried without the partition key
- Thinking every item needs a unique partition key even with a sort key
- Confusing sort-key range queries with a full table scan
- Assuming the sort key affects which partition an item lands on
- Using Scan when a partition key plus sort-key condition would suffice
Best Answer (HR Friendly)
“The partition key decides which bucket a record goes into, and the sort key arranges records within that bucket in order. Together they let you quickly grab, say, one customer's orders and read them by date without scanning the whole table.”
Code Example
import boto3
from boto3.dynamodb.conditions import Key
client = boto3.client('dynamodb')
client.create_table(
TableName='Orders',
KeySchema=[
{'AttributeName': 'customerId', 'KeyType': 'HASH'}, # partition key
{'AttributeName': 'orderDate', 'KeyType': 'RANGE'} # sort key
],
AttributeDefinitions=[
{'AttributeName': 'customerId', 'AttributeType': 'S'},
{'AttributeName': 'orderDate', 'AttributeType': 'S'}
],
BillingMode='PAY_PER_REQUEST'
)
table = boto3.resource('dynamodb').Table('Orders')
# All 2026 orders for one customer, sorted by date
resp = table.query(
KeyConditionExpression=Key('customerId').eq('cust-17')
& Key('orderDate').begins_with('2026')
)Follow-up Questions
- What is an item collection in DynamoDB?
- Which sort-key operators can a Query use?
- Can you query by sort key without the partition key?
- How do partition and sort keys enable one-to-many relationships?
- What is the difference between Query and Scan?
MCQ Practice
1. What does the sort key control within a partition?
The sort key orders items that share the same partition key, enabling range queries within that item collection.
2. To run a Query in DynamoDB you must always supply the:
Query requires an exact partition key value; the sort-key condition is optional and used to narrow the range.
3. Which operator is valid on a sort key in a KeyConditionExpression?
begins_with, equals, less/greater comparisons, and between are valid sort-key conditions; contains is not.
Flash Cards
Partition key vs sort key? — Partition key hashes to a partition; sort key orders items within that partition and enables range queries.
Is the sort key required in a Query? — No — the partition key is mandatory, the sort-key condition is optional.
What is an item collection? — All items that share the same partition key, stored together and sorted by sort key.
Which sort-key operators exist? — equals, less-than, greater-than, between, and begins_with.