Queries, Alerts & Query History
Databricks · Databricks SQL & BI
Theory
A saved query is a named SQL statement stored in the workspace (Queries in the sidebar) that you can share, parameterise, schedule and reuse in dashboards. A Databricks SQL alert runs a query on a schedule, compares a value in the result with a condition, such as failed_orders > 0, and notifies email or Slack/Teams destinations when the condition is met. Query History lists every statement run on SQL warehouses and serverless compute with user, duration, status and source. Clicking a query opens its query profile, a visual breakdown of the execution plan showing time, rows and bytes for each operator, which is the main tool for finding why a query is slow.
Example: an alert checks SELECT COUNT(*) AS late_orders FROM main.sales.orders WHERE amount IS NULL every hour and emails Asha when it is above 0.
Why we use it
Saved queries avoid copy-pasted SQL, alerts catch data problems without someone watching, and query history with profiles shows who ran what and where time went, which is essential for debugging and tuning.
When to use it
Use alerts for simple threshold checks on data (late loads, nulls, sudden drops). For quality rules inside pipelines use expectations instead. Use Query History and the profile whenever a dashboard or query is slow, before resizing the warehouse.
How it works
- Write and save the check query
- + New → Alert, choose the query and the result column
- Set condition (e.g. bad_orders > 0) and a schedule
- Condition met when it runs? Yes: Send notification to subscribers. No: Stay OK, check again next time.
- Investigate with Query History and the query profile
Syntax
UI: Queries → Create query | + New → Alert → pick query/SQL, condition (value, operator, threshold), schedule, notify
UI: Query History → filter (user, warehouse, status, duration) → click query → See query profile
SQL: system.query.history (query history system table)Example
-- Query used by an alert (runs every hour)
SELECT COUNT(*) AS bad_orders
FROM main.sales.orders
WHERE order_date = current_date()
AND (amount IS NULL OR amount <= 0);
-- Alert condition: bad_orders > 0 → notify data-oncall@techtown.in
-- Slowest queries yesterday from the system table
SELECT executed_by, statement_text, total_duration_ms
FROM system.query.history
WHERE start_time >= current_date() - INTERVAL 1 DAY
ORDER BY total_duration_ms DESC
LIMIT 10;Key points
- Saved queries can have parameters and be shared with CAN RUN/CAN EDIT
- Alerts evaluate one value from a query result against a threshold
- Alert notifications can go to email or configured Slack/Teams/webhook destinations
- Query History covers SQL warehouses and serverless; filter by user or duration
- The query profile shows the slowest operators, rows scanned and spill
- system.query.history lets you analyse queries with SQL