Query items in a DynamoDB table

In the last tutorial, we learned how to insert items into a table. In this section of the tutorial, we will learn how to query items in a DynamoDB table. There are few items that we inserted into the table. We can perform a query operation in the primary key. Based on the primary key we added to the table we can perform a query.

In the previous example, we performed have created a table with a primary key a combination of the partition key and a sort key. Let us perform some queries on this table.

The query can be done using using

  • AWS console UI

In this tutorial, we will be covering the AWS console, AWS CLI and AWS SDK in this tutorial.

Query items in a DynamoDB table using AWS console

To use the user interface, log in to AWS console > Go to service > Go to DynamoDB.

There choose the table books we created. There you can see some of the items we inserted. You can also add new items if you need some from the UI.

There you can see a scan/query section. Choose query from the drop-down menu

AWS console to query items in a DynamoDB table

In this console select the query option from the drop-down menu. Once you select the query option. You will have an option to specify what query you want to perform. You can query using either Author or using Author and Category both. You cannot query using category only. As the category is the sort key. The query should at least contain the partition key which in our case in the Author.

Add a filter value to the Author or the Author and the Category and select start search. You can select Ascending or Descending based on your needs. We will talk about the attributes all/projected in the coming section when we talk about indexes. For now, keep it all selected. Now hit the start search and you will see the result.


You can also provide a filter in these results. Select the add filter option and filter on the attributes. You can add a filter on any attribute. This will filter the output of the result from the query. Note that it is not efficient to filter large data. Filter does not support list and map type attributes.

Query items using AWS CLI

Another way to query items is to use AWS CLI. With AWS CLI installed you can use the DynamoDB commands to perform a query on the table.

aws dynamodb query \
    --table-name books \
    --key-condition-expression "Author = :author" \
    --expression-attribute-values '{
        ":author": { "S": "Daniel Kahneman" }

This will query the table books for the Author name Daniel Kahneman. This will return us the output as follows:

"Count": 1,
"Items": [
"Author": {
"S": "Daniel Kahneman"
"Category": {
"S": "self help"
"tile": {
"N": "Thinking fast and slow"
"ScannedCount": 1,
"ConsumedCapacity": null

There are a few other options that you can provide while querying the data:

–consistent-read – The read ensures that the strong consistency is met. By default, this value is false and eventual consistency is only met.
–key-condition-expression – In the above example, we used this option to query our data. For the partition key, you can use the exact value. Whereas for the partition key you have more options like You can use between A AND B, Greater than A, Greater than or equal.

–projection-expression – If you use this option. You can specify what attributes you want to select as output. For example, if you select only Author, the output will not contain other fields. If you select “All”, all the attributes will be in the output.
–scan-index-forward – Asc/Desc
–query-filter – Adds a filter in the output of the query data.
–max-items – How many data you want to return max. If the items are more than returned NextToken is provided for pagination options.
–starting-token – The token from above if provided will paginate from the last point.

A maximum of 1mb of data is only returned while querying. You will be provided with the NextToken along with the result to paginate. This can be used with the –starting-token option to continue pagination.

Query items using AWS Golang SDK

You can also use any AWS SDK for querying the data. In this tutorial, we will be using the Golang SDK to query the items.

// queryItems.go

mySession := session.Must(session.NewSession())
// Create a DynamoDB client from just a session.
svc := dynamodb.New(mySession)
input := &dynamodb.QueryInput{
     TableName: "books",
     ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
         ":Author": {
             S: aws.String("Daniel Kahneman"),
     KeyConditionExpression: aws.String("Author = :Author"),
 output, err := svc.Query(input)
 if err != nil {
     return nil