Context Lab

Databricks Query Tags: Split SQL Warehouse Cost by Team

Post · Cost · GA 2026-09-30 · Updated 2026-10-08

GA on Sep 30, 2026

Query tags put key-value labels such as team or cost_center on SQL warehouse queries. They show up in system.query.history.

How to tag

Run SET QUERY_TAGS['team'] = 'finance' in the SQL editor, a notebook or a dashboard. Connectors and the Statement Execution API can set tags too.

From tags to cost

Split each warehouse's DBUs by each team's share of task time. Treat the result as an estimate, not an invoice.

Keep tags clean

Tags are stored as plain text and may be replicated globally. Never put PII or secrets in keys or values.

    Topic summary · original diagrams · no autoplay

    Query tags for SQL warehouses became generally available on September 30, 2026. You can attach key-value labels such as team or cost_center to SQL workloads, and they show up in system.query.history. That gives you a way to split a shared warehouse bill by team without running one warehouse per team.

    This post covers how to set tags, how to check that they landed, and one way to turn them into a per-team DBU estimate.

    A tagged SQL query lands in system.query.history and rolls up into cost by tag
    Tag the query, read the tags in query history, group cost by tag (original)

    What is new

    Releases are staged. Your account may get the feature a week or more after the release date.

    When to use it, and when not to

    Use it when several teams, dashboards or dbt projects share one SQL warehouse and finance wants a chargeback view, or when you need to find which workload is behind long-running queries.

    Skip it for workloads that do not run on a SQL warehouse or Lakehouse//RT, because nothing is recorded there. Never use tags to carry anything sensitive (see risks below). And do not treat a tag split as an invoice: Databricks bills the warehouse, and tags only help you divide that bill.

    Getting started

    1. Check access. You need access to the query_tags column in system.query.history. By default only admins can read this system table; Databricks recommends sharing it through a dynamic view per user or group.
    2. Agree on a short tag scheme, for example team, cost_center, env. Limits: 20 user tags per query, 128 characters per key or value, 10 KB per session. Keys cannot contain , : - / = or ., and keys starting with @@ are reserved.
    3. Set tags in SQL. This works anywhere you submit SQL to a warehouse: the SQL editor, notebooks and dashboards. An invalid key makes the statement fail.
      SET QUERY_TAGS['team'] = 'finance', QUERY_TAGS['cost_center'] = '701';
      SELECT * FROM sales_data;
      
      -- Show the tags on this session
      SET QUERY_TAGS;
      
      -- Remove one key
      SET QUERY_TAGS['cost_center'] = UNSET;
    4. Or set them from code. Python connector, session level (v4.1.3+):
      from databricks import sql
      import os
      
      with sql.connect(
          server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
          http_path=os.getenv("DATABRICKS_HTTP_PATH"),
          access_token=os.getenv("DATABRICKS_TOKEN"),
          query_tags={"team": "finance", "env": "prod"},
      ) as connection:
          with connection.cursor() as cursor:
              cursor.execute("SELECT * FROM samples.nyctaxi.trips LIMIT 10")

      The Statement Execution API takes a query_tags array of {"key": "team", "value": "finance"} objects on POST /api/2.0/sql/statements. In dbt, use +query_tags in dbt_project.yml.

    5. Confirm the tags landed. Records usually appear within an hour.
      SELECT statement_id, query_tags, executed_by, start_time
      FROM system.query.history
      WHERE MAP_CONTAINS_KEY(query_tags, 'team')
        AND query_tags['team'] = 'finance'
      ORDER BY start_time DESC
      LIMIT 100;

    From tags to a per-team cost estimate

    system.query.history has no dollar column. A practical method is to take each warehouse's DBUs from system.billing.usage and split them by each team's share of total_task_duration_ms. The query below is this site's estimate, built on documented columns; adjust the 30-day window to your billing period.

    -- Estimate: split each warehouse's DBUs by each team's share of task time
    WITH team_time AS (
      SELECT
        compute.warehouse_id AS warehouse_id,
        COALESCE(query_tags['team'], 'untagged') AS team,
        SUM(total_task_duration_ms) AS task_ms
      FROM system.query.history
      WHERE compute.type = 'WAREHOUSE'
        AND start_time >= date_sub(current_date(), 30)
      GROUP BY ALL
    ),
    warehouse_dbus AS (
      SELECT
        usage_metadata.warehouse_id AS warehouse_id,
        SUM(usage_quantity) AS dbus
      FROM system.billing.usage
      WHERE usage_metadata.warehouse_id IS NOT NULL
        AND usage_date >= date_sub(current_date(), 30)
      GROUP BY ALL
    )
    SELECT
      t.warehouse_id,
      t.team,
      ROUND(t.task_ms / SUM(t.task_ms) OVER (PARTITION BY t.warehouse_id), 3) AS share_of_task_time,
      ROUND(w.dbus * t.task_ms / SUM(t.task_ms) OVER (PARTITION BY t.warehouse_id), 2) AS estimated_dbus
    FROM team_time t
    JOIN warehouse_dbus w USING (warehouse_id)
    ORDER BY t.warehouse_id, estimated_dbus DESC;

    Multiply estimated_dbus by your $/DBU for the warehouse SKU to get dollars. If DBUs and $/DBU are new to you, start with What Is a Databricks DBU?, then try the cost calculator.

    Keep in mind: idle and startup time is billed but tied to no query, so this method spreads it by share. A large untagged row means coverage is still thin. Query history only includes workspaces in the same region as the workspace you query from, and billing records usually arrive within 12 hours.

    Cost and permission risks

    Sources

    Checked against official Databricks documentation on 2026-10-08. Context Lab is an independent education site and is not affiliated with Databricks, Inc.