Sub topic 11
Saving, organizing, and sharing queries
This chapter assumes you can write and run a query in the SQL editor and that you know what a SQL warehouse is. It covers how to keep a query, where it lives, who can open it, and what sharing does and does not grant.
What saving does
A new editor tab holds an unsaved draft. When you save it, Databricks stores the query as a workspace object with a name, an owner, and a location. Saved queries keep their text, their parameters, and their visualizations. They do not keep result data. Each person who runs a saved query gets a fresh run on a warehouse.
Give each query a clear name, such as taxi_trips_by_zip, so that colleagues can find it in search.
Organizing queries in folders
Saved queries sit in the workspace file browser, so you can group them in folders.
- Your home folder is private by default.
- A shared folder suits team queries and lets the team manage access in one place.
- Folder permissions are inherited by the objects inside, so moving a query into a shared folder changes who can see it.
A simple convention helps: one folder per project, with queries named for the question they answer.
Sharing and permissions
Sharing is a permission on the query object. The usual levels are:
| Permission | What the person can do |
|---|---|
| Can view | Open and read the query and run it |
| Can run | Run the query without editing it |
| Can edit | Change the query text and settings |
| Can manage | Edit, share, rename, and delete it |
Exact level names can vary by workspace version, so check the Share dialog.
Share with groups rather than individuals where you can. When someone joins or leaves the team, you then update group membership once instead of editing every query.
Two permissions are involved
Running a query needs access to two things, and a query permission covers only one of them.
- The query, which controls who can open it.
- The compute and data, which means permission to use the warehouse, plus Unity Catalog privileges on the tables the query reads.
If a colleague can open your query but gets an error on samples.nyctaxi.trips, they lack data access. If they cannot pick a warehouse, they lack CAN USE on it. Sharing the query never grants either one.
Common errors
- Query runs for you but fails for a teammate. Check their table privileges first, then warehouse access.
- Query is shared but cannot be found. It may be in your private home folder. Share the folder or move the query.
- Edits you did not expect. Anyone with Can edit changes the same shared copy. Grant Can run to people who only need results.
- Departing owner. Queries owned by a deleted user can become hard to manage. Transfer ownership before an account is removed.
Reusing a saved query
Parameters make a saved query reusable. The query below keeps one definition and lets each person choose a distance:
SELECT
pickup_zip,
trip_distance,
fare_amount
FROM samples.nyctaxi.trips
WHERE trip_distance > :min_distance
ORDER BY fare_amount DESC
LIMIT 50;Save it once, share it with a group at Can run, and everyone supplies their own min_distance.
Exercise
A colleague has Can run on your saved query, but it fails with a permission error on the table. What should you check, and does raising their query permission to Can edit fix it?
Show answer
Check their Unity Catalog privileges on the table (and that they have CAN USE on the warehouse). Raising the query permission to Can edit does not fix it, because query permissions control access to the query object, not to the data or the compute.