How to query data in DynamoDB

A query is a request for data from a database based on defined criteria. The specified primary key determines the location of data in a DynamoDB table, so we can use the attributes of the primary key to query the database. These primary keys can be of two types:

  • The simple primary key comprises only one data attribute—a partition keyIt determines the partition in which the data will be stored..

  • The composite primary key consists of two data attributes—a partition key and a sort keyIt determines the order in which the data is stored in a partition..

Let’s look at how we can query DynamoDB using both primary keys.

Query using the partition key

We’ll first look at the case where the created table uses the simple primary key. In this case, only one data attribute can be used while querying. Let’s start by creating a table using the command given below:

aws dynamodb create-table \
--table-name Blog \
--attribute-definitions \
AttributeName=Author,AttributeType=S \
--key-schema \
AttributeName=Author,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

This command will create a table called Blog with the data attribute Author as its simple primary key. The KeyType for partition key is HASH, since it is used as input to a hash function to determine the partition in which the item will be stored.

Next, populate the table using this command:

aws dynamodb batch-write-item \
--request-items '{
"Blog": [
{
"PutRequest": {
"Item": {
"Author": {"S": "John Doe"},
"Title": {"S": "My favorite blog"}
}
}
},
{
"PutRequest": {
"Item": {
"Author": {"S": "Jane Doe"},
"Title": {"S": "My first blog post"},
"Body": {"S": "Hello world"}
}
}
}
]
}' \
--return-consumed-capacity TOTAL

Note: You can refer to this documentationhttps://docs.aws.amazon.com/powershell/latest/userguide/creds-idc.html to see all the options related to the batch-write-item command and its syntax.

Now, let’s query the table using this primary key. Run the command given in the terminal below.

aws dynamodb query \
--table-name Blog \
--key-condition-expression "Author = :JD" \
--expression-attribute-values '{":JD": {"S": "John Doe"}}'

This command will fetch the information about the author John Doe from the database.

Query using the partition key and filter using the sort key

Let’s look at how to query a table with a composite primary key. In this case, we have an additional data attribute, the sort key, that we can use to filter the data. Let’s create another table that uses a composite primary key using the command given below:

aws dynamodb create-table \
--table-name Books \
--attribute-definitions \
AttributeName=Author,AttributeType=S \
AttributeName=Title,AttributeType=S \
--key-schema \
AttributeName=Author,KeyType=HASH \
AttributeName=Title,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1

This command will create a table called Books with the data attribute Author as the partition key and Title as the sort key. The KeyType for sort key is RANGE, since it allows the user to define a range of values within each partition to store the item.

Next, populate the table using this command:

aws dynamodb batch-write-item \
--request-items '{
"Books": [
{
"PutRequest": {
"Item": {
"Author": {"S": "John Doe"},
"Title": {"S": "My favorite blog"},
"Body": {"S": "Hi world"}
}
}
},
{
"PutRequest": {
"Item": {
"Author": {"S": "John Doe"},
"Title": {"S": "My first blog post"},
"Body": {"S": "Hello world"}
}
}
}
]
}' \
--return-consumed-capacity TOTAL

Let’s query the table using both attributes of the primary key. Run the command given in the terminal below.

aws dynamodb query \
--table-name Books \
--key-condition-expression 'Author= :JD AND Title = :name' \
--expression-attribute-values \
"{
\":JD\" : {\"S\" : \"John Doe\"},
\":name\" : {\"S\" : \"My first blog post\"}
}"

This command will fetch the information about the book My first blog post written by the author John Doe, from the database.

As we can see, using the composite primary key attributes provides us with an additional feature of filtration while querying the database

Practice

Run the commands given above using this widget. Enter your AWS access_key_id and secret_access_key in the widget below before running any commands. If you don’t have these keys, follow the steps in this documentation to generate the keys.

Note: The IAM user whose credentials are being used must have the permissions to perform all the required actions.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved