---
title: NerdGraph tutorial: Query your data using NRQL
source: https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-nrql-tutorial
---

You can use our NerdGraph API to make [NRQL](https://docs.newrelic.com/docs/insights/nrql-new-relic-query-language/using-nrql/introduction-nrql) queries.

## Overview of querying with NerdGraph [#overview]

You can run NRQL queries using the [query builder](https://docs.newrelic.com/docs/query-your-data/explore-query-data/query-builder/introduction-query-builder) in the UI, or you can use our NerdGraph API. NerdGraph gives you the ability to do some things you can't do from the UI, such as [query across multiple accounts](#cross-account-query), and run [asynchronous queries](https://docs.newrelic.com/docs/apis/nerdgraph/examples/async-queries-nrql-tutorial).

## Requirements [#requirements]

-   All [user types](https://docs.newrelic.com/docs/accounts/accounts-billing/new-relic-one-user-management/user-type) can query data for the accounts they have access to. If you can't query via NerdGraph, it may be because you're lacking [access to that account](https://docs.newrelic.com/docs/accounts/accounts-billing/new-relic-one-user-management/user-management-concepts/#understand-concepts).
-   All queries are subject to [NRQL query limits](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/rate-limits-nrql-queries)

## Basic NRQL queries with NerdGraph [#basic-queries]

To make basic NRQL queries using NerdGraph, there are two main requirements:

-   You must pass the NRQL query as a string argument to the NRQL object
-   You must include the `results` field in your query

For example, to get a count of all [transaction events](https://docs.newrelic.com/docs/insights/insights-data-sources/default-data/apm-default-event-attributes-insights#transaction-event) in the last hour, you'd run the following query, specifying your [New Relic account ID](https://docs.newrelic.com/docs/accounts/accounts-billing/account-structure/account-id).

```graphql
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      nrql(query: "SELECT count(*) FROM Transaction SINCE 1 HOUR AGO") {
        results
      }
    }
  }
}
```

This query example would return a result like this:

```json
{
  "data": {
    "actor": {
      "account": {
        "nrql": {
          "results": [
            {
              "count": 1000
            }
          ]
        }
      }
    }
  }
}
```

Looking for details on how to write NRQL queries? See [the NRQL docs section](https://docs.newrelic.com/docs/insights/nrql-new-relic-query-language/using-nrql/introduction-nrql).

## Cross-account queries [#cross-account-query]

With NerdGraph, you can run a query across more than one account, which you can't do in the query builder. You'll need the [account IDs](https://docs.newrelic.com/docs/accounts/accounts-billing/account-structure/account-id) for the accounts you want to query.

Here's an example of running a cross-account NRQL query:

```graphql
{
  actor {
    nrql(
      accounts: [ACCOUNT_ID_1, ACCOUNT_ID_2, ACCOUNT_ID_3]
      options: {}
      query: "YOUR_NRQL_QUERY"
      timeout: 70
    ) {
      results
    }
  }
}
```

For how to create a dashboard with data from multiple accounts, see [the NerdGraph dashboard tutorial](https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-dashboards#cross-account).

## Create embeddable charts [#embeddable-charts]

In addition to returning raw data, you can fetch embeddable chart links for the data to use in an application. Please refer to our [public chart docs](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/share-charts-publicly).

## Suggested facets [#suggest-facets]

When using NerdGraph to explore your data, you can use the `suggestedFacets` field to return suggested attributes for use in [faceted NRQL queries](https://docs.newrelic.com/docs/insights/nrql-new-relic-query-language/nrql-resources/nrql-syntax-components-functions#sel-facet).

**Rules governing suggested facets**

Here are some of the rules that govern what attributes are suggested:

-   **Built-in suggestions.** Each [event type](https://docs.newrelic.com/docs/using-new-relic/welcome-new-relic/getting-started/glossary#event) comes with its own set of recommended attributes. These are attributes chosen by New Relic for their importance and popularity.
-   **Usage-based suggestions.** Some attribute suggestions are based on the queries that have been frequently used by your account. These suggestions can include custom attributes.
-   **Role restriction.** [Restricted users](https://docs.newrelic.com/docs/accounts/accounts/roles-permissions/users-roles) do not have access to account-related facet suggestions.

    To disable the use of account data for determining suggested queries, [contact Support](https://docs.newrelic.com/docs/using-new-relic/welcome-new-relic/getting-started/find-help-or-file-support-ticket#support_tickets).

**Example of returning suggested attributes**

Here's an example of returning suggested attributes for faceting [transaction](https://docs.newrelic.com/docs/insights/insights-data-sources/default-data/apm-default-event-attributes-insights#transaction-event) counts. The response suggests the `host` attribute. Faceting by `host` can reveal that one host is servicing more requests than other hosts.

````
{
  actor {
    account(id: YOUR_ACCOUNT_ID) {
      nrql(query: "<a href="/docs/insights/nrql-new-relic-query-language/nrql-resources/nrql-syntax-components-functions#state-select">SELECT</a> count(*) from <a href="https://docs.newrelic.com/docs/insights/insights-data-sources/default-data/apm-default-event-attributes-insights#transaction-event">Transaction</a> <a href="/docs/insights/nrql-new-relic-query-language/nrql-resources/nrql-syntax-components-functions#sel-timeseries">TIMESERIES</a>") {
        suggestedFacets {
          attributes
        }
      }
    }
  }
}
```

This NerdGraph query example returns a response similar to this:

```json
{
  "data": {
    "actor": {
      "account": {
        "nrql": {
          "suggestedFacets": [
            {
              "attributes": [
                "host"
              ]
            }
          ]
        }
      }
    }
  }
}

```

````

## Longer-running queries [#async]

If you need to run longer-running NRQL queries, see [Asynchronous queries](https://docs.newrelic.com/docs/apis/nerdgraph/examples/async-queries-nrql-tutorial).

## Other querying options [#other-options]

NerdGraph gives you other querying options, such as:

-   [Asynchronous queries](https://docs.newrelic.com/docs/apis/nerdgraph/examples/async-queries-nrql-tutorial)
-   [Historical data export](https://docs.newrelic.com/docs/apis/nerdgraph/examples/nerdgraph-historical-data-export)
-   [Create dashboards](https://docs.newrelic.com/docs/apis/nerdgraph/examples/create-widgets-dashboards-api)
