|
1 | 1 | --- |
2 | | -title: OFFSET LIMIT clause in Azure Cosmos DB |
3 | | -description: Learn how to use the OFFSET LIMIT clause to skip and take some certain values when querying in Azure Cosmos DB |
4 | | -author: seesharprun |
| 2 | +title: OFFSET LIMIT |
| 3 | +titleSuffix: Azure Cosmos DB for NoSQL |
| 4 | +description: An Azure Cosmos DB for NoSQL clause that skips and takes a specified number of results. |
| 5 | +author: jcodella |
| 6 | +ms.author: jacodel |
| 7 | +ms.reviewer: sidandrews |
5 | 8 | ms.service: cosmos-db |
6 | 9 | ms.subservice: nosql |
7 | | -ms.custom: ignite-2022 |
8 | | -ms.topic: conceptual |
9 | | -ms.date: 07/29/2020 |
10 | | -ms.author: sidandrews |
11 | | -ms.reviewer: jucocchi |
| 10 | +ms.topic: reference |
| 11 | +ms.date: 07/31/2023 |
| 12 | +ms.custom: query-reference |
12 | 13 | --- |
13 | | -# OFFSET LIMIT clause in Azure Cosmos DB |
| 14 | + |
| 15 | +# OFFSET LIMIT (NoSQL query) |
| 16 | + |
14 | 17 | [!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)] |
15 | 18 |
|
16 | | -The OFFSET LIMIT clause is an optional clause to skip then take some number of values from the query. The OFFSET count and the LIMIT count are required in the OFFSET LIMIT clause. |
| 19 | +The ``OFFSET LIMIT`` clause is an optional clause to **skip** and then **take** some number of values from the query. The ``OFFSET`` count and the ``LIMIT`` count are required in the OFFSET LIMIT clause. |
17 | 20 |
|
18 | | -When OFFSET LIMIT is used in conjunction with an ORDER BY clause, the result set is produced by doing skip and take on the ordered values. If no ORDER BY clause is used, it will result in a deterministic order of values. |
| 21 | +When ``OFFSET LIMIT`` is used with an ``ORDER BY`` clause, the result set is produced by doing skip and take on the ordered values. If no ``ORDER BY`` clause is used, it results in a deterministic order of values. |
19 | 22 |
|
20 | 23 | ## Syntax |
21 | | - |
| 24 | + |
22 | 25 | ```sql |
23 | 26 | OFFSET <offset_amount> LIMIT <limit_amount> |
24 | 27 | ``` |
25 | | - |
| 28 | + |
26 | 29 | ## Arguments |
27 | 30 |
|
28 | | -- `<offset_amount>` |
| 31 | +| | Description | |
| 32 | +| --- | --- | |
| 33 | +| **``<offset_amount>``** | Specifies the integer number of items that the query results should skip. | |
| 34 | +| **``<limit_amount>``** | Specifies the integer number of items that the query results should include. | |
| 35 | + |
| 36 | +## Examples |
29 | 37 |
|
30 | | - Specifies the integer number of items that the query results should skip. |
| 38 | +For the example in this section, this reference set of items is used. Each item includes a ``name`` property. |
31 | 39 |
|
32 | | -- `<limit_amount>` |
33 | | - |
34 | | - Specifies the integer number of items that the query results should include |
| 40 | +:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/offset-limit/seed.json" range="1-2,4,6-7,9,11-12,14,16-17,19,21-22,24,26-27" highlight="3,6,9,12,15"::: |
35 | 41 |
|
36 | | -## Remarks |
37 | | - |
38 | | - Both the `OFFSET` count and the `LIMIT` count are required in the `OFFSET LIMIT` clause. If an optional `ORDER BY` clause is used, the result set is produced by doing the skip over the ordered values. Otherwise, the query will return a fixed order of values. |
| 42 | +This example includes a query using the ``OFFSET LIMIT`` clause to return a subset of the matching items by skipping **one** item and taking the next **three**. |
39 | 43 |
|
40 | | - The RU charge of a query with `OFFSET LIMIT` will increase as the number of terms being offset increases. For queries that have [multiple pages of results](pagination.md), we typically recommend using [continuation tokens](pagination.md#continuation-tokens). Continuation tokens are a "bookmark" for the place where the query can later resume. If you use `OFFSET LIMIT`, there is no "bookmark". If you wanted to return the query's next page, you would have to start from the beginning. |
41 | | - |
42 | | - You should use `OFFSET LIMIT` for cases when you would like to skip items entirely and save client resources. For example, you should use `OFFSET LIMIT` if you want to skip to the 1000th query result and have no need to view results 1 through 999. On the backend, `OFFSET LIMIT` still loads each item, including those that are skipped. The performance advantage is a savings in client resources by avoiding processing items that are not needed. |
| 44 | +:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/offset-limit/query.sql" range="1-5,8-10" highlight="8"::: |
43 | 45 |
|
44 | | -## Examples |
| 46 | +:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/offset-limit/result.json"::: |
| 47 | + |
| 48 | +## Remarks |
45 | 49 |
|
46 | | -For example, here's a query that skips the first value and returns the second value (in order of the resident city's name): |
47 | | - |
48 | | -```sql |
49 | | - SELECT f.id, f.address.city |
50 | | - FROM Families f |
51 | | - ORDER BY f.address.city |
52 | | - OFFSET 1 LIMIT 1 |
53 | | -``` |
54 | | - |
55 | | -The results are: |
56 | | - |
57 | | -```json |
58 | | - [ |
59 | | - { |
60 | | - "id": "AndersenFamily", |
61 | | - "city": "Seattle" |
62 | | - } |
63 | | - ] |
64 | | -``` |
65 | | - |
66 | | -Here's a query that skips the first value and returns the second value (without ordering): |
67 | | - |
68 | | -```sql |
69 | | - SELECT f.id, f.address.city |
70 | | - FROM Families f |
71 | | - OFFSET 1 LIMIT 1 |
72 | | -``` |
73 | | - |
74 | | -The results are: |
75 | | - |
76 | | -```json |
77 | | - [ |
78 | | - { |
79 | | - "id": "WakefieldFamily", |
80 | | - "city": "Seattle" |
81 | | - } |
82 | | - ] |
83 | | -``` |
| 50 | +- Both the ``OFFSET`` count and the ``LIMIT`` count are required in the ``OFFSET LIMIT`` clause. If an optional ``ORDER BY`` clause is used, the result set is produced by doing the skip over the ordered values. Otherwise, the query returns a fixed order of values. |
| 51 | +- The RU charge of a query with ``OFFSET LIMIT`` increases as the number of terms being offset increases. For queries that have [multiple pages of results](pagination.md), we typically recommend using [continuation tokens](pagination.md#continuation-tokens). Continuation tokens are a "bookmark" for the place where the query can later resume. If you use ``OFFSET LIMIT``, there's no "bookmark." If you wanted to return the query's next page, you would have to start from the beginning. |
| 52 | +- You should use ``OFFSET LIMIT`` for cases when you would like to skip items entirely and save client resources. For example, you should use ``OFFSET LIMIT`` if you want to skip to the 1000th query result and have no need to view results 1 through 999. On the backend, ``OFFSET LIMIT`` still loads each item, including those items that are skipped. The performance advantage is measured in reducing client resources by avoiding processing items that aren't needed. |
84 | 53 |
|
85 | 54 | ## Next steps |
86 | 55 |
|
87 | | -- [Getting started](getting-started.md) |
88 | | -- [SELECT clause](select.md) |
89 | | -- [ORDER BY clause](order-by.md) |
| 56 | +- [``GROUP BY`` clause](group-by.md) |
| 57 | +- [``ORDER BY`` clause](order-by.md) |
0 commit comments