1. Query structure #
SQL, adapted to time series.
Clauses #
| Clause | Role | Example |
|---|---|---|
| SELECT | function(s) to compute, with an AS alias | SELECT average(duration) AS 'latence' |
| FROM | one or more event types | FROM Transaction, TransactionError |
| WHERE | filter: =, !=, LIKE, IN, IS NULL, RLIKE | WHERE appName = 'boutique' AND http.statusCode >= 500 |
| FACET | groups (10 groups by default) | FACET name LIMIT 20 |
| SINCE / UNTIL | time range (60 minutes by default) | SINCE 1 day ago UNTIL 1 hour ago |
| TIMESERIES | splits into buckets for a chart | TIMESERIES 5 minutes |
| COMPARE WITH | overlays a previous period | COMPARE WITH 1 week ago |
| LIMIT | number of rows or groups | LIMIT MAX |
First queries #
SELECT count(*) FROM Transaction SINCE 30 minutes ago
SELECT average(duration), percentile(duration, 95, 99) FROM Transaction WHERE appName = 'boutique' FACET name
SELECT count(*) FROM TransactionError FACET error.class SINCE 1 day ago
SELECT * FROM Log WHERE message LIKE '%timeout%' SINCE 15 minutes ago LIMIT 100Good to know : Attribute names containing special characters are written with backticks: `http.response.status_code`. Strings go in single quotes.2. Functions #
Aggregating and computing.
Common functions #
| Function | Result |
|---|---|
| count(*), uniqueCount(attr) | event count, number of distinct values (approximate above 256) |
| average, sum, min, max, median | classic statistics |
| percentile(attr, 95, 99) | percentiles: the real perceived latency |
| histogram(attr, 2, 20) | distribution in 20 buckets up to 2 |
| percentage(count(*), WHERE error IS true) | share of events matching a condition |
| filter(count(*), WHERE …) | aggregate restricted to a condition, several per query |
| rate(count(*), 1 minute) | throughput normalized per time unit |
| latest(attr), earliest(attr) | most recent / oldest value |
| apdex(duration, t: 0.5) | Apdex score |
| funnel(session, WHERE page = '/panier', WHERE page = '/paiement') | conversion funnel |
Analysis queries #
-- Error rate per service, compared with last week
SELECT percentage(count(*), WHERE error IS true) FROM Transaction FACET appName SINCE 1 day ago COMPARE WITH 1 week ago
-- Several indicators in one query
SELECT filter(count(*), WHERE http.statusCode >= 500) AS '5xx', filter(count(*), WHERE http.statusCode = 404) AS '404' FROM Transaction TIMESERIES
-- Group values into readable categories
SELECT count(*) FROM Transaction FACET CASES(WHERE duration < 0.2 AS 'fast', WHERE duration < 1 AS 'ok', WHERE duration >= 1 AS 'slow')3. Advanced NRQL #
Subqueries, metrics and API.
Subqueries and nested aggregation #
-- Nested aggregation: maximum throughput per minute
SELECT max(n) FROM (SELECT count(*) AS n FROM Transaction TIMESERIES 1 minute) SINCE 1 day ago
-- Subquery: transactions of the 5 busiest hosts
SELECT average(duration) FROM Transaction WHERE host IN (SELECT hostname FROM SystemSample FACET hostname ORDER BY average(cpuPercent) LIMIT 5) FACET hostNRQL also supports JOIN between a main query and a faceted subquery, handy to enrich events with a lookup table (lookup tables imported as CSV).
Dimensional metrics #
Metric data (OpenTelemetry, Prometheus, integrations) is pre-aggregated. Query it by metric name, with functions suited to the type: SELECT rate(sum(http.server.requests), 1 minute) FROM Metric FACET service.name TIMESERIES. Events to metrics create metrics from events for long, cheap retention.
NRQL via the API #
curl -s https://api.newrelic.com/graphql \
-H 'Content-Type: application/json' -H "API-Key: $NEW_RELIC_API_KEY" \
-d '{"query":"{ actor { account(id: '"$NEW_RELIC_ACCOUNT_ID"') { nrql(query: \"SELECT count(*) FROM Transaction SINCE 1 hour ago FACET appName\") { results } } } }"}' | jq '.data.actor.account.nrql.results'Best practice : Use the New Relic AI assistant to turn a question into NRQL, then review and adjust the query: a good way to learn the syntax.
Commands to know #
Commands to know
SELECT percentile(duration, 95) FROM Transaction FACET name: p95 latency per transaction… SINCE 1 day ago COMPARE WITH 1 week ago: compare with last weekSELECT percentage(count(*), WHERE error IS true) FROM Transaction: error rateSELECT keyset() FROM Transaction: list available attributesSHOW EVENT TYPES SINCE 1 week ago: list event types
Sources #
Checked in November 2026: New Relic evolves its platform continuously (UI, plans, limits); check the documentation before relying on a detail.
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Dynatrace metrics: Data Explorer, metric selectors, API and dashboards
Use metrics in Dynatrace: built-in and custom metrics, Data Explorer, metric selector syntax, Met...
Digital experience: Browser, mobile and Synthetics
Measuring what users experience with New Relic: browser agent (page load, Core Web Vitals, AJAX, ...
Advanced Grail and DQL: performance, parse, joins, time series, entities and correlation
Writing fast, low-cost DQL queries, parsing text with DPL and JSON, combining sources with lookup...
The HCL language: resources, variables, outputs, data and expressions
Writing Terraform: terraform, provider, resource and data blocks, typed variables with validation...
Using Faro data: queries, dashboards, alerts and GDPR
Query Faro data in Loki (Web Vitals, errors, events), build dashboards and alerts, and comply wit...
Dashboards, reports and alerts in Splunk
Making the most of Splunk searches: reports and scheduling, Dashboard Studio dashboards (data sou...
Automating Grafana: HTTP API, Terraform and dashboard lifecycle
Industrializing Grafana: HTTP API (folders, dashboards, users, data sources), Terraform provider,...
Logs with OpenTelemetry: data model, appenders, correlation and collection
Master the log signal: the LogRecord data model, the Logs Bridge API and appenders (Logback, Log4...
Service-Level Objectives (new platform) in Dynatrace
Defining and steering SLOs in the Dynatrace Service-Level Objectives app: templates and custom DQ...