Skip to content

Commit 970896b

Browse files
committed
Add OFFSET LIMIT
1 parent bb97972 commit 970896b

File tree

1 file changed

+34
-66
lines changed

1 file changed

+34
-66
lines changed
Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,57 @@
11
---
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
58
ms.service: cosmos-db
69
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
1213
---
13-
# OFFSET LIMIT clause in Azure Cosmos DB
14+
15+
# OFFSET LIMIT (NoSQL query)
16+
1417
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1518

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.
1720

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.
1922

2023
## Syntax
21-
24+
2225
```sql
2326
OFFSET <offset_amount> LIMIT <limit_amount>
2427
```
25-
28+
2629
## Arguments
2730

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
2937

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.
3139

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":::
3541

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**.
3943

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":::
4345

44-
## Examples
46+
:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/offset-limit/result.json":::
47+
48+
## Remarks
4549

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.
8453

8554
## Next steps
8655

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

Comments
 (0)