cosmos-db
  1. cosmos-db-sql

SQL - CosmosDB APIs

Syntax

SQL is a widely used language for querying and manipulating databases. In CosmosDB, SQL is one of the available APIs that allows you to work with your data as if you were using a traditional relational database.

Example

SELECT *
FROM MyCollection
WHERE age > 18

Output

The SQL query will return all items in the MyCollection container with an age greater than 18.

Explanation

SQL is a declarative language, meaning that you specify what you want your query to do, and it will be executed by the system. In this example, we are selecting all items (*) from the MyCollection container that have an age greater than 18.

Use

SQL is a popular and widely used language, so if you are already familiar with it, using the CosmosDB SQL API can be an easy way to work with your data. Additionally, the SQL API is very powerful, allowing for complex queries and is optimized for querying large datasets.

Important Points

  • SQL in CosmosDB is not a fully featured SQL implementation, it's a subset designed specifically for working with JSON documents. So be aware of this when working with more complex queries.

  • SQL can be used with any partition key, but all queries must have a partition key defined.

  • Large json documents should be broken up into smaller items to prevent slow queries.

Summary

SQL is a powerful and widely used language for querying and manipulating databases. The CosmosDB API allows you to use SQL to work with your data in a familiar way, making it easier to get started and work with large datasets. However, it is important to be aware of the differences between working with relational databases and CosmosDB, and to understand the limitations and best practices when working with SQL in CosmosDB.

Published on: