AWS DynamoDB - Working with Queries
Last Updated :
28 Mar, 2023
Amazon DynamoDB is a NoSQL managed database service provided by Amazon that stores semi-structured data like key-value pairs. A DynamoDB table consists of items. Each item consists of one partition key and one or more attributes. An example of an item is given below:
Example:
{
"MovieID": 101,
"Name": "The Shawshank Redemption",
"Rating": 9.2,
"Year": 1994
}
In the above example, MovieID is the partition key.
A partition key is used to differentiate between items. A query operation in DynamoDB finds items based on primary key values. The name of the partition key attribute and a single value for that attribute must be provided. The query returns all items searched against that partition key value.
Create a table and add values :
A table say, Movies, with partition key as MovieID has been created already. No two items can have the same partition key. Add few items to query upon. A table has already been created with items in it. See the below image:

Query on the table :
To query upon items in the table, select Query from the dropdown in the items tab. By default, a query will always have a partition key as one of the search filters. We can add one or more attributes to refine the search. See the below image:

In the above image we see that when we enter 50 for primary key MovieID, we obtain 1 record which is shown above in the image.
Adding Filters to Query:
To refine our search we can add one or more filters. In filter, we select an attribute and provide value against it. The query results returned is determined by a filter expression. All of the other results are discarded. A filter expression is applied before the results are returned, but after a Query finishes. Hence, a Query consumes the same amount of Read capacity, regardless of whether a filter expression is present or not. See the below image:

In the above image, we see that in the filter, Rating has been provided and the condition is that it should be greater than or equal to 8. Thus, we get one search result.
Limiting the number of items in ResultSet:
The Query operation allows you to limit the number of items that it reads. To do this, set the Limit parameter to the desired number of items that you want. For example, suppose that you Query a table, with a limit value of 8, and without a filter expression. The Query result contains the first eight items from the table that match the key condition expression from the request. The limit can only be used in Amazon Command Line Interface (CLI).
Paging:
This feature is available on Amazon CLI (Command Line Interface). When data retrieved is larger than 1 MB in the result set, then the result set is divided into pages, each page containing up to 1 MB. For instance, if 3 MB data is retrieved then there will be at least 3 pages.
Counting Item in ResultSet:
If the size of the Query result set is larger than 1 MB, ScannedCount and Count represent only a partial count of the total items. You need to perform multiple Query operations to retrieve all items in the result set.
Capacity Units Consumed:
A read capacity unit speaks for one strongly consistent read per second, or two eventually consistent reads per second, for an item up to the size of 4 KB. A scan operation does not return any data on how much the read capacity units are consumed. However, by specifying the ReturnConsumedCapacity parameter in a Scan request we can obtain this information or change the read capacity unit in the capacity tab of the table. See the below image:

Read Consistency:
By default, a scan operation performs eventually consistent reads. Meaning, the scan results might not include changes due to the recently completed PutItem or UpdateItem request. If required strong consistent reads, as of the time that the Scan begins, then set ConsistentRead parameter to true in the Scan request. By doing so, it ensures that all the write operations that completed before the Scan began are included in the Scan result set.
How to query the table using attributes other than the primary key.:
Using concept of "secondary indexes" DynamoDB allows you to create one or more secondary indexes on a table, which can allow you to query the table using attributes other than the primary key. This can be useful if you want to perform queries on the table based on attributes other than the primary key, or if you want to support multiple query patterns on the same table. There are two types of secondary indexes in DynamoDB: global secondary indexes and local secondary indexes. Global secondary indexes allow you to query the table using an alternate key, while local secondary indexes allow you to query the table using an alternate key that is defined within the primary key. Both types of secondary indexes have their own provisioned throughput, which you can set when you create the index.
Similar Reads
AWS DynamoDB - Working with Indexes
An index is a data structure that enables us to perform fast queries on different columns in a table. After creating an index, the database handles it for us. Whenever data is modified in the table, the index is automatically modified to reflect changes in the table. We can create and use a secondar
2 min read
AWS DynamoDB - Working with Streams
DynamoDB Streams is a DynamoDB feature that allows users to keep track of any changes made to the data in DynamoDB. It is an "ordered flow of data" that contains information about changes made to the data in the DynamoDB table. Let us talk of a use case. Consider a "users" table in DynamoDB and your
3 min read
AWS DynamoDB - Working with Tables
In this article, we will work on DynamoDB tables. DynamoDB is a NoSQL database that stores document data or key-value pairs. A Dynamodb table consists of items and each item is made up of attributes. Different items can have different attributes. See the below example: Example 1: { "MovieID": 123, "
3 min read
AWS DynamoDB - Working with Scans
Amazon DynamoDB is NoSQL managed database that stores semi-structured data like key-value pairs and document data. When creating tables in DynamoDB, no schema structure is required but only a partition key (primary key) is required. DynamoDB tables stores data in form of items and each item consists
3 min read
AWS DynamoDB - Working with Items & Attributes
AWS DynamoDB is a NoSQL managed database that stores semi-structured data i.e. key-value and document data. It stores data in form of an item. An item consists of attributes. Upon table creation in DynamoDB, it only requires a primary key to differentiate between items and no schema is to be defined
3 min read
AWS DynamoDB - Working with Backups
Amazon DynamoDB supports on-demand backup and restores features. Those features are available to the user independent of whether the user uses AWS Backup or not. Users can use the DynamoDB on-demand backup capability to create full backups of its tables for a long-term period and archival for regula
3 min read
Amazon RDS - Working with Read Replicas
This article intends to make you aware of "Read Replicas". As the name itself indicates, read replica allows us to have "non-editable copies" of our production database. This is done by doing asynchronous replication from the primary RDS Instance, to the replicas, i.e. updates made on the source dat
3 min read
AWS DynamoDB - Introduction to NoSQL Workbench
This article intends to introduce you to the basics of NoSQL Workbench. NoSQL is basically a nonrelational database, with numerous other extra advantages too. NoSQL Workbench is nothing but a virtual development as well as a testing environment for developers anywhere in the world. It allows its use
2 min read
AWS DynamoDB - Write Data to a Table
In this article, we will write data to a table in DynamoDB. DynamoDB is a NoSQL database that supports semi-structured data i.e. key-value and document data structures. A DynamoDB table contains items. An item is a collection of attributes and each item has a primary key that should be not null. Als
2 min read
AWS DynamoDB - Primary Key
Primary keys are used for uniquely identifying each item in a table. No two-item can have the same primary key. In DynamoDB the primary key must be specified along with the table name while creating a table. DynamoDB supports two different kinds of primary keys: Partition keyPartition key and sort k
2 min read