The sort key allows multiple items to share the same partition key but be uniquely identified and sorted within that partition. It helps organize data and enables range queries.
Query( TableName='UserActivity', KeyConditionExpression='UserID = :uid AND Timestamp > :ts', ExpressionAttributeValues={':uid': '123', ':ts': 1000} )
The query filters items by partition key 'UserID' equal to '123' and sort key 'Timestamp' greater than 1000, returning only matching items.
CreateTable( TableName='Orders', KeySchema=[ {'AttributeName': 'CustomerID', 'KeyType': 'HASH'}, {'AttributeName': 'OrderDate', 'KeyType': 'RANGE'} ], AttributeDefinitions=[ {'AttributeName': 'CustomerID', 'AttributeType': 'S'}, {'AttributeName': 'OrderDate', 'AttributeType': 'S'} ], ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5} )
The partition key must be defined as 'HASH' and the sort key as 'RANGE' in KeySchema. AttributeDefinitions must match the attribute names and types.
Using Query with partition key and sort key range condition efficiently fetches only relevant items without scanning the whole table.
The query references ':date' in the KeyConditionExpression but does not provide a value for it in ExpressionAttributeValues, causing a validation error.
