SQL Query
Fails when a SQL query of your own returns rows.
The SQL Query monitor runs a SQL query of your own that selects the rows breaking a business rule, and alerts you when it returns rows.
When to Use
- Complex business rules: The rule needs aggregations, subqueries, or window functions, for example "the amounts of the related rows in another table add up to the total of each row".
- Suspicious aggregates: Find the identifiers that appear in more than 50 rows created on the same day, which reveals a bot or a duplicated import.
When the rule can be written as a condition on each row, use No-Code Condition or SQL Condition instead: they support incremental scans, where clauses, and group by without writing a full query.
How It Works
Write a query that selects the rows that break your rule. The query runs on the data source of the dataset you selected, with its database and schema as defaults, and can join other tables of the same data source.
Sifflet wraps your query in a SELECT COUNT(*) query and uses the number of returned rows as the data point. Each run produces one data point. If the query returns a single row with a single integer column, Sifflet uses this integer as the data point instead.
The default static threshold fails when the data point is greater than 0, that is, as soon as the query returns one row. Change the threshold to tolerate some rows, to compare with the previous run (relative), or to learn the usual number of rows (dynamic).
Example
You want every order created yesterday to be fully paid. Create a SQL Query monitor on orders, with a daily schedule and the following query (here on Snowflake):
SELECT o.order_id
FROM sales.orders o
LEFT JOIN sales.payments p
ON p.order_id = o.order_id AND p.status = 'succeeded'
WHERE o.status <> 'cancelled'
AND o.created_at::date = CURRENT_DATE - 1
GROUP BY o.order_id, o.order_total
HAVING COALESCE(SUM(p.amount), 0) <> o.order_totalEach order is paid by one or more rows of payments, and its successful payments must add up to its order_total. Keep the default static threshold. On June 18, a payment provider outage leaves 230 orders of June 17 without a successful payment. The query returns 230 rows, greater than 0, so the monitor fails.
Configure the Monitor
SQL Query Settings
| Setting | Description | Default |
|---|---|---|
| SQL query | The query that selects the rows breaking your rule. | None |
To write the query from a description in natural language, use Generate SQL: the Sifflet AI Assistant uses the descriptions of your Data Catalog to understand your data, so documenting fields with unclear names improves the result. If a test run fails, select Fix SQL with AI to get a corrected query.

Fix SQL with AI after a test run fails because of an incorrect column name
Common Settings
- Threshold: Static (default), relative, or dynamic. See Thresholds.
- There is no scan mode, where clause, or group by: write them in the query.
Monitor as Code
The monitor kind is Sql. The following YAML creates the monitor of the example:
kind: Sql
sql: |
SELECT o.order_id
FROM sales.orders o
LEFT JOIN sales.payments p
ON p.order_id = o.order_id AND p.status = 'succeeded'
WHERE o.status <> 'cancelled'
AND o.created_at::date = CURRENT_DATE - 1
GROUP BY o.order_id, o.order_total
HAVING COALESCE(SUM(p.amount), 0) <> o.order_total| Parameter | Required | Description |
|---|---|---|
sql | Yes | The query that selects the rows breaking your rule. |
See the Parameters reference.
Limitations
Sifflet cannot wrap a multi-step query (a SQL script, or a query with statements such as DECLARE, BEGIN, CREATE, or INSERT) in a SELECT COUNT(*) query. It runs such a query as is and counts the returned rows, up to 1,000: if the query returns more rows, the data point is 1,000.
Updated 5 days ago

