Bird
Raised Fist0
DynamoDBquery~20 mins

Key condition expressions 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
🎖️
Key Condition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query with simple partition key condition
Given a DynamoDB table named Orders with OrderID as the partition key, what will be the output of this key condition expression?

OrderID = :orderId

Assuming :orderId is set to 12345 and the table contains one item with OrderID 12345 and CustomerName 'Alice'.
DynamoDB
KeyConditionExpression: 'OrderID = :orderId',
ExpressionAttributeValues: { ':orderId': { N: '12345' } }
AReturns all items because partition key is ignored in key condition.
BReturns items with OrderID greater than 12345.
CReturns the item with OrderID 12345 and CustomerName 'Alice'.
DReturns no items because the key condition syntax is invalid.
Attempts:
2 left
💡 Hint
Remember, the key condition expression must specify the partition key equality.
query_result
intermediate
2:00remaining
Using sort key with BETWEEN in key condition
Consider a DynamoDB table Events with UserID as partition key and EventDate as sort key (string in 'YYYY-MM-DD' format). What will this key condition expression return?

UserID = :uid AND EventDate BETWEEN :start AND :end

With values:
:uid = 'user1'
:start = '2023-01-01'
:end = '2023-01-31'

The table has events for user1 on '2023-01-10', '2023-02-01', and '2023-01-20'.
DynamoDB
KeyConditionExpression: 'UserID = :uid AND EventDate BETWEEN :start AND :end',
ExpressionAttributeValues: { ':uid': { S: 'user1' }, ':start': { S: '2023-01-01' }, ':end': { S: '2023-01-31' } }
AReturns events on '2023-01-10' and '2023-01-20' for user1.
BReturns all events for user1 regardless of date.
CReturns only the event on '2023-02-01' for user1.
DReturns no events because BETWEEN is not valid in key condition.
Attempts:
2 left
💡 Hint
BETWEEN works on sort keys to filter a range of values.
📝 Syntax
advanced
2:00remaining
Identify syntax error in key condition expression
Which option contains a syntax error in the DynamoDB key condition expression?
AUserID = :uid AND begins_with(EventDate, :prefix)
BUserID = :uid OR EventDate = :date
CUserID = :uid AND EventDate > :date
DUserID = :uid AND EventDate BETWEEN :start AND :end
Attempts:
2 left
💡 Hint
Key condition expressions only allow AND, not OR.
query_result
advanced
2:00remaining
Effect of missing partition key in key condition
What happens if you run a DynamoDB query with this key condition expression?

EventDate = :date

Given that EventDate is the sort key and UserID is the partition key.
DynamoDB
KeyConditionExpression: 'EventDate = :date',
ExpressionAttributeValues: { ':date': { S: '2023-01-01' } }
AQuery returns all items with EventDate '2023-01-01' across all users.
BQuery returns all items in the table.
CQuery returns no items because sort key alone cannot be used.
DQuery fails with an error because partition key is missing.
Attempts:
2 left
💡 Hint
Partition key equality is mandatory in key condition expressions.
🧠 Conceptual
expert
2:00remaining
Understanding key condition expression limitations
Which statement correctly describes a limitation of DynamoDB key condition expressions?
AKey condition expressions must include the partition key with equality and can optionally include sort key conditions.
BKey condition expressions can filter on any attribute, including non-key attributes.
CKey condition expressions support OR operators to combine multiple conditions.
DKey condition expressions can use any comparison operator on the partition key.
Attempts:
2 left
💡 Hint
Think about what is mandatory and optional in key condition expressions.

Practice

(1/5)
1. What is the required part of a key condition expression in DynamoDB?
easy
A. Sort key condition
B. Partition key condition
C. Both partition and sort key conditions
D. Neither partition nor sort key condition

Solution

  1. Step 1: Understand key condition expression components

    Key condition expressions must always include the partition key condition to identify the partition to query.
  2. Step 2: Identify optional parts

    The sort key condition is optional and used to refine the query within the partition.
  3. Final Answer:

    Partition key condition -> Option B
  4. Quick Check:

    Partition key condition = required [OK]
Hint: Partition key condition is always required in key condition expressions [OK]
Common Mistakes:
  • Thinking sort key condition is mandatory
  • Using only sort key condition without partition key
  • Confusing key condition with filter expressions
2. Which of the following is the correct syntax for a key condition expression to find items with partition key 'UserID' equal to '123'?
easy
A. UserID = '123'
B. partition_key = 'UserID' AND sort_key = '123'
C. UserID == 123
D. Key('UserID').eq('123')

Solution

  1. Step 1: Identify correct key condition syntax

    In DynamoDB, key condition expressions use attribute names and operators like '=' with values in quotes for strings.
  2. Step 2: Evaluate each option

    UserID = '123' uses correct syntax: UserID = '123'. partition_key = 'UserID' AND sort_key = '123' uses wrong attribute names and syntax. UserID == 123 uses '==' which is invalid. Key('UserID').eq('123') is not valid DynamoDB syntax.
  3. Final Answer:

    UserID = '123' -> Option A
  4. Quick Check:

    Correct syntax uses '=' and quotes for strings [OK]
Hint: Use single '=' and quotes for string values in key condition expressions [OK]
Common Mistakes:
  • Using '==' instead of '='
  • Confusing attribute names with keywords
  • Using function syntax not supported in key conditions
3. Given a DynamoDB table with partition key 'UserID' and sort key 'Timestamp', what will this key condition expression return?
UserID = 'user1' AND Timestamp BETWEEN 100 AND 200
medium
A. All items with UserID 'user1' only
B. All items with Timestamp between 100 and 200 regardless of UserID
C. All items with UserID 'user1' and Timestamp between 100 and 200 inclusive
D. Syntax error due to BETWEEN usage

Solution

  1. Step 1: Analyze partition key condition

    The expression requires UserID to be 'user1', so only items with this partition key are considered.
  2. Step 2: Analyze sort key condition with BETWEEN

    The sort key Timestamp must be between 100 and 200 inclusive, filtering items within that range.
  3. Final Answer:

    All items with UserID 'user1' and Timestamp between 100 and 200 inclusive -> Option C
  4. Quick Check:

    Partition key + BETWEEN on sort key filters correctly [OK]
Hint: BETWEEN filters sort key range within partition key [OK]
Common Mistakes:
  • Ignoring partition key condition
  • Thinking BETWEEN applies to partition key
  • Assuming syntax error with BETWEEN
4. You wrote this key condition expression:
begins_with(UserID, :uid) AND Timestamp = :ts
But your query returns an error. What is the likely cause?
medium
A. Using begins_with on the partition key
B. Missing partition key condition
C. begins_with function used on sort key is valid, so no error
D. Incorrect use of AND operator

Solution

  1. Step 1: Identify key condition expression rules

    Partition key condition must use '=' operator; functions like begins_with are not allowed on partition key.
  2. Step 2: Check usage of begins_with

    begins_with is valid only on sort key, not partition key. If used on partition key, it causes an error.
  3. Final Answer:

    Using begins_with on the partition key -> Option A
  4. Quick Check:

    begins_with only valid on sort key [OK]
Hint: begins_with only works on sort key, not partition key [OK]
Common Mistakes:
  • Using begins_with on partition key
  • Omitting partition key condition
  • Misusing AND operator syntax
5. You want to query a DynamoDB table with partition key 'Category' and sort key 'Date'. You need all items where Category is 'Books' and Date starts with '2023-06'. Which key condition expression should you use?
hard
A. begins_with(Category, 'Books') AND Date = '2023-06'
B. Category = 'Books' AND Date BETWEEN '2023-06-01' AND '2023-06-30'
C. Category = 'Books' OR begins_with(Date, '2023-06')
D. Category = 'Books' AND begins_with(Date, '2023-06')

Solution

  1. Step 1: Identify partition key condition

    Category must be exactly 'Books', so use equality operator on partition key.
  2. Step 2: Use begins_with on sort key

    Date starts with '2023-06' means use begins_with function on sort key Date.
  3. Step 3: Evaluate options

    Category = 'Books' AND begins_with(Date, '2023-06') correctly combines equality on partition key and begins_with on sort key. Category = 'Books' AND Date BETWEEN '2023-06-01' AND '2023-06-30' uses BETWEEN but requires exact date range, which is more complex. Category = 'Books' OR begins_with(Date, '2023-06') uses OR which is invalid in key condition expressions. begins_with(Category, 'Books') AND Date = '2023-06' incorrectly uses begins_with on partition key.
  4. Final Answer:

    Category = 'Books' AND begins_with(Date, '2023-06') -> Option D
  5. Quick Check:

    Partition key = value AND begins_with on sort key [OK]
Hint: Use '=' for partition key and begins_with() for sort key prefix [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using begins_with on partition key
  • Using BETWEEN without exact date range