**Source URL:** https://general.veevavault.dev/vql/functions-options/allversions-latestversion.md

# ALLVERSIONS & LATESTVERSION



By default, a query on the documents object targets the latest version of all documents. In v8.0+, you can query by document version:

* Use `ALLVERSIONS` with `FROM` to query all document versions.

* Use `LATESTVERSION` with `SELECT` and `ALLVERSIONS` to retrieve the latest version that matches the `FIND` or `WHERE` search criteria. `LATESTVERSION` on its own uses the default behavior.

## Syntax {#Syntax}

```
SELECT LATESTVERSION {fields}
FROM ALLVERSIONS documents

```

## Query Examples {#Query_Examples}

The following are examples of queries using `ALLVERSIONS` and `LATESTVERSION`.

### Query: ALLVERSIONS {#Query_ALLVERSIONS}

The following query returns the ID and major and minor version numbers of all document versions:

```
SELECT id, major_version_number__v, minor_version_number__v
FROM ALLVERSIONS documents

```

### Response: ALLVERSIONS {#Response_ALLVERSIONS}

```
{
   "responseStatus": "SUCCESS",
   "responseDetails": {
       "pagesize": 1000,
       "pageoffset": 0,
       "size": 3,
       "total": 3
   },
   "data": [
       {
           "id": 6,
           "major_version_number__v": 1,
           "minor_version_number__v": 1
       },
       {
           "id": 6,
           "major_version_number__v": 1,
           "minor_version_number__v": 0
       },
       {
           "id": 5,
           "major_version_number__v": 0,
           "minor_version_number__v": 1
       }
   ]
}

```

### Query: LATESTVERSION {#Query_LATESTVERSION}

In this example, a user has assigned a value to the *Country* field in a document with version 1.1. The following query returns the latest version of this document where the *Country* field is empty:

```
SELECT LATESTVERSION id, major_version_number__v, minor_version_number__v
FROM ALLVERSIONS documents
WHERE id = 6 AND country__c = NULL

```

### Response: LATESTVERSION {#Response_LATESTVERSION}

```
{
   "responseStatus": "SUCCESS",
   "responseDetails": {
       "pagesize": 1000,
       "pageoffset": 0,
       "size": 1,
       "total": 1
   },
   "data": [
       {
           "id": 6,
           "major_version_number__v": 1,
           "minor_version_number__v": 0
       }
   ]
}

```


---

**Previous:** [Functions & Options](/vql/functions-options)  
**Next:** [Attachment Field Functions](/vql/functions-options/attachment-field-functions)