# Query table rows (https://guides.dataportal.gov.lt/docs/api/guides/querying)



Send a JSON request to the table’s `query` route with the rows and columns you need. The response contains rows and pagination details.

<MethodPath method="POST" path="/v1/data/organizations/{id}/tables/{table}/query" />

```json
{ "data": [ /* rows */ ], "total": 1860, "total_pages": 372, "current_page": 1 }
```

Read `total` for the count of all matching rows, including those on other pages. The examples use organisation `188600177` and the English population table `S3R167_M3010203_en` from the [quickstart](https://guides.dataportal.gov.lt/docs/api/guides/overview).

## Request body [#request-body]

All fields are optional. Send the empty JSON object `{}` for the first 100 rows with every column.

<ParamTable
  lang="en"
  params="[
  { name: &#x22;select&#x22;, type: &#x22;string&#x22;, description: &#x22;Comma-separated column names. Omit for all columns.&#x22; },
  { name: &#x22;filter&#x22;, type: &#x22;string&#x22;, description: &#x22;Which rows to return (grammar below).&#x22; },
  { name: &#x22;sort&#x22;, type: &#x22;string&#x22;, description: &#x22;Comma-separated column names; prefix - for descending.&#x22; },
  { name: &#x22;limit&#x22;, type: &#x22;int&#x22;, default: &#x22;100&#x22;, description: &#x22;Rows to return, from 1 to 100,000. null, 0 or a value above 100,000 returns 400.&#x22; },
  { name: &#x22;offset&#x22;, type: &#x22;int&#x22;, default: &#x22;0&#x22;, description: &#x22;Rows to skip, from 0. A negative value returns 400.&#x22; },
  { name: &#x22;exclude_nulls&#x22;, type: &#x22;string[]&#x22;, description: &#x22;Columns that must have a value; rows with an empty one are left out.&#x22; },
]"
/>

Use column names exactly as they appear in the [distribution schema](https://guides.dataportal.gov.lt/docs/api/guides/searching#column-values) or in `GET /v1/data/organizations/{id}/tables/{table}/schema`.

## Select columns [#select-columns]

```bash
curl -X POST 'https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/query' \
  -H 'Content-Type: application/json' \
  -d '{"select":"administrative_territory,sex,time_period,s3r167","limit":3}'
```

## Filter [#filter]

Use a single `=` for equality, as in `column='value'`, or `column._op=value` with an operator from the table. Join conditions with `&`. Put text values in single quotes, except for the cases described below.

```bash
curl -X POST 'https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/query' \
  -H 'Content-Type: application/json' \
  -d "{\"filter\":\"administrative_territory='Panevėžys c. mun.'&time_period='2026'\",\"limit\":5}"
```

Equality takes one `=`. A double `==`, or spaces around the sign, is an error: the API returns `400` with the code `INVALID_TABLE_QUERY`.

```text
administrative_territory='Panevėžys c. mun.'      ✓ selects 1,860 rows
administrative_territory == 'Panevėžys c. mun.'   ✗ 400 INVALID_TABLE_QUERY
```

<Callout type="warn" title="Quote text, especially when it looks like a number">
  Check the column type even when the value contains only digits. For a text period use `time_period='2026'`. Without quotes, the value is read as a number and the API returns `400` with the code `TRINO_USER_ERROR`.
</Callout>

### Operators [#operators]

<ParamTable
  columns="[&#x22;Form&#x22;, &#x22;Meaning&#x22;, &#x22;Example or note&#x22;]"
  lang="en"
  params="[
  { name: &#x22;col='v'&#x22;, type: &#x22;equals&#x22;, description: &#x22;sex='Males' — 45,880 rows.&#x22; },
  { name: &#x22;col._gt=v&#x22;, type: &#x22;greater than&#x22;, description: &#x22;s3r167._gt=40000&#x22; },
  { name: &#x22;col._ge=v&#x22;, type: &#x22;at least&#x22;, description: &#x22;Two letters: ._ge, not ._gte.&#x22; },
  { name: &#x22;col._lt=v&#x22;, type: &#x22;less than&#x22;, description: &#x22;&#x22; },
  { name: &#x22;col._le=v&#x22;, type: &#x22;at most&#x22;, description: &#x22;Two letters: ._le, not ._lte.&#x22; },
  { name: &#x22;col._co=v&#x22;, type: &#x22;contains&#x22;, description: &#x22;administrative_territory._co=Panev — no quotes; case-sensitive.&#x22; },
  { name: &#x22;col._sw=v&#x22;, type: &#x22;starts with&#x22;, description: &#x22;No quotes; case-sensitive.&#x22; },
  { name: &#x22;col._ew=v&#x22;, type: &#x22;ends with&#x22;, description: &#x22;No quotes; case-sensitive.&#x22; },
]"
/>

Quoted text is compared in lexical order. For example, `time_period._ge='2020'` selects four-digit years from 2020 onwards.

<Callout type="warn" title="Two exceptions to the quoting rule">
  * **`_co`, `_sw` and `_ew` take the value bare.** Quotes there are part of the text
    searched for: `._co='Panev'` finds nothing, `._co=Panev` finds 5,580 rows.
  * **Only these eight forms exist.** There is no "not equal", "in a list" or "is
    empty" operator; `._ne`, `._in`, `._gte` and the like return `400` with the code
    `INVALID_TABLE_QUERY`. Use
    `exclude_nulls` to drop empty values.
</Callout>

### Combining conditions [#combining-conditions]

By default a row must satisfy every condition joined with `&`. To allow an alternative to the previous condition, prefix the next condition with `_or.`.

```bash
# Panevėžys city AND males — 620 rows
-d "{\"filter\":\"administrative_territory='Panevėžys c. mun.'&sex='Males'\"}"

# males OR females — 91,760 rows
-d "{\"filter\":\"sex='Males'&_or.sex='Females'\"}"
```

Use `_or` for alternative values in one column. Check `total` in the response to see how many rows matched.

## Sort [#sort]

List sort columns in order of priority in `sort`. Prefix a column with a minus sign to sort it in descending order.

```bash
-d "{\"filter\":\"administrative_territory='Panevėžys c. mun.'\",\"sort\":\"-time_period,age_5_year_groups\",\"limit\":3}"
```

## Paging [#paging]

Omit `limit` to get the first 100 rows. Increase it to at most 100,000, or use `offset` to skip rows and retrieve the next page.

<Steps>
  <Step>
    ### First page [#first-page]

    `{"limit": 1000, "offset": 0, "sort": "row_id"}`
  </Step>

  <Step>
    ### Next pages [#next-pages]

    For each new page, increase `offset` by `limit`. With pages of 1,000 rows, use offsets of 0, 1,000, 2,000 and so on.
  </Step>

  <Step>
    ### Stop [#stop]

    Stop when a page contains fewer rows than `limit`.
  </Step>
</Steps>

<CodeTabs labels="[&#x22;Python&#x22;, &#x22;JavaScript&#x22;, &#x22;cURL&#x22;]">
  ```python
  import requests

  URL = "https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/query"
  query = {"filter": "administrative_territory='Panevėžys c. mun.'", "sort": "row_id", "limit": 1000}

  rows, offset = [], 0
  while True:
      page = requests.post(URL, json={**query, "offset": offset}).json()["data"]
      rows += page
      if len(page) < query["limit"]:
          break
      offset += query["limit"]

  print(len(rows), "rows")  # 1860 rows
  ```

  ```js
  const URL = "https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/query";
  const query = { filter: "administrative_territory='Panevėžys c. mun.'", sort: "row_id", limit: 1000 };

  const rows = [];
  for (let offset = 0; ; offset += query.limit) {
    const { data } = await fetch(URL, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ ...query, offset }),
    }).then((r) => r.json());
    rows.push(...data);
    if (data.length < query.limit) break;
  }
  console.log(rows.length, "rows"); // 1860 rows
  ```

  ```bash
  URL='https://api.dataportal.gov.lt/v1/data/organizations/188600177/tables/S3R167_M3010203_en/query'
  FILTER="administrative_territory='Panevėžys c. mun.'"
  LIMIT=1000; OFFSET=0
  while :; do
    BODY=$(jq -nc --arg f "$FILTER" --argjson l $LIMIT --argjson o $OFFSET '{filter:$f, sort:"row_id", limit:$l, offset:$o}')
    COUNT=$(curl -s -X POST "$URL" -H 'Content-Type: application/json' -d "$BODY" | tee "page-$OFFSET.json" | jq '.data | length')
    [ "$COUNT" -lt "$LIMIT" ] && break
    OFFSET=$((OFFSET + LIMIT))
  done
  ```
</CodeTabs>

<Callout type="warn" title="Sort when you page">
  Use the same sort order for every page and include a unique column such as `row_id` in statistical tables. Otherwise pages can repeat or skip rows.
</Callout>

<span id="everything-in-one-response" className="ldp-anchor" />

<span id="return-all-matching-rows" className="ldp-anchor" />

### All matching rows in one response [#all-matching-rows-in-one-response]

One response holds up to 100,000 rows. Set `limit` to at least the number of matching rows. The Panevėžys city selection has 1,860 rows, so `"limit": 2000` covers it.

```bash
-d "{\"filter\":\"administrative_territory='Panevėžys c. mun.'\",\"limit\":2000}"
```

When more than 100,000 rows match, page through them with a stable sort. The whole table, for example, has 137,640 rows:

```bash
# rows 1 to 100,000
-d '{"sort":"row_id","limit":100000,"offset":0}'
# the remaining 37,640 rows
-d '{"sort":"row_id","limit":100000,"offset":100000}'
```

For a file, [export the rows](https://guides.dataportal.gov.lt/docs/api/guides/downloading) instead. An export has no row cap: it returns all 137,640 rows in one file.

## Drop empty values [#drop-empty-values]

Statistical tables may include rows with no figure for a given breakdown. Use `exclude_nulls` to keep only rows with a value. In the Panevėžys city example, 1,487 of 1,860 rows contain a figure.

```bash
-d "{\"filter\":\"administrative_territory='Panevėžys c. mun.'\",\"exclude_nulls\":[\"s3r167\"]}"
```

Keep `conventional_symbol` if you need to explain missing values. It may indicate, for example, that data were not collected or that the figure is confidential.

## Try it [#try-it]

<ApiExplorer
  lang="en"
  organization="188600177"
  table="S3R167_M3010203_en"
  defaultSelected="[&#x22;administrative_territory&#x22;, &#x22;age_5_year_groups&#x22;, &#x22;sex&#x22;, &#x22;time_period&#x22;, &#x22;s3r167&#x22;]"
  defaultLimit="10"
  defaultSort="-time_period"
  defaultFilterColumn="sex"
  defaultFilterValue="Males and females"
  columns="[
  { name: &#x22;row_id&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;age_5_year_groups&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;sex&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;administrative_territory&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;administrative_territory_code&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;time_period&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;s3r167&#x22;, type: &#x22;double&#x22; },
  { name: &#x22;unit_of_measurement&#x22;, type: &#x22;string&#x22; },
  { name: &#x22;conventional_symbol&#x22;, type: &#x22;string&#x22; },
]"
/>

## See also [#see-also]

* [Downloading data](https://guides.dataportal.gov.lt/docs/api/guides/downloading)
* [Errors](https://guides.dataportal.gov.lt/docs/api/guides/errors)
* [Recipes](https://guides.dataportal.gov.lt/docs/api/guides/recipes)
* [Query data table rows](https://guides.dataportal.gov.lt/docs/api/data/query-table) (reference)
