0
0
DynamoDBquery~20 mins

Querying GSI in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GSI Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Querying a GSI with a partition key

Given a DynamoDB table with a Global Secondary Index (GSI) named GSI1 having category as the partition key, what will be the output of the following query?

const params = {
  TableName: 'Products',
  IndexName: 'GSI1',
  KeyConditionExpression: 'category = :cat',
  ExpressionAttributeValues: { ':cat': { S: 'Books' } }
};

// Assume the table has these items:
// { id: '1', category: 'Books', title: 'Learn SQL' }
// { id: '2', category: 'Electronics', title: 'Smartphone' }
// { id: '3', category: 'Books', title: 'DynamoDB Guide' }

// Query is executed with these params.

What items will be returned?

DynamoDB
const params = {
  TableName: 'Products',
  IndexName: 'GSI1',
  KeyConditionExpression: 'category = :cat',
  ExpressionAttributeValues: { ':cat': { S: 'Books' } }
};
A[{ id: '1', category: 'Books', title: 'Learn SQL' }]
B[{ id: '2', category: 'Electronics', title: 'Smartphone' }]
C[]
D[{ id: '1', category: 'Books', title: 'Learn SQL' }, { id: '3', category: 'Books', title: 'DynamoDB Guide' }]
Attempts:
2 left
💡 Hint

Remember, querying a GSI with a partition key returns all items matching that key in the index.

🧠 Conceptual
intermediate
1:30remaining
Understanding GSI consistency

Which statement about querying a Global Secondary Index (GSI) in DynamoDB is true?

AQueries on a GSI return eventually consistent results by default.
BQueries on a GSI always return strongly consistent results.
CQueries on a GSI cannot use filter expressions.
DQueries on a GSI require the base table's primary key in the query condition.
Attempts:
2 left
💡 Hint

Think about how DynamoDB replicates data to GSIs.

📝 Syntax
advanced
2:30remaining
Correct syntax for querying a GSI with sort key condition

Which of the following DynamoDB query parameter objects correctly queries a GSI named GSI2 with partition key category equal to 'Books' and sort key price less than 20?

A{ TableName: 'Products', IndexName: 'GSI2', KeyConditionExpression: 'category = :cat AND price < :p', ExpressionAttributeValues: { ':cat': 'Books', ':p': 20 } }
B{ TableName: 'Products', IndexName: 'GSI2', KeyConditionExpression: 'category = :cat AND price < :p', ExpressionAttributeValues: { ':cat': { S: 'Books' }, ':p': { N: '20' } } }
C{ TableName: 'Products', IndexName: 'GSI2', KeyConditionExpression: 'category = :cat AND price <= :p', ExpressionAttributeValues: { ':cat': { S: 'Books' }, ':p': { N: '20' } } }
D{ TableName: 'Products', IndexName: 'GSI2', KeyConditionExpression: 'category = :cat AND price < :p', ExpressionAttributeValues: { ':cat': { S: 'Books' }, ':p': 20 } }
Attempts:
2 left
💡 Hint

Check the format of ExpressionAttributeValues for DynamoDB SDK.

optimization
advanced
1:30remaining
Optimizing GSI queries with projections

You want to reduce the amount of data returned by a GSI query to only the id and title attributes. Which parameter should you add to your query?

AAdd <code>KeyConditionExpression: 'id, title'</code> to the query parameters.
BAdd <code>FilterExpression: 'id, title'</code> to the query parameters.
CAdd <code>ProjectionExpression: 'id, title'</code> to the query parameters.
DAdd <code>ExpressionAttributeNames: { '#id': 'id', '#title': 'title' }</code> only.
Attempts:
2 left
💡 Hint

Think about how to specify which attributes to return in DynamoDB queries.

🔧 Debug
expert
3:00remaining
Diagnosing a GSI query returning no results

A developer runs this query on a DynamoDB table with a GSI named GSI1:

const params = {
  TableName: 'Orders',
  IndexName: 'GSI1',
  KeyConditionExpression: 'orderDate = :date',
  ExpressionAttributeValues: { ':date': { S: '2023-01-01' } }
};

But the query returns no items, even though there are orders on that date. What is the most likely cause?

AThe GSI's partition key is not <code>orderDate</code>, so the query condition is invalid.
BThe <code>ExpressionAttributeValues</code> is missing a data type for <code>:date</code>.
CThe query must include the base table's primary key in the condition.
DThe <code>IndexName</code> parameter should be omitted when querying a GSI.
Attempts:
2 left
💡 Hint

Check the GSI's key schema and what the query condition uses.