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

  1. Write and save the check query
  2. + New → Alert, choose the query and the result column
  3. Set condition (e.g. bad_orders > 0) and a schedule
  4. Condition met when it runs? Yes: Send notification to subscribers. No: Stay OK, check again next time.
  5. 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

Databricks topics

Databricks Fundamentals

Compute

Notebooks & Development

Apache Spark on Databricks

Delta Lake

Unity Catalog & Governance

Data Ingestion

Declarative Pipelines (Lakeflow)

Lakeflow Jobs

Databricks SQL & BI

Architecture, Performance & Cost

Machine Learning & AI

Certification & Interview

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master Databricks

The lakehouse platform from the ground up: Apache Spark, Delta Lake, Unity Catalog, Lakeflow pipelines and jobs, Databricks SQL and MLflow. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL / Spark SQL Sharpen the SQL you use in Databricks SQL and Spark SQL: 150 query problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Databricks  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

{{ sec.h }}

{{ sec.body }}

{{ sec.body }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
On screen — where to click

{{ rich.ui.title }}

{{ u.text }}
  1. {{ st.n }}{{ st.text }}
{{ rich.ui.title }}
{{ u.text }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
Want to run this yourself? Databricks needs a real workspace — there's no in-browser engine for it. Click Copy above, then paste it into a notebook or the SQL editor in Databricks Free Edition (free, no credit card) and run it on serverless compute.
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic