Query icon

Query

Query your data by executing SQL queries

Overview

This node allows you to run SQL queries on your incoming data within an n8n workflow. It treats the input data as tables and executes SQL statements against them, enabling complex data manipulation, filtering, joining, and aggregation without needing an external database.

Common scenarios include:

  • Filtering or transforming JSON data arrays using familiar SQL syntax.
  • Joining multiple arrays of objects contained in different properties of an item.
  • Aggregating data from incoming items treated as rows in a table.
  • Extracting specific subsets of data based on conditions.

For example, if you have a list of user objects coming into the node, you can write a SQL query to select only users from a certain country or with a specific attribute. Or, if your items contain nested arrays (like orders or teams), you can treat those arrays as separate tables and join them in your query.

Properties

Name Meaning
Mode Choose how to treat incoming data:
- Treat incoming items as rows of a table: all input items form one table.
- Select properties in item to treat as a table: specify which properties (arrays) inside each item should be treated as separate tables.
In this mode the node will be executed only once Informational notice shown when "Treat incoming items as rows of a table" mode is selected.
Table Name The name of the table representing all incoming items when using "Treat incoming items as rows of a table" mode. Use this name in your SQL query to reference the data. Example: items.
Properties to Treat as Tables Comma-separated list of property names inside the incoming item that contain arrays of objects to be treated as tables. Required when using "Select properties in item to treat as a table" mode. Example: users, teams.
Query The SQL query to execute on the prepared tables. Supports full SQL syntax as provided by the underlying SQL engine. This is required.
Simplify Output Whether to return a simplified version of the query result (true) or the raw detailed data (false). Simplified output returns just the resulting rows; raw output includes statement indices and metadata.

Output

The node outputs an array of items where each item's json field contains the results of the SQL query execution:

  • If Simplify Output is enabled (default), the output items' json fields contain individual rows returned by the query as plain objects.
  • If disabled, the output includes more detailed information per statement, including the statement index and raw data.
  • If no results are returned, a single item with { success: true } is output.
  • If errors occur during table creation or query execution, error details are included in the output if "Continue On Fail" is enabled.

The node does not output binary data.

Dependencies

  • Uses the alasql library internally to parse and execute SQL queries on JavaScript objects.
  • No external API or service calls are made.
  • Requires no special credentials but depends on correct input data structure matching the selected mode.

Troubleshooting

  • Error: The selected property X is not an array
    Occurs when using "Select properties in item to treat as a table" mode but the specified property is missing or not an array. Ensure the property exists and contains an array of objects.

  • Issue while creating table
    Indicates a problem preparing the data for querying, possibly due to invalid table names or malformed input data.

  • Issue while executing query
    Usually caused by syntax errors in the SQL query or referencing non-existent tables/columns. Verify your SQL syntax and table/property names.

  • If the node is set to continue on fail, errors will appear in the output items under an error key instead of stopping execution.

  • When using "Treat incoming items as rows of a table" mode, the node runs only once regardless of the number of input items.

Links and References

Discussion