---
schema_version: "1"
id: 4dfb48faaf38607bfff4adc8790104b641d4f65c7386ef32ceada8c56d887c3c
title: data query
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/sfclireference/guides/data/cli_reference_data_query.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-cli-reference/guide/cli_reference_data_query.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: aa8897acd6681b1822357d425567780e390d7715dcaeb928ad70e7347c8b06f0
products:
  - Salesforce CLI Command Reference
  - salesforcedx
---
<!-- prettier-ignore-start -->

# data query

Execute a SOQL query.

## Description for data query

Specify the SOQL query at the command line with the --query flag or read the query from a file with the --file flag.

If your query returns more than 10,000 records, prefer to use the `sf data export bulk` command instead. It runs the query using Bulk API 2.0, which has higher limits than the default API used by the command.

## Aliases for data query

`force:data:soql:query`

## Flags

| Flag Name (Long) | Flag Name (Short) | Description |
|------------------|-------------------|-------------|
| <code>&#8209;&#8209;all&#8209;rows</code> | N/A | **Type:** Boolean<br><br>Include deleted records. By default, deleted records are not returned. |
| <code>&#8209;&#8209;api&#8209;version</code> | N/A | **Type:** Value<br><br>Override the api version used for api requests made by this command |
| <code>&#8209;&#8209;file</code> | <code>&#8209;f</code> | **Type:** Value<br><br>File that contains the SOQL query. |
| <code>&#8209;&#8209;flags&#8209;dir</code> | N/A | **Type:** Value<br><br>Import flag values from a directory. |
| <code>&#8209;&#8209;json</code> | N/A | **Type:** Boolean<br><br>Format output as json. |
| <code>&#8209;&#8209;output&#8209;file</code> | N/A | **Type:** Value<br><br>File where records are written; only CSV and JSON output formats are supported. |
| <code>&#8209;&#8209;query</code> | <code>&#8209;q</code> | **Type:** Value<br><br>SOQL query to execute. |
| <code>&#8209;&#8209;result&#8209;format</code> | <code>&#8209;r</code> | **Type:** Value<br>**Valid Values:** `human`, `csv`, `json`<br>**Default value:** `human`<br><br>Format to display the results; the --json flag overrides this flag. |
| <code>&#8209;&#8209;target&#8209;org</code> | <code>&#8209;o</code> | **Type:** Value<br>**Required**<br><br>Username or alias of the target org. Not required if the `target-org` configuration variable is already set. |
| <code>&#8209;&#8209;use&#8209;tooling&#8209;api</code> | <code>&#8209;t</code> | **Type:** Boolean<br><br>Use Tooling API so you can run queries on Tooling API objects. |

## Examples for data query

Specify a SOQL query at the command line; the command uses your default org:

```shell
sf data query --query "SELECT Id, Name, Account.Name FROM Contact"
```

Read the SOQL query from a file called "query.txt" and write the CSV-formatted output to a file; the command uses the org with alias "my-scratch":

```shell
sf data query --file query.txt --output-file output.csv --result-format csv --target-org my-scratch
```

Use Tooling API to run a query on the ApexTrigger Tooling API object:

```shell
sf data query --query "SELECT Name FROM ApexTrigger" --use-tooling-api
```
<!-- prettier-ignore-end -->
