Azure Table storage is a NoSQL key-value store that can store a large amount of structured, non-relational data. This document explains how to query entities within an Azure Table. Querying is a fundamental operation for retrieving specific data that meets certain criteria. Azure Table storage supports a rich query language that allows for filtering, sorting, and projection of entities.
Filter expressions are string literals that define the query criteria. They are composed of property names, comparison operators, and literal values. You can combine multiple conditions using logical operators (and, or).
The following operators are supported in filter expressions:
| Operator | Description |
|---|---|
eq |
Equal |
ne |
Not equal |
gt |
Greater than |
ge |
Greater than or equal to |
lt |
Less than |
le |
Less than or equal to |
You can combine conditions using the following logical operators:
and: Both conditions must be true.or: At least one of the conditions must be true.When constructing filter expressions, be mindful of property types and how to represent literals:
'completed').L (e.g., datetime'2023-10-27T10:00:00Z'L).guid (e.g., guid'a1b2c3d4-e5f6-7890-1234-567890abcdef'guid).true or false.y (e.g., 100y).l (e.g., 2000l).123.45).Azure Table storage reserves two special properties for all entities:
These properties are crucial for efficient querying. Queries that filter on PartitionKey are highly efficient because they can retrieve all entities within a specific partition. Queries that include both PartitionKey and RowKey can retrieve a single entity very quickly.
To retrieve all entities from a partition named Customers:
PartitionKey eq 'Customers'
To retrieve a specific entity with PartitionKey Customers and RowKey user123:
PartitionKey eq 'Customers' and RowKey eq 'user123'
To retrieve entities where the Status property is 'Active':
Status eq 'Active'
To retrieve active customers who registered after a specific date:
PartitionKey eq 'Customers' and Status eq 'Active' and Timestamp gt datetime'2023-01-01T00:00:00Z'L
To retrieve orders with a total amount greater than 100:
OrderTotal gt 100.00
PartitionKey and RowKey. Queries that can be satisfied by a single partition access are significantly faster.
Projection allows you to specify which properties to return in the query results, reducing the amount of data transferred and processed. This is done by including a $select query option with a comma-separated list of property names.
To retrieve only the Name and Email properties for all active customers:
PartitionKey eq 'Customers' and Status eq 'Active' &$select=Name,Email
Azure Table storage supports ordering query results. You can specify an order by property and direction (ascending or descending) using the $orderby query option.
PartitionKey and RowKey must be the first properties in the order-by clause, or the query must be scoped to a single partition.
To retrieve all active customers, ordered by their registration date in ascending order:
PartitionKey eq 'Customers' and Status eq 'Active' &$orderby=Timestamp asc
You can also order by multiple properties:
PartitionKey eq 'Orders' &$orderby=OrderDate desc, OrderTotal asc
Now that you understand how to query Azure Storage Tables, you can proceed to learn about: