Sub topic 11
Querying with Databricks SQL
This chapter assumes you know basic SQL, and that you have read the chapters on compute and Unity Catalog. Databricks SQL is the part of the platform for running SQL queries and building dashboards. It runs on SQL warehouses instead of the clusters that notebooks use.
The working model
A SQL warehouse is compute sized for SQL workloads. You pick a T-shirt size, such as Small or Large, and Databricks maps it to a set of resources. Warehouses can be serverless, where compute runs in Databricks' account and starts in seconds. They can also be pro or classic, where compute runs in your cloud account.
You write queries in the SQL editor and attach them to a warehouse. The editor shows results in a table and lets you save queries, add visualizations, and schedule refreshes. Every table is addressed by its Unity Catalog three-level name, catalog.schema.table.
Running a query
Use the table from the earlier chapters. Select the warehouse in the editor, then run:
SELECT
customer_id,
COUNT(*) AS order_count,
SUM(amount) AS total_amount
FROM main.tutorial.orders
GROUP BY customer_id
ORDER BY total_amount DESC;The warehouse reads the Delta table, applies the aggregation, and returns the rows. This is the same result the PySpark example produced, so you can pick SQL or Python by preference.
You can also set a default catalog and schema so that short names work:
USE CATALOG main;
USE SCHEMA tutorial;
SELECT * FROM orders LIMIT 10;Warehouse behavior to know
- Auto stop. An idle warehouse shuts down after a set number of minutes, which stops charges. The next query waits while it starts. Serverless warehouses start much faster than the other types.
- Scaling. A warehouse can add clusters when many users query at once. This handles concurrency. It does not make a single query faster.
- Permissions. The user needs
CAN USEon the warehouse, plusUSE CATALOG,USE SCHEMA, andSELECTon the data.
Common errors
| Symptom | Likely cause |
|---|---|
| `TABLE_OR_VIEW_NOT_FOUND` | Wrong catalog or schema, or no `USE` privilege on a parent |
| Query waits a long time before running | Warehouse was stopped and is starting |
| Permission denied on a table you can see | Missing `USE SCHEMA` or `USE CATALOG` |
| Slow query on a large table | Warehouse too small, or the query scans more data than it needs |
Warehouse or notebook compute?
Use a SQL warehouse for analyst queries, dashboards, and BI tools that connect over JDBC or ODBC. Use all-purpose or job compute when you need Python or Scala logic, machine learning libraries, or custom processing. Both read the same governed tables.
Exercise
An analyst has SELECT on main.tutorial.orders but gets a permission error when running a query against it from a SQL warehouse. Name two things you would check.
Show answer
Check that the analyst's group has USE CATALOG on main and USE SCHEMA on main.tutorial, because SELECT alone is not enough. Also check that the analyst has CAN USE permission on the SQL warehouse itself.