Our pipeline from the earlier lessons lands clean data in gold tables every morning. But nobody at ShopKart wants to open a notebook to see yesterday's sales. They want a dashboard they can filter, and a message when something looks wrong. That's the job of Databricks SQL. In this lesson we create a 50-day sales table, analyse it with SQL, turn it into a dashboard, and add an alert.
We ran every query on Apache Spark 4 with Delta Lake, which uses the same SQL and the same named parameters as Databricks SQL. The results below are the real output. (Two Databricks-only features, materialized views and the dashboard and alert screens, are marked as such.)
What Databricks SQL is
π₯Gold tables
In Unity Catalog, written by your pipeline
β‘SQL warehouse
Compute made for SQL
πQueries
SQL editor, with :parameters
πDashboards & Genie
Charts, filters, plain-English questions
πAlerts
Email or Slack when a number crosses a line
Everything reads the same Delta tables your pipelines write, with the same Unity Catalog permissions. No copying data into a separate warehouse, no second set of access rules.
SQL warehouses: compute for SQL
A SQL warehouse is compute made for SQL: many short queries from dashboards, the SQL editor and BI tools like Power BI or Tableau. You pick a type and a T-shirt size (2X-Small up to 4X-Large), and it can add clusters when many people query at once.
| Type | Starts in | Includes | Choose it when |
|---|---|---|---|
| Serverless | Typically 2β6 seconds | Photon, Predictive IO, Intelligent Workload Management | Almost always, if your region has it |
| Pro | About 4 minutes | Photon, Predictive IO | Serverless isn't available, or you need custom networking |
| Classic | About 4 minutes | Photon | Basic needs only |
Step 1: Create the sample sales table
Open the SQL editor, pick a SQL warehouse, and run this once. It builds shopkart.gold.daily_sales: one row per day, city and category, for 1 October to 19 November 2026. The numbers look random but come from hash(), so you get the same table every time, with a Diwali rush (5β8 November), busier weekends, and each city's favourite category.
-- 50 days of ShopKart sales: one row per day, city and category.
-- hash() makes the numbers look random but come out the same every time, on any workspace.
CREATE OR REPLACE TABLE shopkart.gold.daily_sales AS
WITH days AS (
SELECT explode(sequence(DATE'2026-10-01', DATE'2026-11-19', INTERVAL 1 DAY)) AS order_date
),
cities AS (
SELECT * FROM VALUES ('Kolkata'), ('Mumbai'), ('Delhi'), ('Bengaluru'), ('Pune') AS t(city)
),
categories AS (
SELECT * FROM VALUES ('Electronics', 2400), ('Fashion', 900), ('Home', 1300) AS t(category, avg_price)
),
tastes AS ( -- some cities love some categories
SELECT * FROM VALUES ('Kolkata', 'Fashion', 3.0), ('Mumbai', 'Home', 2.0), ('Pune', 'Home', 2.0) AS t(city, category, taste)
),
grid AS (
SELECT d.order_date, c.city, k.category, k.avg_price,
coalesce(t.taste, 1.0) *
CASE WHEN d.order_date BETWEEN DATE'2026-11-05' AND DATE'2026-11-08' THEN 2.0 -- Diwali rush
WHEN dayofweek(d.order_date) IN (1, 7) THEN 1.3 -- weekends
ELSE 1.0 END AS boost
FROM days d CROSS JOIN cities c CROSS JOIN categories k
LEFT JOIN tastes t ON t.city = c.city AND t.category = k.category
)
SELECT order_date, city, category,
CAST((20 + abs(hash(order_date, city, category)) % 40) * boost AS INT) AS orders,
CAST((20 + abs(hash(order_date, city, category)) % 40) * boost
* (avg_price + abs(hash(category, order_date)) % 300) AS DECIMAL(12, 2)) AS revenue
FROM grid;Change shopkart to a catalog you can write to, if needed. Our run created 750 rows: 43,556 orders and βΉ6.90 crore of revenue.
Step 2: Explore it with SQL
SELECT city, SUM(orders) AS orders, SUM(revenue) AS revenue
FROM shopkart.gold.daily_sales
GROUP BY city
ORDER BY revenue DESC+---------+------+-----------+
|city |orders|revenue |
+---------+------+-----------+
|Kolkata |11971 |16831362.60|
|Mumbai |9212 |14913465.50|
|Pune |8746 |14180806.30|
|Delhi |6999 |11845804.50|
|Bengaluru|6628 |11249362.00|
+---------+------+-----------+Kolkata leads by a wide margin. We'll find out why in a minute. The SQL editor also lets you turn any result into a chart, and keeps every run in Query history.
Step 3: Add parameters
A dashboard needs the same query for different dates and cities. Instead of editing the SQL, add named parameter markers: a colon and a name. The editor shows an input box for each one.
-- The three numbers at the top of the dashboard
SELECT SUM(revenue) AS revenue,
SUM(orders) AS orders,
ROUND(SUM(revenue) / SUM(orders), 0) AS avg_order_value
FROM shopkart.gold.daily_sales
WHERE order_date BETWEEN :start_date AND :end_date
AND (:city = 'All' OR city = :city)With :start_date = '2026-11-01', :end_date = '2026-11-19' and :city = 'All':
+-----------+------+---------------+
|revenue |orders|avg_order_value|
+-----------+------+---------------+
|28514395.50|18094 |1576 |
+-----------+------+---------------+Same query, :city = 'Pune':
+----------+------+---------------+
|revenue |orders|avg_order_value|
+----------+------+---------------+
|5802173.40|3581 |1620 |
+----------+------+---------------+(:city = 'All' OR city = :city)is the standard trick for an βAllβ option: when the user picks All, the first part is true for every row.- Parameters are passed as values, not pasted into the SQL text, so a city name like
'; DROP TABLE β¦is just a strange city with no rows. Never build SQL by joining strings. - The same
:namesyntax works in the SQL editor, notebooks, dashboards and alerts. The old{{ name }}style only works in the legacy editor.
Two more datasets for the dashboard: daily revenue for a line chart, and revenue per city and category for a bar chart.
-- One point per day: the line chart
SELECT order_date, SUM(revenue) AS revenue
FROM shopkart.gold.daily_sales
WHERE order_date BETWEEN :start_date AND :end_date
AND (:city = 'All' OR city = :city)
GROUP BY order_date
ORDER BY order_date+----------+----------+
|order_date|revenue |
+----------+----------+
|2026-11-01|1608692.80|
|2026-11-02|1211543.00|
|2026-11-03|1103659.00|
|2026-11-04|1275123.00|
|2026-11-05|2307754.00|
+----------+----------+
only showing top 5 rows-- Revenue per city and category: the stacked bar chart
SELECT city, category, SUM(revenue) AS revenue
FROM shopkart.gold.daily_sales
WHERE order_date BETWEEN :start_date AND :end_date
GROUP BY city, category
ORDER BY city, category+---------+-----------+----------+
|city |category |revenue |
+---------+-----------+----------+
|Bengaluru|Electronics|2453975.00|
|Bengaluru|Fashion |921924.50 |
|Bengaluru|Home |1354350.60|
|Delhi |Electronics|2756314.80|
|Delhi |Fashion |1017729.40|
|Delhi |Home |1442346.00|
|Kolkata |Electronics|2382798.90|
|Kolkata |Fashion |3162732.30|
|Kolkata |Home |1277652.40|
|Mumbai |Electronics|2126100.10|
|Mumbai |Fashion |972372.90 |
|Mumbai |Home |2843925.20|
|Pune |Electronics|2326762.30|
|Pune |Fashion |974105.90 |
|Pune |Home |2501305.20|
+---------+-----------+----------+There's Kolkata's secret: fashion (βΉ31.6 lakh, more than any other city's best category), while Mumbai and Pune love home goods.
Step 4: Answer real business questions
βHow did each week compare with the week before?β
-- Week over week: each full week's revenue next to the week before
WITH weekly AS (
SELECT date_trunc('WEEK', order_date) AS week_start, SUM(revenue) AS revenue
FROM shopkart.gold.daily_sales
GROUP BY 1
HAVING COUNT(DISTINCT order_date) = 7 -- skip part weeks at the start and end
)
SELECT CAST(week_start AS DATE) AS week_start,
revenue,
LAG(revenue) OVER (ORDER BY week_start) AS last_week,
ROUND(100 * (revenue - LAG(revenue) OVER (ORDER BY week_start))
/ LAG(revenue) OVER (ORDER BY week_start), 1) AS change_pct
FROM weekly
ORDER BY week_start+----------+-----------+-----------+----------+
|week_start|revenue |last_week |change_pct|
+----------+-----------+-----------+----------+
|2026-10-05|9165247.30 |NULL |NULL |
|2026-10-12|9097890.50 |9165247.30 |-0.7 |
|2026-10-19|9100002.40 |9097890.50 |0.0 |
|2026-10-26|9132322.30 |9100002.40 |0.4 |
|2026-11-02|13025769.00|9132322.30 |42.6 |
|2026-11-09|9217978.70 |13025769.00|-29.2 |
+----------+-----------+-----------+----------+LAG() reads the previous row's value (see window functions). Diwali week (2 November) jumped 42.6%, and the week after fell back to normal.
βWhat's the best-selling category in each city?β
-- The best-selling category in each city
WITH ranked AS (
SELECT city, category, SUM(revenue) AS revenue,
ROW_NUMBER() OVER (PARTITION BY city ORDER BY SUM(revenue) DESC) AS rank
FROM shopkart.gold.daily_sales
GROUP BY city, category
)
SELECT city, category, revenue
FROM ranked
WHERE rank = 1
ORDER BY revenue DESC+---------+-----------+----------+
|city |category |revenue |
+---------+-----------+----------+
|Kolkata |Fashion |7896390.60|
|Mumbai |Home |6756501.40|
|Pune |Home |6181899.60|
|Delhi |Electronics|6050940.60|
|Bengaluru|Electronics|5750531.50|
+---------+-----------+----------+Databricks SQL also has a shortcut for this pattern, QUALIFY, which filters on a window function without the extra CTE: QUALIFY ROW_NUMBER() OVER (β¦) = 1. We used the CTE version because it runs everywhere, including open-source Spark, which rejected QUALIFY in our test.
Step 5: Build the dashboard
AI/BI dashboards are Databricks' built-in dashboards. They have two parts: the Data tab, where you define datasets (queries), and canvas pages, where you place charts and filters. Here's how to build ShopKart's:
- New β Dashboard. On the Data tab, create three datasets and paste the
kpis,trendandby_cityqueries. Dataset queries are read-only:SELECTyes,CREATEorINSERTno. - Add visualizations on the canvas: three counters from
kpis(revenue, orders, average order value), a line chart fromtrend(order_date on the x-axis, revenue on the y-axis), and a bar chart fromby_city(city, revenue, coloured by category). - Add filters. A date-range filter and a city filter, each connected to the matching parameter. A parameter filter puts the chosen value into the query itself; a field filter just filters the rows a dataset already returned.
- Publish. Choose shared data permissions (the default: viewers see data through the publisher's access) or individual data permissions (each viewer runs queries with their own access, so Unity Catalog row filters apply per person).
- Schedule and subscribe. Add a schedule (say, 8 AM daily) to refresh the dashboard on a SQL warehouse; subscribers get a PDF snapshot by email after each scheduled run.
This is that dashboard, working, on the same 750 rows. Try Diwali week, then pick Kolkata:
ShopKart sales dashboard (working copy)
Revenue
βΉ2.85 Cr
Orders
18,094
Avg order value
βΉ1,576
Daily revenue
Revenue by city and category
-- dataset "kpis", with this run's parameters -- :start_date = '2026-11-01' :end_date = '2026-11-19' :city = 'All' SELECT SUM(revenue), SUM(orders), ROUND(SUM(revenue) / SUM(orders), 0) FROM shopkart.gold.daily_sales WHERE order_date BETWEEN :start_date AND :end_date AND (:city = 'All' OR city = :city)
Same 750 rows and same SQL as the lesson. Change a parameter and every dataset that uses it re-runs. The city bar chart's dataset has no :city parameter, so the city filter doesn't change it.
Step 6: Get told when something's wrong (alerts)
Nobody watches a dashboard all day. An alert runs a query on a schedule, checks a condition on the result, and notifies people (email, Slack, Teams and more) when it's true.
-- Alert query: yesterday's revenue (the alert fires if it's below a threshold)
SELECT SUM(revenue) AS revenue_yesterday
FROM shopkart.gold.daily_sales
WHERE order_date = date_sub(current_date(), 1)We ran it on 20 November 2026, so βyesterdayβ is 19 November:
+-----------------+
|revenue_yesterday|
+-----------------+
|1280509.00 |
+-----------------+| Alert setting | ShopKart's choice |
|---|---|
| Query | The one above, on a small serverless SQL warehouse |
| Condition | revenue_yesterday < 1000000 (βΉ10 lakh: an unusually bad day) |
| Schedule | Every day at 9:00 AM IST, after the morning pipeline finishes |
| Notify | The sales team's email and a Slack channel |
Yesterday's βΉ12.8 lakh is above the line, so no message today. Note that refreshing an alert by hand doesn't send notifications; only scheduled runs do. Alerts can also be a task inside a Lakeflow Job, so they run right after the pipeline that feeds them.
Step 7: Keep it fast and cheap
| Tool | What it does | Good to know |
|---|---|---|
| Query result cache | Re-running the same query on unchanged tables returns the saved result instantly | Never stale: any change to the table clears it. Entries last up to 24 hours |
| Dashboard cache | Dashboards keep results for up to 24 hours | Can show slightly older data; a scheduled refresh updates it |
| Materialized view | Stores a query's result as a table and refreshes it, incrementally when possible | Databricks only; needs a Pro or Serverless warehouse |
| Query history & profile | Every query run, with the time spent in each step | Start here when a dashboard is slow |
| Good gold tables | Small, pre-aggregated, well-clustered tables | Dashboards should read gold, not scan raw bronze |
A dashboard read by 200 people every morning is a good fit for a materialized view:
CREATE OR REPLACE MATERIALIZED VIEW shopkart.gold.city_daily_revenue
SCHEDULE EVERY 1 HOUR
AS SELECT order_date, city, SUM(orders) AS orders, SUM(revenue) AS revenue
FROM shopkart.gold.daily_sales
GROUP BY order_date, city;
-- Refresh now, without waiting for the schedule
REFRESH MATERIALIZED VIEW shopkart.gold.city_daily_revenue;Who can see what
Dashboards and Genie respect Unity Catalog. Give analysts read access to gold, and nothing else:
GRANT USE CATALOG ON CATALOG shopkart TO `analysts`;
GRANT USE SCHEMA ON SCHEMA shopkart.gold TO `analysts`;
GRANT SELECT ON TABLE shopkart.gold.daily_sales TO `analysts`;If different people must see different rows (each city manager sees only their city), use a row filter on the table and publish the dashboard with individual data permissions.
π― Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What's the difference between a SQL warehouse and an all-purpose cluster?Fresher+
Q2A dashboard is slow every morning. How do you investigate?1β3 yrs+
Q3How do you make one dashboard show each manager only their region?1β3 yrs+
Q4How would you design analytics serving for 500 business users?3+ yrs+
Key takeaways
- βSQL warehouses serve SQL to people and tools; serverless is the default choice.
- βNamed parameters make one query serve every date range and city, safely.
- βAI/BI dashboards are datasets plus charts plus filters, published with a permission model you choose.
- βAlerts and scheduled subscriptions bring the numbers to people instead of the other way round.
- βFast dashboards come from good gold tables, caching and materialized views.
Next: make the pipelines and queries behind these dashboards faster with Spark performance tuning.
Frequently asked questions
What is Databricks SQL?
Databricks SQL is the part of Databricks built for analysts and SQL users: a SQL editor, SQL warehouses (compute made for SQL), AI/BI dashboards, Genie Agents for questions in plain English, and alerts. It queries the same Delta tables in Unity Catalog that your pipelines write, so there's no copy into a separate warehouse.
What is the difference between a SQL warehouse and a cluster?
Both are compute. A cluster runs notebooks in Python, SQL, Scala or R and is used for data engineering and data science. A SQL warehouse runs only SQL and is tuned for many short queries from dashboards, the SQL editor and BI tools. Serverless SQL warehouses start in seconds and scale on their own.
Which SQL warehouse type should I choose?
Serverless when it's available in your region: it starts in a few seconds, scales quickly and includes Databricks' newest performance features. Pro is the fallback when serverless isn't available or you need custom networking. Classic is the most basic option.
How do parameters work in Databricks SQL?
Write :name in a query, for example WHERE city = :city. The SQL editor shows a box to fill in, and dashboards can connect a filter widget to it. Values are passed safely as parameters, not pasted into the SQL text. The old {{ name }} syntax only works in the legacy editor.
What are Genie Agents?
Genie Agents (called Genie spaces until July 2026) let business users ask questions about data in plain English. You choose the tables, add instructions and example queries, and Genie writes and runs the SQL. Published dashboards can also get their own Genie Agent built from the dashboard's datasets.
Can I use Power BI or Tableau with Databricks?
Yes. BI tools connect to a SQL warehouse with its connection details, and they see the same Unity Catalog tables and permissions. AI/BI dashboards are handy when you want the dashboard inside Databricks with no extra tool.
π§ Quick check: Databricks SQL
1. An analyst's dashboard should start quickly at 9 AM and scale when 50 people open it. Which compute?
2. Which is the right way to filter by a city the user picks?
3. The last week in a week-over-week report has only 4 days of data. What should the query do?
4. You publish a dashboard with "shared data permissions". Viewers' queries run with...
Checking your accessβ¦