---
title: Add SQL conditions to job triggers
description: Add a SQL condition to a Lakeflow Jobs trigger so that a run starts only when a SQL query shows that your data is ready.
canonical_url: https://docs.databricks.com/gcp/en/jobs/trigger-sql-conditions
---

# Add SQL conditions to job triggers

Use a SQL condition to check your data before a triggered job run starts. When the trigger fires, Databricks runs a saved SQL query on a SQL warehouse that you choose. The run starts only if the query result meets the condition. For example, run a job only when an upstream table has finished loading or a data quality check passes, without adding a separate checker task to the job.

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/gcp/en/release-notes/release-types.md). To use it, a workspace admin must turn on **SQL conditions for triggers** from the **Previews** page. See [Manage Databricks previews](https://docs.databricks.com/gcp/en/admin/workspace-settings/manage-previews.md).

## Requirements

* You must have CAN MANAGE on the job to configure its triggers.
* You need a [saved query](https://docs.databricks.com/gcp/en/sql/user/queries/index.md) that doesn't use `{{ }}` query parameters. To use values from the job, write [named parameter markers](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-parameter-marker.md) instead. See [Use job parameters in a condition](https://docs.databricks.com/gcp/en/jobs/trigger-sql-conditions.md#use-job-parameters-in-a-condition). You must have access to the query and be able to run it on the SQL warehouse that you select.
* The job's [Run as identity](https://docs.databricks.com/gcp/en/jobs/privileges.md) must be able to open the saved query, have CAN USE on the SQL warehouse, and have read access to every table that the query reads. The condition query runs as this identity.

## How SQL conditions work

The trigger decides when to check. The SQL condition decides whether to start a run. After the trigger fires, Databricks runs the query in read-only mode. A run starts only when both the trigger and the SQL condition are satisfied.

You can add one SQL condition to a [scheduled](https://docs.databricks.com/gcp/en/jobs/scheduled.md), [table update](https://docs.databricks.com/gcp/en/jobs/trigger-table-update.md), [file arrival](https://docs.databricks.com/gcp/en/jobs/file-arrival-triggers.md), [model update](https://docs.databricks.com/gcp/en/jobs/model-update-triggers.md), or [job completion](https://docs.databricks.com/gcp/en/jobs/job-completion-triggers.md) trigger. [Continuous triggers](https://docs.databricks.com/gcp/en/jobs/continuous.md) don't support SQL conditions.

Choose how Databricks evaluates the query result:

| Condition type           | API value              | Starts a run when                                        | Query requirements                                                              |
| ------------------------ | ---------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------- |
| **Query returns rows**   | `QUERY_RETURNS_ROWS`   | The result contains at least one row.                    | None. Zero rows means that the condition isn't met.                             |
| **Result value changes** | `RESULT_VALUE_CHANGES` | The result differs from the value at the previous check. | Must return exactly one row and one column. The value must be 1 MiB or smaller. |

For **Result value changes**, Databricks runs the query in the background right after you save the trigger and stores the result as a baseline. This check doesn't start a run. Later checks start a run when the result differs from the stored value. Changing the query, SQL warehouse, or condition type on the trigger resets the baseline, and Databricks stores a new one in the same way. Pausing the trigger or changing other trigger settings can also reset it. Editing the text of the saved query or changing a job parameter doesn't reset it.

Each check works as follows:

* A check, including SQL warehouse startup, times out after 5 minutes. If a check times out, no run starts, and the trigger checks again at its next evaluation.
* A trigger runs at most one check at a time. If the trigger is due while a check is still running, Databricks skips that evaluation instead of queuing it.
* If the query has more than one statement, only the result of the last statement is used.
* SQL conditions apply only to runs that the trigger starts. **Run now** and other manual runs don't check the SQL condition.

> [!TIP]
>
> For **Query returns rows**, make sure that the query returns zero rows when the data isn't ready. A query like `SELECT count(*) > 100 FROM main.sales.orders` returns one row even when the count is 100 or less, so the condition is met. Filter with `WHERE` or `HAVING` instead:
>
> ```sql
> SELECT 1
> FROM main.sales.orders
> WHERE order_date = current_date()
> HAVING count(*) > 100
> ```

## <a id="use-job-parameters-in-a-condition" />Use job parameters in a condition

A condition query can use the job's [parameters](https://docs.databricks.com/gcp/en/jobs/job-parameters.md). Write a named parameter marker, such as `:catalog`, in the query. When Databricks checks the condition, each marker gets the value of the job parameter with the same name. For example, give the job a `catalog` parameter with a different value in each environment, and use the same query everywhere.

The following query reads the `sales.orders` table in the catalog that the `catalog` job parameter names:

```sql
SELECT 1
FROM IDENTIFIER(:catalog || '.sales.orders')
WHERE order_date = current_date()
HAVING count(*) > 100
```

Markers and values work as follows:

* A marker matches the job parameter with exactly the same name, including case. For a name that isn't a regular identifier, enclose the name in backticks, for example ``:`env-name` ``.
* The query doesn't need to use every job parameter. A check ignores the parameters that the query doesn't use.
* If a job parameter's value contains a [dynamic value reference](https://docs.databricks.com/gcp/en/jobs/dynamic-value-references.md), Databricks fills it in before the check runs. A check fills in `{{job.id}}`, `{{workspace.id}}`, `{{job.trigger.type}}`, and `{{job.trigger.time.<argument>}}`. The trigger time is the time of the check, in UTC. [Date arithmetic](https://docs.databricks.com/gcp/en/jobs/dynamic-value-references.md#date-arithmetic) on the trigger time works the same way as in a job run.

A check fails with an error, and no run starts, when the query uses a marker that has no value:

* The job has no parameter with the marker's name. The error names the marker.
* The job parameter's value uses a reference that has a value only during a run, such as `{{job.run_id}}` or `{{job.start_time.iso_date}}`.
* The job parameter's value uses another reference that a check can't fill in, such as `{{job.trigger.file_arrival.location}}` or `{{workspace.url}}`.

When you select a query in the trigger dialog, the **Parameters** table lists each marker in the query. **Value comes from** shows the job parameter that provides the value, and **Value at check time** shows the value that a check would use now. The table also flags each marker that has no value:

* A missing job parameter shows **Not set** with a warning. You can still save the trigger.
* A reference that a check can't fill in shows **Not available** with a warning. You can still save the trigger.
* A reference that has a value only during a run shows **Not available** with an error. **Save** and **Test trigger** are unavailable until you change the job parameter or remove the marker from the query.

To change a value, edit the job parameter. See [Add or edit job parameters](https://docs.databricks.com/gcp/en/jobs/job-parameters.md#add-or-edit-job-parameters).

## Add a SQL condition to a trigger

1. In the workspace sidebar, click **Jobs & Pipelines**, then open the job.
2. In **Schedules & Triggers**, click **Add trigger**, or edit an existing trigger.
3. Select the **Trigger type** and configure the trigger.
4. Expand **Advanced**, then click **Add SQL condition**.
5. Select the **SQL query** and the **SQL warehouse**.
6. Under **Trigger when**, select **Query returns rows** or **Result value changes**.
7. Click **Save**.

**Test trigger** checks that the job can use the saved query. It confirms that the query exists, isn't in the trash, and has no `{{ }}` parameters, but it doesn't run the query or fill in job parameter values. To see the query result, run the query in the SQL editor.

To remove a condition, edit the trigger, expand **Advanced**, and click **Remove SQL condition**.

## Configure with the API, CLI, or bundles

Set `sql_condition` with `sql_query_id`, `warehouse_id`, and `trigger_mode`. For a scheduled trigger that uses a cron expression, set it on `schedule`. For other trigger types, set it on `trigger`, next to the trigger type settings. With the [Multiple Triggers Beta](https://docs.databricks.com/gcp/en/jobs/triggers.md#multiple-triggers), set it on each entry in `triggers`.

### REST API

Save this request as `update-job.json` and send it to `POST /api/2.2/jobs/update` in the [Jobs API](https://docs.databricks.com/api/workspace/jobs/update). Replace the IDs with your job, saved query, and SQL warehouse IDs.

```json
{
  "job_id": 9001,
  "new_settings": {
    "trigger": {
      "pause_status": "UNPAUSED",
      "table_update": {
        "table_names": ["main.sales.orders"],
        "condition": "ANY_UPDATED"
      },
      "sql_condition": {
        "sql_query_id": "fe25e731-92f2-4838-9fb2-1ca364320a3d",
        "warehouse_id": "5d409943fcd32c6f",
        "trigger_mode": "QUERY_RETURNS_ROWS"
      }
    }
  }
}
```

### CLI

Save the request body from the **REST API** tab as `update-job.json`, then run:

```bash
databricks api post /api/2.2/jobs/update --json @update-job.json
```

### Bundles

For [Declarative Automation Bundles](https://docs.databricks.com/gcp/en/dev-tools/bundles/resources.md), add the following settings to an existing job resource, alongside its tasks:

```yaml
resources:
  jobs:
    orders_report:
      trigger:
        pause_status: UNPAUSED
        periodic:
          interval: 1
          unit: DAYS
        sql_condition:
          sql_query_id: fe25e731-92f2-4838-9fb2-1ca364320a3d
          warehouse_id: 5d409943fcd32c6f
          trigger_mode: RESULT_VALUE_CHANGES
```

Bundles can't create saved queries. Create the query in the workspace first, then reference its ID.

To give the condition a different catalog in each target, set a job parameter from a [custom variable](https://docs.databricks.com/gcp/en/dev-tools/bundles/variables.md#custom-variables), and use `:catalog` in the query. Add the following settings:

```yaml
variables:
  catalog:
    default: dev

targets:
  prod:
    variables:
      catalog: prod

resources:
  jobs:
    orders_report:
      parameters:
        - name: catalog
          default: ${var.catalog}
```

A `sql_condition` that you send to `jobs/update` replaces the current one as a whole, so include all of its fields. If you leave out `sql_condition`, the current condition stays. To update multiple triggers, see [Add multiple triggers to a job](https://docs.databricks.com/gcp/en/jobs/triggers.md#multiple-triggers).

## Monitor SQL condition checks

* For scheduled triggers, the trigger status on the job page shows whether the latest SQL condition check was satisfied. Click **Query execution details** to open the statement in [query history](https://docs.databricks.com/gcp/en/sql/user/queries/query-history.md).
* To read the latest check that ran without an error, call the [Get job API](https://docs.databricks.com/api/workspace/jobs/get) with `include_trigger_state=true` and read `trigger_state.sql_condition`. The `latest_condition_evaluation_satisfied` field shows whether that check met the condition, and `latest_condition_evaluation_sql_statement_id` identifies its statement in query history. A failed check doesn't change these fields. With Multiple Triggers, read `trigger_details[].state.sql_condition`.
* For each run that a SQL condition started, the **Launched** field in the runs list and on the run details page shows a **SQL** tag. Click it, then click **Query execution details** to open that run's check in query history.
* If a check fails for a reason that you can fix, the error appears in the trigger status on the job page. Databricks also sends the error to the job's failure notifications, including email recipients and webhook destinations. You get one notification when a working condition starts to fail, not one for every failed check. The error stays until the next successful check. See [Add notifications on a job](https://docs.databricks.com/gcp/en/jobs/notifications.md).

Each check runs as a SQL statement on your SQL warehouse. To review checks over time, query the [query history system table](https://docs.databricks.com/gcp/en/admin/system-tables/query-history.md) for the warehouse.

## Troubleshooting

| Issue                                                                  | Action                                                                                                                                                             |
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| The job runs every time the trigger fires                              | For **Query returns rows**, change the query so that it returns zero rows when the data isn't ready.                                                               |
| **Result value changes** never starts a run                            | The baseline is the query result at the time you saved the trigger. A run starts only after the query result changes from that value.                              |
| The check times out                                                    | Make the query faster, or use a [serverless SQL warehouse](https://docs.databricks.com/gcp/en/compute/sql-warehouse/index.md), which starts faster.                |
| A check fails because the job has no parameter with the marker's name  | Add a job parameter with the same name as the marker, or remove the marker from the query. Names are case-sensitive.                                               |
| A check fails because a job parameter has no value before a run starts | The job parameter uses a reference that exists only during a run, such as `{{job.run_id}}`. Change the job parameter's value, or remove the marker from the query. |
| **Add SQL condition** doesn't appear                                   | Expand **Advanced**. If the button is still missing, ask a workspace admin to enable **SQL conditions for triggers**.                                              |

## Limitations

* Each trigger supports one SQL condition.
* Continuous triggers don't support SQL conditions.
* Condition queries run only on SQL warehouses, in read-only mode.
* A check, including SQL warehouse startup, times out after 5 minutes.
* A trigger keeps only its latest check, not a full history of every check. To see earlier checks, use the **SQL** tag on runs that the condition started, or [query history](https://docs.databricks.com/gcp/en/sql/user/queries/query-history.md).
* Checks run on your SQL warehouse and are billed as SQL warehouse usage. A check can start a stopped warehouse.
* Triggers with SQL conditions count toward the [workspace limit of 12,000 active triggers](https://docs.databricks.com/gcp/en/jobs/triggers.md#trigger-limits).
* Job parameter values reach the query as text. To compare a number or a date, cast the value in the query, for example `CAST(:min_rows AS INT)`.
* A check can't fill in `{{workspace.url}}`. If the query uses a job parameter whose value contains it, the check fails.
* **Test trigger** doesn't run the query or fill in job parameter values.
