Bird
Raised Fist0
DynamoDBquery~20 mins

Filter 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
🎖️
Filter Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Filter expression with attribute_exists
Given a DynamoDB table with items having attributes id and status, what will be the output of this filter expression?

FilterExpression: "attribute_exists(status) AND status = :val"

with :val set to "active"?
AReturns all items regardless of the <code>status</code> attribute.
BReturns only items where the attribute <code>status</code> exists and its value is exactly "active".
CReturns items where <code>status</code> does not exist or is "active".
DReturns items where <code>status</code> exists but its value is not "active".
Attempts:
2 left
💡 Hint
Think about what attribute_exists means and how it combines with the equality check.
query_result
intermediate
2:00remaining
Using begins_with in filter expressions
What will be the result of this filter expression on a DynamoDB table with a username attribute?

FilterExpression: "begins_with(username, :prefix)"

with :prefix set to "user"?
AReturns items where the <code>username</code> ends with "user".
BReturns items where the <code>username</code> contains "user" anywhere.
CReturns items where the <code>username</code> starts with "user".
DReturns items where the <code>username</code> is exactly "user".
Attempts:
2 left
💡 Hint
Remember what the function begins_with checks in DynamoDB.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this filter expression
Which option contains a syntax error in the DynamoDB filter expression?

Expression: FilterExpression: "status = :val AND (attribute_exists(age) OR age > :minAge"

Assume :val and :minAge are defined.
AMissing closing parenthesis in the filter expression causes a syntax error.
BUsing <code>attribute_exists</code> inside parentheses is not allowed.
CUsing comparison operators like <code>></code> in filter expressions is not allowed.
DUsing <code>AND</code> and <code>OR</code> together is invalid syntax.
Attempts:
2 left
💡 Hint
Check if all parentheses are properly closed.
optimization
advanced
2:00remaining
Optimizing filter expressions for performance
Which filter expression is more efficient for filtering items where score is between 50 and 100 inclusive in DynamoDB?
AFilterExpression: "score BETWEEN :min AND :max" with <code>:min=50</code> and <code>:max=100</code>
BFilterExpression: "score >= :min AND score <= :max" with <code>:min=50</code> and <code>:max=100</code>
CFilterExpression: "score > :min AND score < :max" with <code>:min=49</code> and <code>:max=101</code>
DFilterExpression: "score IN (:min, :max)" with <code>:min=50</code> and <code>:max=100</code>
Attempts:
2 left
💡 Hint
Consider which expression is simpler and uses built-in operators.
🧠 Conceptual
expert
2:00remaining
Understanding filter expressions impact on read capacity
Which statement best describes how filter expressions affect DynamoDB read capacity units (RCUs) when scanning a table?
AFilter expressions cause DynamoDB to read only matching items, greatly reducing RCUs.
BFilter expressions reduce the number of items read from the table, lowering RCUs consumed.
CFilter expressions increase RCUs consumed because they add extra processing on the server.
DFilter expressions do not reduce the number of items read; they only filter results after reading, so RCUs consumed remain the same.
Attempts:
2 left
💡 Hint
Think about when filtering happens during a scan operation.

Practice

(1/5)
1. What is the main purpose of a FilterExpression in a DynamoDB scan or query?
easy
A. To return only items that meet specific conditions after reading all data
B. To reduce the size of the table permanently
C. To speed up the write operations
D. To create new indexes automatically

Solution

  1. Step 1: Understand what FilterExpression does

    A FilterExpression is used to specify conditions that items must meet to be returned after the scan or query reads the data.
  2. Step 2: Differentiate from other operations

    It does not change the table or speed up writes; it only filters results after reading.
  3. Final Answer:

    To return only items that meet specific conditions after reading all data -> Option A
  4. Quick Check:

    FilterExpression filters results after reading [OK]
Hint: FilterExpression filters results after reading data [OK]
Common Mistakes:
  • Thinking FilterExpression reduces read capacity usage
  • Confusing FilterExpression with key condition
  • Assuming it changes the table data
2. Which of the following is the correct syntax for a FilterExpression to find items where attribute status equals 'active' in DynamoDB?
easy
A. FilterExpression: "status = 'active'"
B. FilterExpression: "status == 'active'"
C. FilterExpression: "status equals 'active'"
D. FilterExpression: "status === 'active'"

Solution

  1. Step 1: Recall DynamoDB FilterExpression syntax

    DynamoDB uses single equals sign (=) for equality in FilterExpressions.
  2. Step 2: Check each option

    FilterExpression: "status = 'active'" uses correct syntax. The other options use invalid operators (==, equals, ===) or keywords.
  3. Final Answer:

    FilterExpression: "status = 'active'" -> Option A
  4. Quick Check:

    Use single equals (=) for equality in FilterExpression [OK]
Hint: Use single '=' for equality in FilterExpression [OK]
Common Mistakes:
  • Using double or triple equals (==, ===)
  • Writing 'equals' as a word
  • Using incorrect operators
3. Given a DynamoDB table with items having attribute age, what will be the result of this scan with FilterExpression age < 30 if the table has ages 25, 30, 35, and 40?
medium
A. Items with ages 25 and 30
B. Items with ages 25 only
C. Items with ages 30, 35, and 40
D. Items with ages 35 and 40

Solution

  1. Step 1: Understand the FilterExpression condition

    The condition is age < 30, meaning only items with age less than 30 pass the filter.
  2. Step 2: Check which ages satisfy the condition

    From the list (25, 30, 35, 40), only 25 is less than 30.
  3. Final Answer:

    Items with ages 25 only -> Option B
  4. Quick Check:

    age < 30 means less than 30 [OK]
Hint: Remember < means strictly less than [OK]
Common Mistakes:
  • Including age 30 when condition is less than 30
  • Confusing < with <= operator
  • Assuming filter changes data in table
4. You wrote this FilterExpression: contains(name, 'John') but it returns no results even though some items have 'John' in their name. What is the likely mistake?
medium
A. Using contains() instead of equals()
B. Not using ExpressionAttributeNames for reserved words
C. FilterExpression is case-sensitive and 'John' does not match 'john'
D. FilterExpression must be in uppercase

Solution

  1. Step 1: Understand contains() behavior

    contains() checks if the attribute contains the exact substring, including case.
  2. Step 2: Consider case sensitivity

    If the data has 'john' lowercase but the filter uses 'John' with uppercase J, no match occurs.
  3. Step 3: Check other choices

    Using equals() instead of contains() is not the issue since contains() is appropriate for substrings; reserved words would cause a different error; FilterExpressions are not required to be uppercase.
  4. Final Answer:

    FilterExpression is case-sensitive and 'John' does not match 'john' -> Option C
  5. Quick Check:

    contains() is case-sensitive [OK]
Hint: Remember contains() is case-sensitive [OK]
Common Mistakes:
  • Ignoring case sensitivity in contains()
  • Not using ExpressionAttributeNames when needed
  • Assuming contains() works like equals()
5. You want to scan a DynamoDB table to find items where score is greater than 50 and status is 'active'. Which FilterExpression correctly applies both conditions?
hard
A. FilterExpression: "score > 50, status = 'active'"
B. FilterExpression: "score > 50 OR status = 'active'"
C. FilterExpression: "score > 50 && status == 'active'"
D. FilterExpression: "score > 50 AND status = 'active'"

Solution

  1. Step 1: Understand logical operators in FilterExpression

    DynamoDB uses uppercase AND and OR for combining conditions.
  2. Step 2: Check each option's syntax

    FilterExpression: "score > 50 AND status = 'active'" uses correct syntax with AND and single equals. FilterExpression: "score > 50 OR status = 'active'" uses OR which is incorrect for both conditions. FilterExpression: "score > 50, status = 'active'" uses comma which is invalid. FilterExpression: "score > 50 && status == 'active'" uses && and double equals which are invalid.
  3. Final Answer:

    FilterExpression: "score > 50 AND status = 'active'" -> Option D
  4. Quick Check:

    Use AND in uppercase and single '=' for equality [OK]
Hint: Use uppercase AND and single '=' in FilterExpression [OK]
Common Mistakes:
  • Using OR instead of AND for both conditions
  • Using commas or && instead of AND
  • Using double equals (==) instead of single equals (=)