|
1 | 1 | ---
|
2 |
| -title: 'Tutorial: How to query with SQL in Azure Cosmos DB?' |
3 |
| -description: 'Tutorial: Learn how to query with SQL queries in Azure Cosmos DB using the query playground' |
| 2 | +title: | |
| 3 | + Tutorial: Query data |
| 4 | +titleSuffix: Azure Cosmos DB for NoSQL |
| 5 | +description: In this tutorial, learn how to query data in Azure Cosmos DB for NoSQL with the built-in query syntax using the Data Explorer. |
4 | 6 | author: seesharprun
|
5 | 7 | ms.author: sidandrews
|
6 | 8 | ms.reviewer: mjbrown
|
7 | 9 | ms.service: cosmos-db
|
8 | 10 | ms.subservice: nosql
|
9 | 11 | ms.custom: tutorial-develop, mvc, ignite-2022
|
10 | 12 | ms.topic: tutorial
|
11 |
| -ms.date: 08/26/2021 |
| 13 | +ms.date: 03/16/2023 |
12 | 14 | ---
|
13 | 15 |
|
14 |
| -# Tutorial: Query Azure Cosmos DB by using the API for NoSQL |
| 16 | +# Tutorial: Query data in Azure Cosmos DB for NoSQL |
| 17 | + |
15 | 18 | [!INCLUDE[NoSQL](../includes/appliesto-nosql.md)]
|
16 | 19 |
|
17 |
| -The Azure Cosmos DB [API for NoSQL](../introduction.md) supports querying documents using SQL. This article provides a sample document and two sample SQL queries and results. |
| 20 | +[Azure Cosmos DB for NoSQL](../introduction.md) supports querying documents using the built-in query syntax. This article provides a sample document and two sample queries and results. |
18 | 21 |
|
19 |
| -This article covers the following tasks: |
| 22 | +This article covers the following tasks: |
20 | 23 |
|
21 | 24 | > [!div class="checklist"]
|
22 |
| -> * Querying data with SQL |
| 25 | +> |
| 26 | +> - Query NoSQL data with the built-in query syntax |
| 27 | +> |
| 28 | +
|
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +This tutorial assumes you have an Azure Cosmos DB account, database, and container. |
| 32 | + |
| 33 | +Don't have any of those resources? Complete this quickstart: [Create an Azure Cosmos DB account, database, container, and items from the Azure portal](quickstart-portal.md). |
| 34 | + |
| 35 | +You can run the queries using the [Azure Cosmos DB Explorer](../data-explorer.md) in the Azure portal. You can also run queries by using the [REST API](/rest/api/cosmos-db/) or [various SDKs](sdk-dotnet-v3.md). |
| 36 | + |
| 37 | +For more information about queries, see [setting started with queries](query/getting-started.md). |
23 | 38 |
|
24 | 39 | ## Sample document
|
25 | 40 |
|
26 |
| -The SQL queries in this article use the following sample document. |
| 41 | +The queries in this article use the following sample document. |
27 | 42 |
|
28 | 43 | ```json
|
29 | 44 | {
|
30 | 45 | "id": "WakefieldFamily",
|
31 | 46 | "parents": [
|
32 |
| - { "familyName": "Wakefield", "givenName": "Robin" }, |
33 |
| - { "familyName": "Miller", "givenName": "Ben" } |
| 47 | + { "familyName": "Wakefield", "givenName": "Robin" }, |
| 48 | + { "familyName": "Miller", "givenName": "Ben" } |
34 | 49 | ],
|
35 | 50 | "children": [
|
36 |
| - { |
37 |
| - "familyName": "Merriam", |
38 |
| - "givenName": "Jesse", |
39 |
| - "gender": "female", "grade": 1, |
40 |
| - "pets": [ |
41 |
| - { "givenName": "Goofy" }, |
42 |
| - { "givenName": "Shadow" } |
43 |
| - ] |
44 |
| - }, |
45 |
| - { |
46 |
| - "familyName": "Miller", |
47 |
| - "givenName": "Lisa", |
48 |
| - "gender": "female", |
49 |
| - "grade": 8 } |
| 51 | + { |
| 52 | + "familyName": "Merriam", |
| 53 | + "givenName": "Jesse", |
| 54 | + "gender": "female", "grade": 1, |
| 55 | + "pets": [ |
| 56 | + { "givenName": "Goofy" }, |
| 57 | + { "givenName": "Shadow" } |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "familyName": "Miller", |
| 62 | + "givenName": "Lisa", |
| 63 | + "gender": "female", |
| 64 | + "grade": 8 |
| 65 | + } |
50 | 66 | ],
|
51 | 67 | "address": { "state": "NY", "county": "Manhattan", "city": "NY" },
|
52 | 68 | "creationDate": 1431620462,
|
53 | 69 | "isRegistered": false
|
54 | 70 | }
|
55 | 71 | ```
|
56 | 72 |
|
57 |
| -## Where can I run SQL queries? |
58 |
| - |
59 |
| -You can run queries using the Data Explorer in the Azure portal and via the [REST API and SDKs](sdk-dotnet-v2.md). |
60 |
| - |
61 |
| -For more information about SQL queries, see: |
62 |
| -* [SQL query and SQL syntax](query/getting-started.md) |
63 |
| - |
64 |
| -## Prerequisites |
| 73 | +## Select all fields and apply a filter |
65 | 74 |
|
66 |
| -This tutorial assumes you have an Azure Cosmos DB account and collection. Don't have any of those resources? Complete the [5-minute quickstart](quickstart-portal.md). |
| 75 | +Given the sample family document, the following query returns the documents where the ID field matches `WakefieldFamily`. Since it's a `SELECT *` statement, the output of the query is the complete JSON document: |
67 | 76 |
|
68 |
| -## Example query 1 |
69 |
| - |
70 |
| -Given the sample family document above, following SQL query returns the documents where the ID field matches `WakefieldFamily`. Since it's a `SELECT *` statement, the output of the query is the complete JSON document: |
71 |
| - |
72 |
| -**Query** |
| 77 | +Query: |
73 | 78 |
|
74 | 79 | ```sql
|
75 |
| - SELECT * |
76 |
| - FROM Families f |
77 |
| - WHERE f.id = "WakefieldFamily" |
| 80 | +SELECT * |
| 81 | +FROM Families f |
| 82 | +WHERE f.id = "WakefieldFamily" |
78 | 83 | ```
|
79 | 84 |
|
80 |
| -**Results** |
| 85 | +Results: |
81 | 86 |
|
82 | 87 | ```json
|
83 | 88 | {
|
84 | 89 | "id": "WakefieldFamily",
|
85 | 90 | "parents": [
|
86 |
| - { "familyName": "Wakefield", "givenName": "Robin" }, |
87 |
| - { "familyName": "Miller", "givenName": "Ben" } |
| 91 | + { "familyName": "Wakefield", "givenName": "Robin" }, |
| 92 | + { "familyName": "Miller", "givenName": "Ben" } |
88 | 93 | ],
|
89 | 94 | "children": [
|
90 |
| - { |
91 |
| - "familyName": "Merriam", |
92 |
| - "givenName": "Jesse", |
93 |
| - "gender": "female", "grade": 1, |
94 |
| - "pets": [ |
95 |
| - { "givenName": "Goofy" }, |
96 |
| - { "givenName": "Shadow" } |
97 |
| - ] |
98 |
| - }, |
99 |
| - { |
100 |
| - "familyName": "Miller", |
101 |
| - "givenName": "Lisa", |
102 |
| - "gender": "female", |
103 |
| - "grade": 8 } |
| 95 | + { |
| 96 | + "familyName": "Merriam", |
| 97 | + "givenName": "Jesse", |
| 98 | + "gender": "female", "grade": 1, |
| 99 | + "pets": [ |
| 100 | + { "givenName": "Goofy" }, |
| 101 | + { "givenName": "Shadow" } |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "familyName": "Miller", |
| 106 | + "givenName": "Lisa", |
| 107 | + "gender": "female", |
| 108 | + "grade": 8 |
| 109 | + } |
104 | 110 | ],
|
105 | 111 | "address": { "state": "NY", "county": "Manhattan", "city": "NY" },
|
106 | 112 | "creationDate": 1431620462,
|
107 | 113 | "isRegistered": false
|
108 | 114 | }
|
109 | 115 | ```
|
110 | 116 |
|
111 |
| -## Example query 2 |
| 117 | +## Select a cross-product of a child collection field |
112 | 118 |
|
113 | 119 | The next query returns all the given names of children in the family whose ID matches `WakefieldFamily`.
|
114 | 120 |
|
115 |
| -**Query** |
| 121 | +Query: |
116 | 122 |
|
117 | 123 | ```sql
|
118 |
| - SELECT c.givenName |
119 |
| - FROM Families f |
120 |
| - JOIN c IN f.children |
121 |
| - WHERE f.id = 'WakefieldFamily' |
| 124 | +SELECT c.givenName |
| 125 | +FROM Families f |
| 126 | +JOIN c IN f.children |
| 127 | +WHERE f.id = 'WakefieldFamily' |
122 | 128 | ```
|
123 | 129 |
|
124 |
| -**Results** |
| 130 | +Results: |
125 | 131 |
|
126 |
| -``` |
| 132 | +```json |
127 | 133 | [
|
128 |
| - { |
129 |
| - "givenName": "Jesse" |
130 |
| - }, |
131 |
| - { |
132 |
| - "givenName": "Lisa" |
133 |
| - } |
| 134 | + { |
| 135 | + "givenName": "Jesse" |
| 136 | + }, |
| 137 | + { |
| 138 | + "givenName": "Lisa" |
| 139 | + } |
134 | 140 | ]
|
135 | 141 | ```
|
136 | 142 |
|
137 |
| - |
138 | 143 | ## Next steps
|
139 | 144 |
|
140 | 145 | In this tutorial, you've done the following tasks:
|
141 | 146 |
|
142 | 147 | > [!div class="checklist"]
|
143 |
| -> * Learned how to query using SQL |
| 148 | +> |
| 149 | +> - Learned how to query using the built-in query syntax |
| 150 | +> |
144 | 151 |
|
145 | 152 | You can now proceed to the next tutorial to learn how to distribute your data globally.
|
146 | 153 |
|
147 | 154 | > [!div class="nextstepaction"]
|
148 | 155 | > [Distribute your data globally](tutorial-global-distribution.md)
|
149 |
| -
|
150 |
| -Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning. |
151 |
| -* If all you know is the number of vcores and servers in your existing database cluster, read about [estimating request units using vCores or vCPUs](../convert-vcore-to-request-unit.md) |
152 |
| -* If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-with-capacity-planner.md) |
|
0 commit comments