Skip to main content
The Narrative SDK provides methods to execute, validate, and compile NQL queries programmatically. This guide covers common query patterns and best practices.
For NQL syntax and language features, see the NQL Reference.

Prerequisites

Running a query

Basic execution

Use executeNql() to run a query and retrieve results:

Query input options

The executeNql() method accepts the following options:

Creating a view dataset

Set create_as_view to true to create a view dataset instead of a materialized view. A view dataset stores only the NQL query definition—not the query results. The NQL is inlined and re-evaluated at query time whenever other queries reference the view dataset.
View datasets have several restrictions compared to materialized views. You cannot create access rules, connections, or use features like MERGE ON, PARTITIONED_BY, or chunking strategies with view datasets. See view dataset limitations for the full list.

Understanding the result

The NqlResult object contains execution metadata:

Validating a query

Use validateNql() to check query syntax without execution:
If the query has syntax errors, the method throws an error with details about the issue.

Compiling a query

Use compileNql() to see the transpiled SQL without executing:
This is useful for:
  • Debugging query issues
  • Understanding how NQL maps to the underlying SQL dialect
  • Verifying Rosetta Stone mappings

Parsing a query

Use parseNql() to get the abstract syntax tree (AST):

Retrieving a previous query

If you have a job ID from a previous query, retrieve its results:

Query patterns

Filtering data

Aggregations

Using Rosetta Stone attributes

Query normalized data across datasets:

Error handling

Wrap query execution in try-catch to handle errors:

Best practices

NQL Syntax Reference

Complete NQL language reference

NQL Design Philosophy

Understanding NQL’s approach

Query Optimization

Tips for efficient queries

Error Handling

Handle SDK errors gracefully