Bird
Raised Fist0
DynamoDBquery~20 mins

Querying GSI in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of querying a Global Secondary Index (GSI) in DynamoDB?
easy
A. To delete items from the table
B. To create a backup of the table
C. To update the primary key of items
D. To find items using an alternate key without scanning the whole table

Solution

  1. Step 1: Understand what a GSI is used for

    A GSI allows querying data using a different key than the primary key.
  2. Step 2: Identify the main benefit of querying a GSI

    Querying a GSI helps find items quickly without scanning the entire table.
  3. Final Answer:

    To find items using an alternate key without scanning the whole table -> Option D
  4. Quick Check:

    Query GSI = alternate key search [OK]
Hint: GSI lets you search by different keys fast [OK]
Common Mistakes:
  • Confusing GSI query with table scan
  • Thinking GSI is for deleting or updating keys
  • Assuming GSI creates backups
2. Which of the following is the correct way to specify a GSI name in a DynamoDB query using AWS SDK?
easy
A. query({ TableName: 'MyTable', Index: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } })
B. query({ TableName: 'MyTable', KeyName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } })
C. query({ TableName: 'MyTable', IndexName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } })
D. query({ TableName: 'MyTable', GSIName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } })

Solution

  1. Step 1: Recall the correct parameter name for GSI in DynamoDB query

    The correct parameter to specify a GSI is 'IndexName'.
  2. Step 2: Check the options for correct syntax

    Only query({ TableName: 'MyTable', IndexName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } }) uses 'IndexName' correctly with other required fields.
  3. Final Answer:

    query({ TableName: 'MyTable', IndexName: 'MyGSI', KeyConditionExpression: 'GSIKey = :val', ExpressionAttributeValues: { ':val': '123' } }) -> Option C
  4. Quick Check:

    Use IndexName to specify GSI [OK]
Hint: Use 'IndexName' to specify GSI in query params [OK]
Common Mistakes:
  • Using 'KeyName' or 'GSIName' instead of 'IndexName'
  • Omitting ExpressionAttributeValues
  • Wrong key condition syntax
3. Given a DynamoDB table with a GSI named 'StatusIndex' on attribute 'status', what will the following query return?
query({ TableName: 'Orders', IndexName: 'StatusIndex', KeyConditionExpression: 'status = :s', ExpressionAttributeValues: { ':s': 'shipped' } })
medium
A. All orders with status 'shipped' using the GSI
B. All orders regardless of status
C. An error because 'status' is not the primary key
D. No results because GSI queries are not supported

Solution

  1. Step 1: Understand the query parameters

    The query uses 'StatusIndex' GSI with a key condition on 'status' = 'shipped'.
  2. Step 2: Determine what the query returns

    It returns all items where 'status' attribute equals 'shipped' using the GSI, which is designed for this attribute.
  3. Final Answer:

    All orders with status 'shipped' using the GSI -> Option A
  4. Quick Check:

    Query GSI by status = shipped returns matching items [OK]
Hint: Query GSI with key condition returns matching items [OK]
Common Mistakes:
  • Thinking GSI query returns all items
  • Assuming GSI query causes error if attribute not primary key
  • Believing GSI queries are unsupported
4. You wrote this DynamoDB query to use a GSI but get an error:
query({ TableName: 'Products', IndexName: 'CategoryIndex', KeyConditionExpression: 'category = :c', ExpressionAttributeValues: { ':c': 'books' } })

What is the most likely cause of the error?
medium
A. You cannot query a GSI in DynamoDB
B. The attribute 'category' is not the partition key of the GSI 'CategoryIndex'
C. ExpressionAttributeValues is missing a value
D. The table name 'Products' is incorrect

Solution

  1. Step 1: Understand GSI key requirements

    When querying a GSI, the KeyConditionExpression must use the GSI's partition key attribute.
  2. Step 2: Identify the error cause

    If 'category' is not the partition key of 'CategoryIndex', the query will fail with an error.
  3. Final Answer:

    The attribute 'category' is not the partition key of the GSI 'CategoryIndex' -> Option B
  4. Quick Check:

    GSI query key must be GSI partition key [OK]
Hint: GSI query key must match GSI partition key [OK]
Common Mistakes:
  • Using a non-key attribute in KeyConditionExpression
  • Assuming table name typo causes this error
  • Believing GSI queries are unsupported
5. You have a DynamoDB table 'Employees' with a GSI named 'DeptIndex' where the partition key is 'department' and sort key is 'hireDate'. You want to find all employees in 'Sales' hired after '2023-01-01'. Which query is correct?
hard
A. query({ TableName: 'Employees', IndexName: 'DeptIndex', KeyConditionExpression: 'department = :d AND hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } })
B. query({ TableName: 'Employees', IndexName: 'DeptIndex', KeyConditionExpression: 'department = :d OR hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } })
C. query({ TableName: 'Employees', KeyConditionExpression: 'department = :d AND hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } })
D. query({ TableName: 'Employees', IndexName: 'DeptIndex', FilterExpression: 'department = :d AND hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } })

Solution

  1. Step 1: Use the GSI name and correct key condition

    Query must specify 'IndexName' as 'DeptIndex' and use KeyConditionExpression with partition key and sort key conditions.
  2. Step 2: Check logical operators and expressions

    KeyConditionExpression supports 'AND' for partition key and sort key conditions; 'OR' is invalid here.
  3. Step 3: Verify FilterExpression usage

    FilterExpression filters after query; it cannot replace KeyConditionExpression for keys.
  4. Final Answer:

    query({ TableName: 'Employees', IndexName: 'DeptIndex', KeyConditionExpression: 'department = :d AND hireDate > :h', ExpressionAttributeValues: { ':d': 'Sales', ':h': '2023-01-01' } }) -> Option A
  5. Quick Check:

    Use KeyConditionExpression with AND on GSI keys [OK]
Hint: Use AND in KeyConditionExpression for partition and sort keys [OK]
Common Mistakes:
  • Using OR instead of AND in KeyConditionExpression
  • Omitting IndexName for GSI query
  • Using FilterExpression instead of KeyConditionExpression for keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard