What is Azure Cosmos DB SQL API?
Azure Cosmos DB is a globally distributed, multi-model database service that enables you to harness the benefits of NoSQL capabilities with a familiar SQL query experience. The SQL API for Cosmos DB allows developers to interact with their data using a rich, declarative query language that is very similar to Transact-SQL (T-SQL) used in SQL Server.
It's designed for modern application development, offering:
- Low Latency: Millisecond read and write latencies globally.
- High Throughput: Scalable performance to handle demanding workloads.
- High Availability: Guaranteed availability with multi-region replication.
- Schema Agnostic: Store and query any structured, semi-structured, and unstructured data.
Key Concepts
Understanding these core concepts is crucial for working with Cosmos DB:
Accounts
The top-level resource for Cosmos DB. It contains all your databases, containers, and data.
Databases
A namespace for containers. Each database can contain one or more containers.
Containers
The fundamental unit of scalability and throughput in Cosmos DB. It holds items (documents) and their respective index properties.
Items
The actual data stored in a container, typically in JSON format. Items can be thought of as documents.
Partition Key
A property within your items that Cosmos DB uses to determine how your data is distributed across logical partitions for scalability and performance.
Request Units (RUs)
The unit of throughput in Cosmos DB. A RU represents a normalized measure of the database resources required to execute a request.
SQL Query Examples
The SQL API allows for powerful querying. Here are a few common examples:
Selecting Data
Retrieve all documents from a container:
SELECT * FROM c
Filtering Data
Select documents where a specific property meets a condition:
SELECT * FROM c WHERE c.category = "Electronics"
Selecting Specific Properties
Retrieve only the 'name' and 'price' of items:
SELECT c.name, c.price FROM c WHERE c.available = true
Ordering Results
Get the top 5 most expensive items:
SELECT TOP 5 * FROM c ORDER BY c.price DESC
Explore More Queries