Sub topic 11
Saving, organizing, and sharing queries
This chapter assumes you can write and run a query in the SQL editor, that you know the catalog.schema.table naming from earlier chapters, and that you have used named parameters such as :min_distance. Here you will turn a scratch query into a reusable asset. You will save it, place it in a folder, control who can open and edit it, and understand why a shared query can still fail for a colleague.
Menu labels differ slightly between workspace releases. The concepts below stay the same.
What a saved query is
An unsaved editor tab is a draft. It lives in your session and is easy to lose track of. When you save it, Databricks stores the query as a workspace object with these properties:
- A name and an owner (the creator).
- A location in the workspace file tree, such as your home folder or a shared folder.
- An access control list that says who can view, run, edit, or manage it.
- The query text, the parameter definitions, and the default warehouse selection.
A saved query does not store results as a durable table. It stores the text that produces them. Anyone who runs it executes it again on a warehouse, subject to their own permissions.
-- Top fares for long trips. Owner: analytics team.
-- Parameter: :min_distance (numeric, default 5)
SELECT
pickup_zip,
trip_distance,
fare_amount
FROM samples.nyctaxi.trips
WHERE trip_distance > :min_distance
ORDER BY fare_amount DESC
LIMIT 50;The comment header is worth the habit. It records intent and the parameter contract in the query itself, where the next reader will see it.
Saving and naming
Click Save in the editor and give the query a name. Pick a name that describes the output, not the action. Top fares by trip distance is easier to find than query 3. A consistent convention helps once a team has dozens of queries:
- Start with the subject area:
Taxi - Top fares by trip distance. - Avoid dates in the name unless the query is truly time-bound.
- Put the audience or owner in the folder path instead of the name.
Saving again overwrites the stored text. The editor keeps a version history, so you can compare earlier versions and restore one if an edit goes wrong. Use it before you rewrite a query other people depend on.
Organizing with folders
Saved queries appear in the workspace browser alongside notebooks and dashboards. You have three main places to put them:
| Location | Who sees it by default | Typical use |
|---|---|---|
| Your home folder | Only you | Drafts and personal exploration |
| A shared folder | Whoever has access to the folder | Team-owned, reviewed queries |
| Trash | Only you, until it is purged | Recovering deleted queries |
Folders carry permissions that child objects inherit. Giving a group access to a folder gives that group access to everything inside it, unless an object has its own more specific settings. This is usually easier to maintain than sharing each query one by one.
The Favorites star is a personal shortcut that does not change permissions. Use it for the handful of queries you open daily.
Deleted queries go to Trash first and can be restored for a limited time. Do not rely on Trash as a backup. If a query matters, keep its text in version control as well.
Sharing and permission levels
Use Share to grant access to users, groups, or service principals. Prefer groups. When someone changes teams, you update group membership once, instead of editing every query.
Query permissions come in four levels. Each level includes the abilities of the one above it.
| Level | Can open and read the SQL | Can run | Can edit | Can change permissions or delete |
|---|---|---|---|---|
| Can View | Yes | No | No | No |
| Can Run | Yes | Yes | No | No |
| Can Edit | Yes | Yes | Yes | No |
| Can Manage | Yes | Yes | Yes | Yes |
The owner always has full control. Workspace admins can also manage any object. If the owner leaves the organization, an admin or someone with Can Manage should take over ownership, so the query does not become orphaned.
Grant the lowest level that does the job. Most consumers need Can Run. Give Can Edit only to people who maintain the logic, and keep Can Manage to a small set of owners.
Why a shared query can still fail
Sharing a query gives access to the query object. It does not give access to the compute or the data. Three independent checks apply when someone runs it:
- Query permission. Can the user open and run this saved query?
- Warehouse permission. Does the user have Can Use on the warehouse that will execute it?
- Data permission. Does the user have the Unity Catalog privileges (
USE CATALOG,USE SCHEMA,SELECT) on every table the query reads?
The usual symptom is that the query opens fine, then fails on execution with a permission error on a table. Fix it by granting the data privilege to the group, not by giving the user more query permissions:
GRANT USE CATALOG ON CATALOG samples TO `analysts`;
GRANT USE SCHEMA ON SCHEMA samples.nyctaxi TO `analysts`;
GRANT SELECT ON TABLE samples.nyctaxi.trips TO `analysts`;The samples catalog is read-only and managed by Databricks, so you may not be able to run these grants on it. Treat them as a template for your own catalogs.
Whose credentials run the query
When a colleague runs your saved query, the data access check uses an identity. For ad hoc runs in the editor, that identity is the person who clicked Run, so each user sees only what they are entitled to. Dashboards that you publish behave differently. You can choose whether viewers run with their own permissions or with the publisher's embedded credentials. Embedding credentials lets people see results from tables they cannot query directly. Use it deliberately, and only for data you are comfortable exposing to every viewer.
Scheduled runs, such as those that feed an alert, execute under the schedule's owner. If that person loses access to a table, the schedule starts failing. For production schedules, a service principal as owner is more stable than an individual.
Parameters in shared queries
Parameter definitions are saved with the query. When someone with Can Run opens it, they see the input field and can supply their own value without editing the SQL. Their value applies to their session only. It does not overwrite your saved default.
A change to the parameter's name, type, or the SQL that uses it affects every consumer. Before you rename :min_distance, check what depends on it, such as dashboard widgets and alerts, and update those at the same time.
Practical habits for teams
- Keep production queries in a shared folder owned by a group or service principal, not in a personal home folder.
- Put a comment header on every shared query that states its purpose and parameters.
- Use full three-level names. A query that depends on a default catalog or schema may behave differently for another user or warehouse.
- Review the version history before editing a query that feeds a dashboard.
- Remove access when a project ends. Stale grants are a common audit finding.
Exercise
You save a query in your home folder and share it with the group finance-analysts at Can Run. A group member opens it and gets "Insufficient privileges" on main.sales.orders. What is wrong, and how do you fix it without raising their query permission?
Show answer
The query-level permission is fine, because they could open and run it. The failure is at the data layer: the group lacks Unity Catalog privileges on the table. Grant USE CATALOG on main, USE SCHEMA on main.sales, and SELECT on main.sales.orders to finance-analysts. Also confirm that the group has Can Use on the warehouse. Raising the query permission to Can Edit or Can Manage would not help.
Exercise
A team lead wants a query to be runnable by 30 analysts but editable only by two maintainers. Which permissions should be set, and where should the query live?
Show answer
Grant the analyst group Can Run and the two maintainers (or a maintainers group) Can Edit, or Can Manage for whoever may change permissions. Put the query in a shared folder owned by the team or a service principal, not in a personal home folder, so access survives personnel changes.