---
title: How to query with NRQL: the mechanics of querying
source: https://docs.newrelic.com/docs/nrql/get-started/introduction-nrql-how-nrql-works
---

Before using any tool, it's best to know how to use it. There's a process to creating, structuring, and writing queries with NRQL. Understanding the rules of querying with NRQL enables you to get the most from your data. Even if you've never queried anything before, just a basic understanding the rules allows you to access (almost) any data you need and visualize them in [charts](https://docs.newrelic.com/docs/query-your-data/explore-query-data/use-charts/use-your-charts) and [dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards).

## Data exploration [#explore-data]

One of the best ways to learn how to use NRQL is to access a New Relic [query tool](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/introduction-nrql-new-relics-query-language#where) and use it to query your data. Here's an example of exploring your data using the [query builder](https://docs.newrelic.com/docs/chart-builder/use-chart-builder/get-started/introduction-chart-builder) and the suggested entries from the interface.

> #### 💡 TIP
>
> Don't be afraid to play with your data! You won't break anything using any of our query interfaces, so feel free to tinker as much as you like!

The query begins with `FROM` followed by a space. The interface suggests available types of data, and you select `Transaction` from that list.

![A screenshot of a NRQL query beginning with From](https://docs.newrelic.com/images/queries-nrql_screenshot-crop_event-definitions-query-builder.webp "nrql_query01.png")

Next, choose an [attribute](https://docs.newrelic.com/docs/using-new-relic/welcome-new-relic/get-started/glossary#attribute) using `SELECT`, and the query looks as follows:

```sql
FROM Transaction SELECT
```

Pressing space again causes the interface to suggest available attributes. In the example below, you choose `appId`.

![A screenshot of a NRQL query using the previous screenshot and adding Select](https://docs.newrelic.com/images/queries-nrql_screenshot-crop_attribute-definitions-query-builder.webp "nrql_query02.png")

This results in a very basic NRQL query using the required clause and statement (`FROM` and `SELECT`), and it provides a list of APM transactions and the associated `appId` for each, as shown below.

![A screenshot of a basic NRQL query, From Transaction Select appId](https://docs.newrelic.com/images/queries-nrql_screenshot-crop_basic-nrql-query-example.webp "basic_nrql_example.png")

## NRQL query examples [#examples]

Here's an example of a slightly more in-depth NRQL query of `Transaction` data reported by [APM](https://docs.newrelic.com/docs/apm). For this query:

-   You choose `Transaction` as the data type.
-   You use `Select` to determine the average duration.
-   You group results by appName using `Facet`.
-   You use `Timeseries` to display the data over an automated timespan.

```sql
FROM Transaction 
SELECT average(duration) 
  FACET appName 
  TIMESERIES auto
```

This generates a chart that looks like:

![nrql_example.png](https://docs.newrelic.com/images/queries-nrql_screenshot-crop_nrql-example-timeseries.webp "nrql_example.png")

Here are some more query examples:

**Basic NRQL query of browser data**

Here's a NRQL query of `PageView` data from [browser monitoring](https://docs.newrelic.com/docs/browser).

````sql
SELECT uniqueCount(user) FROM PageView 
  WHERE userAgentOS = 'Mac' 
  FACET countryCode
  SINCE 1 day ago 
  LIMIT 20
```

````

**Attribute name with a space in it**

If a [custom attribute](https://docs.newrelic.com/docs/insights/new-relic-insights/decorating-events/insights-custom-attributes) name has a space in it, use backticks around the attribute name:

````sql
SELECT count(*)
 FROM Transaction
 FACET `Logged-in user`
```

````

**Querying multiple data sources**

To return data from two data sources, separate their data types with a comma. For example, this query returns a count of all [APM transactions](https://docs.newrelic.com/docs/insights/new-relic-insights/decorating-events/insights-attributes#transaction-defaults) and [browser events](https://docs.newrelic.com/docs/insights/new-relic-insights/decorating-events/insights-attributes#browser-defaults) over the last three days:

````sql
SELECT count(*) FROM Transaction, PageView SINCE 3 days ago
```

````

**Query returning multiple columns**

To return multiple columns from a dataset, separate the aggregator arguments with a comma:

````sql
SELECT function(attribute), function(attribute) ...
  FROM ...
```

This query returns the minimum, average, and maximum duration for browser monitoring `PageView` events over the last week:

```sql
SELECT min(duration), max(duration), average(duration)
  FROM PageView SINCE 1 week ago
```

````

> #### ⚠️ IMPORTANT
>
> To explore your data without having to use NRQL, use the [metrics and events data explorer](https://docs.newrelic.com/docs/query-your-data/explore-query-data/browse-data/introduction-data-explorer/). Learn more about [querying data in New Relic](https://docs.newrelic.com/docs/query-your-data/explore-query-data/get-started/introduction-querying-new-relic-data/).

Ready to learn more? Be sure to check out our [introduction to NRQL](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/introduction-nrql-new-relics-query-language/) if you haven't already, or learn [how to use charts and dashboards with NRQL](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/charts-and-dashboards-with-nrql/). If you want to start using NRQL right away, jump straight into our [guided NRQL tutorial](https://docs.newrelic.com/docs/query-your-data/nrql-new-relic-query-language/get-started/introduction-nrql-tutorial/).
