---
title: Entities & relationships examples via NRQL (E&R via NRQL)
source: https://docs.newrelic.com/docs/nrql/nrql-examples/query-entities-via-nrql
---

Streamline your system analysis by using New Relic Query Language (NRQL) to directly query entity and relationship data. This approach replaces cumbersome manual processes for crucial tasks like correlating CPU performance with host attributes, viewing past entity states, or tracking configuration changes, offering faster insights into your complex environments.

To query this data, use the `Entity` event type (and `Relationships` or `entityRelationships` for relationship-specific queries).

> #### 💡 TIP
>
> New to querying entities & relationships with NRQL? Learn about the [concepts and benefits of E&R via NRQL](https://docs.newrelic.com/docs/nrql/get-started/query-entities-and-relationships-via-nrql) before exploring these examples.

Explore the practical NRQL solutions below to help you:

**Correlate CPU usage with host attributes**

-   **Objective**: An operations team needs to analyze CPU utilization for all production hosts located in a specific AWS region (e.g., 'eu-central-1') to identify potential performance bottlenecks.
-   **Challenge**: This often required exporting host data and performance metrics separately and then using external tools or complex scripts to join and analyze them.
-   **NRQL solution**:

    ```sql
    FROM SystemSample
    JOIN (FROM Entity SELECT id, name WHERE type = 'INFRA-HOST' AND `tags.aws.awsRegion` = 'eu-central-1')
    ON entityGuid = id
    SELECT average(cpuPercent) FACET name
    ```

**View entity state at a specific point in time**

-   **Objective**: A developer is investigating an alert event that occurred several hours ago and needs to know the exact configuration or state of a specific host (or container, application, etc.) at that particular point in time.

-   **Challenge**: Obtaining a snapshot of an entity's attributes from a specific past timeframe was often not possible or required sifting through voluminous configuration logs, if available.

-   **NRQL solution**: This query retrieves all available attributes for a specific entity (identified by its id) within a narrow one-hour window from seven hours ago.

    ```sql
    FROM Entity
    SELECT *
    WHERE id = '<your_entity_id>'
    SINCE 7 hours ago UNTIL 6 hours ago
    LIMIT 1
    ```

**Tracking entity state changes**

-   **Objective**: An SRE wants to understand how an entity's configuration or key attributes have changed over the last few hours, perhaps to see if a deployment or an automated process has altered its state as expected.

-   **Challenge**: Tracking subtle changes in an entity's state over time was difficult and often involved manual comparisons or custom monitoring scripts.

-   **NRQL solution**: This query fetches all recorded states for a particular entity within the last three hours, allowing for an audit of any changes.

    ```sql
      SELECT *
      FROM Entity
      WHERE id = '<your_entity_id>'
      SINCE 3 hours ago
    ```

**Finding applications related to hosts**

-   **Objective**: Identify the applications running on specific hosts

-   **Challenge**: Difficult to correlate applications to hosts

-   **NRQL solution**:

    ```sql
    FROM EntityRelationship SELECT sourceEntityGuid, targetEntityGuid, sourceEntityType, targetEntityType, relationshipType   where (sourceEntityType like '%HOST%' and targetEntityType='APPLICATION') or (targetEntityType LIKE '%HOST%' and sourceEntityType='APPLICATION')
    ```

```
  
```
