---
title: Query data with Lens
source: https://docs.newrelic.com/docs/new-relic-lens/query-data
---

You can write Lens queries in **Query Builder**, **Notebooks**, and **custom dashboards**. This page explains how to query data in each platform.

> #### 💡 TIP
>
> When querying data sources, Lens doesn't restrict the number of rows you can retrieve from external sources. However, queries to NRDB through Lens maintain the same 5,000 row limit as NRQL.

## Prerequisites [#prerequisites]

Before you start:

-   Verify you have the necessary [permissions](https://docs.newrelic.com/docs/new-relic-lens/set-up-connectors#permissions) to query data sources.
-   If you need to query external data sources, ensure the [connectors](https://docs.newrelic.com/docs/new-relic-lens/set-up-connectors) are set up.

## Query in the query builder [#query-builder]

1.  Go to **[one.newrelic.com](https://one.newrelic.com)**.
2.  From the drawer at the bottom of the page, open the **Query your data** console.
3.  In the query editor, select **Lens data (SQL)** as the query language.
4.  If you want to query NRDB data, select the account from the account selector. Other data connectors are available at the organization level and don't require account selection.
5.  Write your SQL query using the [table reference format](#table-format): `connection_name.schema.table`
6.  Click **Run**. The chart renders based on your query results.

## Save query in Notebooks [#notebooks]

[Notebooks](https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/introduction-notebooks/) are shareable documents that combine queries, visualizations, and markdown documentation.

1.  Go to **[one.newrelic.com](https://one.newrelic.com)**.
2.  From the drawer at the bottom of the page, open the **Query your data** console.
3.  Enable the toggle beside **Notebooks**. At the bottom of the page, the **+ SQL** button appears.
4.  Click **+ SQL** to add a SQL block.
5.  If you want to query NRDB data, select the account from the account selector. Other data connectors are available at the organization level and don't require account selection.
6.  Write your SQL query using the [table reference format](#table-format): `connection_name.schema.table`
7.  Click **Run**. The chart renders based on your query results.
8.  Click **Save**.

For further guidance, refer to the [Notebooks documentation](https://docs.newrelic.com/docs/query-your-data/explore-query-data/notebooks/introduction-notebooks/).

## Query in custom dashboards [#dashboards]

You can add SQL-based charts to your [custom dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards):

1.  Go to **[one.newrelic.com](https://one.newrelic.com) > Dashboards**.
2.  [Create a new dashboard](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards#dashboards-create-new) or open an existing one.
3.  Click **+ Add widget** and select a chart type.
4.  In the query editor, select **Lens data (SQL)** as the query language.
5.  If you want to query NRDB data, select the account from the account selector. Other data connectors are available at the organization level and don't require account selection.
6.  Write your SQL query using the [table reference format](#table-format): `connection_name.schema.table`
7.  Click **Run**. The chart renders based on your query results.
8.  Click **Add to dashboard**. For further guidance, refer to the [custom dashboards documentation](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards/).
    SQL-based charts are only supported in private dashboards. Public dashboards don't support SQL queries.

## SQL syntax [#syntax]

Lens uses ANSI SQL to query data sources, so you don't need to learn database-specific query languages.

### Table reference format [#table-format]

When writing queries in Lens, reference tables using the format:

```
connection_name.schema.table
```

For example:

-   `telemetry.Transaction` - Query the Transaction table from the telemetry (NRDB) connector
-   `snowflake.sales.orders` - Query the orders table from a Snowflake connector named "snowflake" in the "sales" schema
-   `gsheets.default.employees` - Query a Google Sheets connector named "gsheets"

### Supported SQL statements [#supported-sql]

Lens supports standard ANSI SQL statements including:

-   `SELECT` - Query data from one or more tables
-   `JOIN` - Combine data from multiple tables (including cross-database joins)
-   `WHERE` - Filter results
-   `GROUP BY` - Aggregate data
-   `ORDER BY` - Sort results
-   `LIMIT` - Restrict the number of returned rows
-   `WITH` (CTEs) - Define common table expressions for complex queries

### Performance tips [#performance-tips]

-   **Use filters early**: Apply `WHERE` clauses to reduce the amount of data processed.
-   **Limit results**: Use `LIMIT` to restrict returned rows when exploring data.
-   **Consider materialized views**: For frequently-run complex queries, create a materialized view.
-   **Optimize joins**: When joining large tables, filter data before the join when possible.

## Query syntax differences from NRQL and examples [#syntax-differences]

Lens uses ANSI SQL instead of NRQL. The following examples show key differences and common query patterns.

**Specify \`SELECT\` before \`FROM\`**

Specify `SELECT` clause before the `FROM` clause. NRQL allows use of `FROM` to start a NRQL statement but that is incorrect syntax for ANSI SQL supported by Lens.
**NRQL:**

````sql
FROM PageViewTiming
SELECT count(*)
```

**SQL:**

```sql
SELECT count(*)
FROM telemetry.PageViewTiming
```

````

**Use \`GROUP BY\` instead of \`FACET\`**

When grouping results, use `GROUP BY` and include the grouped column in your `SELECT` statement.

**NRQL:**

````sql
FROM PageViewTiming
SELECT count(*)
FACET enduser.id
```

**SQL:**

```sql
SELECT "enduser.id", count(*)
FROM telemetry.PageViewTiming
GROUP BY "enduser.id"
```

````

**Use \`WHERE\` with \`timestamp\` instead of \`SINCE\`**

Filter time ranges using `WHERE` with the `timestamp` column and date intervals.

**NRQL:**

````sql
FROM PageViewTiming
SELECT count(*)
SINCE 1 day ago
```

**SQL:**

```sql
SELECT count(*)
FROM telemetry.PageViewTiming
WHERE timestamp > current_date - INTERVAL '1' DAY
```

````

**Time series with epoch format**

To create time series visualizations, structure your query so the first column contains dates in epoch format. Use `to_unixtime()` to convert dates.

````sql
WITH orders AS (
  SELECT
    to_unixtime(date(date_parse(order_date, '%Y-%m-%d'))) AS dt,
    count(*) AS orders
  FROM gsheets.default.orders
  GROUP BY 1
)
SELECT *
FROM orders
WHERE dt >= to_unixtime(date('2025-10-19'))
  AND dt <= to_unixtime(date('2025-10-21'))
ORDER BY dt ASC
```

````

**Group and order data for a line chart**

Unlike NRQL, which returns pre-bucketed time series data, Lens returns query results as a flat table. A query can return the expected rows in table view and still render as a broken line or area chart if the data isn't bucketed and ordered for a timeline.

Group your timestamp into day-level buckets using `date_trunc()`, and order the results in ascending order by that same bucket expression. If the dataset isn't ordered by the time bucket, the chart connects points out of sequence.

````sql
SELECT date_trunc('day', timestamp) AS day, COUNT(*)
FROM telemetry.QueryExecution
WHERE ("tags.environment" = 'production' OR "tags.environment" = 'eu-production')
  AND timestamp > current_date - INTERVAL '7' DAY
  AND userId > 0
GROUP BY date_trunc('day', timestamp)
ORDER BY date_trunc('day', timestamp) ASC
```

````

**Cross-database \`JOIN\`**

Use standard SQL `JOIN` syntax to combine data from multiple sources in a single query.

````sql
WITH sf AS (
  SELECT DISTINCT user_id, assigned_success_manager
  FROM demogorgon.default.relibank_opportunity_matrix
)
SELECT
  sf.assigned_success_manager,
  MAX(nrdb.largestContentfulPaint)
FROM telemetry.PageViewTiming nrdb
LEFT JOIN sf ON nrdb."enduser.id" = sf.user_id
WHERE timingName = 'largestContentfulPaint'
  AND nrdb.timestamp > current_date - INTERVAL '1' DAY
GROUP BY sf.assigned_success_manager
```

````

**Query materialized views**

Reference materialized views using the `mv.` prefix:

````sql
SELECT *
FROM mv.weekly_performance_summary
WHERE week_number = 42
```

You can join <DNT>materialized views</DNT> with other data sources:

```sql
SELECT
  mv.store_id,
  mv.total_sales,
  s.store_name,
  s.region
FROM mv.store_sales_mv mv
JOIN snowflake.retail.stores s ON mv.store_id = s.store_id
WHERE mv.total_sales > 10000
```

````

## Related topics [#related-topics]

[Materialized views](https://docs.newrelic.com/docs/new-relic-lens/materialized-views)

Store query results for better performance.

[Lens overview](https://docs.newrelic.com/docs/new-relic-lens/overview)

Learn about Lens features, capabilities, and use cases.

[Set up connectors](https://docs.newrelic.com/docs/new-relic-lens/set-up-connectors)

Connect to external data sources.
