Bird
Raised Fist0
DynamoDBquery~20 mins

Query by partition key in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Partition Key Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying items by partition key
Given a DynamoDB table named Orders with OrderID as the partition key, what will be the output of this query if the table contains three orders with OrderID values '1001', '1002', and '1003'?
DynamoDB
const params = {
  TableName: 'Orders',
  KeyConditionExpression: 'OrderID = :id',
  ExpressionAttributeValues: {
    ':id': '1002'
  }
};

// Assume docClient.query(params).promise() is called and returns Items
A[{ OrderID: '1001', Customer: 'Bob', Amount: 100 }]
B[{ OrderID: '1002', Customer: 'Alice', Amount: 250 }]
C[]
DSyntaxError: Missing ExpressionAttributeNames
Attempts:
2 left
💡 Hint
Remember, querying by partition key returns all items with that key.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in DynamoDB query
Which option contains a syntax error in the DynamoDB query to get items by partition key 'UserID' with value 'user123'?
DynamoDB
const params = {
  TableName: 'Users',
  KeyConditionExpression: 'UserID = :uid',
  ExpressionAttributeValues: {
    ':uid': 'user123'
  }
};
AAll syntax is correct
BExpressionAttributeValues key should be ':UserID' not ':uid'
CKeyConditionExpression should be 'UserID == :uid'
DMissing comma after TableName property
Attempts:
2 left
💡 Hint
Check the commas between object properties carefully.
optimization
advanced
2:30remaining
Optimizing a query by partition key with a sort key
You have a DynamoDB table Messages with partition key UserID and sort key Timestamp. You want to query all messages for user 'user42' sent after '2023-01-01'. Which query is the most efficient?
AKeyConditionExpression: 'UserID = :uid AND Timestamp > :time', ExpressionAttributeValues: { ':uid': 'user42', ':time': '2023-01-01' }
BKeyConditionExpression: 'UserID = :uid', FilterExpression: 'Timestamp > :time', ExpressionAttributeValues: { ':uid': 'user42', ':time': '2023-01-01' }
CFilterExpression: 'UserID = :uid AND Timestamp > :time', ExpressionAttributeValues: { ':uid': 'user42', ':time': '2023-01-01' }
DScan with FilterExpression: 'UserID = :uid AND Timestamp > :time', ExpressionAttributeValues: { ':uid': 'user42', ':time': '2023-01-01' }
Attempts:
2 left
💡 Hint
Use KeyConditionExpression to filter by partition and sort keys efficiently.
🔧 Debug
advanced
2:00remaining
Debugging a query that returns no results
A developer runs this DynamoDB query to get items with partition key 'ProductID' = 'P123', but gets an empty result. What is the most likely cause?
DynamoDB
const params = {
  TableName: 'Products',
  KeyConditionExpression: 'ProductID = :pid',
  ExpressionAttributeValues: {
    ':pid': 'p123'
  }
};
ATableName is incorrect
BMissing FilterExpression to narrow results
CPartition key value case mismatch: 'P123' vs 'p123'
DKeyConditionExpression syntax error
Attempts:
2 left
💡 Hint
DynamoDB partition keys are case sensitive.
🧠 Conceptual
expert
2:30remaining
Understanding query behavior with composite keys
In a DynamoDB table with partition key CustomerID and sort key OrderDate, which statement is true about querying by partition key only?
AQuerying by partition key is not allowed without a sort key condition
BQuerying by partition key requires specifying a sort key condition
CQuerying by partition key returns only the latest item by OrderDate
DQuerying by partition key returns all items for that CustomerID, regardless of OrderDate
Attempts:
2 left
💡 Hint
Think about how DynamoDB organizes items with composite keys.

Practice

(1/5)
1. What does querying by partition key in DynamoDB do?
easy
A. Updates all items with the same partition key value
B. Deletes all items with the same partition key value
C. Fetches all items with the same partition key value
D. Creates a new item with the partition key value

Solution

  1. Step 1: Understand partition key role

    The partition key uniquely identifies the partition where items are stored.
  2. Step 2: Query by partition key fetches items

    Querying by partition key returns all items that share that key value.
  3. Final Answer:

    Fetches all items with the same partition key value -> Option C
  4. Quick Check:

    Query by partition key = fetch items [OK]
Hint: Partition key query fetches matching items only [OK]
Common Mistakes:
  • Thinking it deletes or updates items
  • Confusing partition key with sort key
  • Assuming it creates new items
2. Which of the following is the correct syntax to query items by partition key 'UserID' with value '123' in DynamoDB?
easy
A. Table.query(KeyConditionExpression: 'UserID = 123')
B. Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'})
C. Table.query(FilterExpression: 'UserID = 123')
D. Table.query(KeyConditionExpression: 'UserID == 123')

Solution

  1. Step 1: Identify correct query syntax

    DynamoDB requires KeyConditionExpression with placeholders and ExpressionAttributeValues.
  2. Step 2: Check each option

    Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'}) uses correct syntax with placeholder ':val' and value mapping. Others misuse operators or omit placeholders.
  3. Final Answer:

    Table.query(KeyConditionExpression: 'UserID = :val', ExpressionAttributeValues: {':val': '123'}) -> Option B
  4. Quick Check:

    Use placeholders and ExpressionAttributeValues [OK]
Hint: Use placeholders (:val) and ExpressionAttributeValues [OK]
Common Mistakes:
  • Using '==' instead of '=' in KeyConditionExpression
  • Not using ExpressionAttributeValues for values
  • Using FilterExpression instead of KeyConditionExpression
3. Given a DynamoDB table with items: {UserID: '123', Name: 'Alice'}, {UserID: '123', Name: 'Bob'}, {UserID: '456', Name: 'Carol'}, what will the query by partition key 'UserID' = '123' return?
medium
A. [{UserID: '123', Name: 'Alice'}, {UserID: '123', Name: 'Bob'}]
B. [{UserID: '456', Name: 'Carol'}]
C. [{UserID: '123', Name: 'Alice'}]
D. [] (empty list)

Solution

  1. Step 1: Identify items with partition key '123'

    Items with UserID '123' are Alice and Bob.
  2. Step 2: Query returns all matching items

    Query returns both Alice and Bob items as they share the partition key.
  3. Final Answer:

    [{UserID: '123', Name: 'Alice'}, {UserID: '123', Name: 'Bob'}] -> Option A
  4. Quick Check:

    Query by '123' returns Alice and Bob [OK]
Hint: Query returns all items with matching partition key [OK]
Common Mistakes:
  • Expecting only one item returned
  • Confusing partition key with sort key filtering
  • Assuming query returns items with different partition keys
4. You wrote this query to get items with partition key 'OrderID' = '789': Table.query(KeyConditionExpression: 'OrderID = 789'). It returns an error. What is the problem?
medium
A. Partition key name is case-sensitive and should be 'orderid'
B. Using wrong operator '==' instead of '='
C. Query method does not support KeyConditionExpression
D. Missing ExpressionAttributeValues for the value '789'

Solution

  1. Step 1: Check query syntax requirements

    DynamoDB requires placeholders and ExpressionAttributeValues for values in KeyConditionExpression.
  2. Step 2: Identify missing part

    The query uses 'OrderID = 789' directly without placeholder or ExpressionAttributeValues, causing error.
  3. Final Answer:

    Missing ExpressionAttributeValues for the value '789' -> Option D
  4. Quick Check:

    Use placeholders and ExpressionAttributeValues [OK]
Hint: Always use placeholders and ExpressionAttributeValues [OK]
Common Mistakes:
  • Using raw values instead of placeholders
  • Assuming case-insensitivity for partition key
  • Using '==' operator instead of '='
5. You want to query a DynamoDB table for all items with partition key 'Category' = 'Books' and sort key 'Price' less than 20. Which approach correctly uses query by partition key with a condition on sort key?
hard
A. Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
B. Use FilterExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
C. Use KeyConditionExpression: 'Category = :cat' and FilterExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}
D. Use FilterExpression: 'Category = :cat' and KeyConditionExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20}

Solution

  1. Step 1: Understand query conditions

    KeyConditionExpression must include partition key equality and supports sort key conditions like <. FilterExpression filters results after the query.
  2. Step 2: Analyze options

    Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} correctly uses KeyConditionExpression: 'Category = :cat AND Price < :price', which efficiently queries the partition with a sort key range condition. Use KeyConditionExpression: 'Category = :cat' and FilterExpression: 'Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} uses FilterExpression for Price (valid but less efficient). B lacks KeyConditionExpression. D omits partition key from KeyConditionExpression.
  3. Final Answer:

    Use KeyConditionExpression: 'Category = :cat AND Price < :price' with ExpressionAttributeValues {':cat': 'Books', ':price': 20} -> Option A
  4. Quick Check:

    KeyConditionExpression: partition = AND sort < [OK]
Hint: KeyConditionExpression: partition = AND sort condition [OK]
Common Mistakes:
  • Using FilterExpression for partition key
  • Using FilterExpression instead of KeyConditionExpression for sort key range
  • Mixing KeyConditionExpression and FilterExpression incorrectly