Sub topic 7
Addressing data with catalog.schema.table
This chapter covers how Unity Catalog names objects and how the SQL editor resolves those names. It assumes you can write basic SELECT, WHERE, JOIN, and GROUP BY statements. It also assumes you have attached a SQL editor tab to a running warehouse, as covered in the earlier chapters on the editor.
The three-level namespace
Unity Catalog organizes data in three levels. Every table or view you query lives in a schema, and every schema lives in a catalog.
- A catalog is the top-level container. It usually groups data by environment, business unit, or team.
- A schema (also called a database) sits inside a catalog. It groups related tables, views, volumes, and functions.
- A table or view sits inside a schema and holds the rows you query.
A fully qualified name joins the three levels with dots: catalog.schema.table. In the sample data that ships with many workspaces, samples.nyctaxi.trips means the catalog samples, the schema nyctaxi, and the table trips.
Above the catalog sits the metastore, which is the Unity Catalog container for a region. You rarely write the metastore in a query. Your workspace is attached to one metastore, and the catalogs you can see come from it.
Exploring what exists
You can find names in two ways: the schema browser in the editor's left panel, or SQL commands. The commands work in any tab, so they are easy to put in a saved query.
SHOW CATALOGS;
SELECT * FROM samples.nyctaxi.trips LIMIT 10;SHOW CATALOGS lists only the catalogs your account can see. If a catalog you expect is missing, the likely cause is a missing privilege, not a missing catalog. The second statement reads a sample table by its full name.
Other discovery statements follow the same shape:
SHOW SCHEMAS IN samples;
SHOW TABLES IN samples.nyctaxi;
DESCRIBE TABLE samples.nyctaxi.trips;DESCRIBE TABLE returns the column names and types, which you need before writing a SELECT list. Use DESCRIBE TABLE EXTENDED to also see the table type, owner, storage location, and other metadata.
Partial names and defaults
You do not have to write all three parts every time. If you leave parts off, the warehouse fills them in from the current catalog and current schema of your session.
| You write | Resolved as |
|---|---|
| `samples.nyctaxi.trips` | Exactly that table |
| `nyctaxi.trips` | `<current catalog>.nyctaxi.trips` |
| `trips` | `<current catalog>.<current schema>.trips` |
You can inspect and change the defaults with these statements:
SELECT current_catalog(), current_schema();
USE CATALOG samples;
USE SCHEMA nyctaxi;
SELECT COUNT(*) FROM trips;The editor also has catalog and schema selectors near the run button. Choosing values there sets the same defaults for that tab. You can combine both settings in one statement with USE samples.nyctaxi.
Partial names are convenient for exploration. They are risky in saved queries, dashboards, and alerts, because the result depends on the session defaults of whoever runs the query. A teammate who has a different default schema may get an error, or worse, may silently read a different table with the same name.
Setting defaults is per session
USE CATALOG and USE SCHEMA last for the session of the editor tab. They do not change the workspace, the warehouse, or other users. Two things follow from that:
- If you run a multi-statement script, statements after a
USEline see the new defaults. - If you open a new tab or reconnect, you start again from the default catalog for your workspace, or from the value in the selector.
Many workspaces set a default catalog, often main or hive_metastore in older setups. If a query unexpectedly reads old data, check current_catalog() first.
Identifiers with special characters
Names that contain spaces, hyphens, or reserved words must be wrapped in backticks. Each part is quoted on its own.
SELECT *
FROM `my-catalog`.`sales data`.`order`
LIMIT 5;Do not quote the whole string as one unit. ` my-catalog.sales data.order is read as a single name with dots inside it, and it will not be found. Name matching for catalogs, schemas, and tables is case-insensitive, so Samples.NYCTaxi.Trips` finds the same table. Simple lowercase names with underscores avoid most of these problems.
Privileges and what a name lets you do
Writing a valid name does not mean you can read it. Unity Catalog checks privileges at each level.
| Privilege | Needed on | Purpose |
|---|---|---|
| `USE CATALOG` | The catalog | Lets you reference objects inside it |
| `USE SCHEMA` | The schema | Lets you reference objects inside it |
| `SELECT` | The table or view | Lets you read rows |
You need all three along the path to read a table. Having SELECT on a table is not enough if you lack USE SCHEMA on its parent schema. You can check what you hold with SHOW GRANTS ON TABLE samples.nyctaxi.trips, though you typically see only grants that apply to you or that you are allowed to administer.
Common errors
Most naming problems show up as one of a few messages.
TABLE_OR_VIEW_NOT_FOUND: the name does not resolve. Causes include a typo, a wrong default catalog or schema, or missing privileges that hide the object.SCHEMA_NOT_FOUNDorCATALOG_NOT_FOUND: the same cause at a higher level. RunSHOW SCHEMAS IN <catalog>to confirm.- A permission error such as
INSUFFICIENT_PERMISSIONS: the name resolves but you lack a privilege at one of the three levels. - A parse error near a dot or a keyword: a name needs backticks, often because it contains a hyphen or is a reserved word.
When a name fails, work from the top down. Run SHOW CATALOGS, then SHOW SCHEMAS IN <catalog>, then SHOW TABLES IN <catalog>.<schema>. The first command that does not show what you expect tells you which level has the problem.
Legacy names: hive_metastore
Older workspaces may also expose a catalog named hive_metastore. It holds tables registered in the legacy workspace-local Hive metastore, not in Unity Catalog. You address them the same way, as hive_metastore.default.my_table, but Unity Catalog governance features such as centralized grants and lineage do not apply to them in the same manner. If you see a two-part name in older notebooks, such as default.my_table, it was relying on a session default pointing at this catalog.
Names in views and queries that run elsewhere
A view stores a query text. If that text uses partial names, they can resolve differently depending on how the view was created, so create views with fully qualified names. The same rule applies to queries you later turn into dashboard datasets or alerts, and to statements you send through the Statement Execution API. Each statement there runs on a warehouse without your editor's tab state, although you can pass catalog and schema fields in the request body to set defaults. Fully qualified names make the statement independent of those fields.
curl -X POST "$DATABRICKS_HOST/api/2.0/sql/statements" \
-H "Authorization: Bearer $DATABRICKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"warehouse_id": "'"$WAREHOUSE_ID"'",
"statement": "SELECT COUNT(*) FROM samples.nyctaxi.trips",
"wait_timeout": "30s"
}'Practical habits
- Use fully qualified names in anything saved, shared, scheduled, or sent through an API.
- Use
USE CATALOGandUSE SCHEMAfor interactive exploration only. - Print
current_catalog()andcurrent_schema()when a result looks wrong. - Keep object names lowercase with underscores so you rarely need backticks.
- Read one level at a time when debugging a missing object.
Exercise
A colleague runs your saved query SELECT COUNT(*) FROM trips and gets TABLEORVIEWNOTFOUND, though it works for you. Give two likely causes and the fix for both.
Show answer
First, the colleague's session has a different current catalog or schema, so trips resolves to a different place. Second, the colleague lacks USE CATALOG, USE SCHEMA, or SELECT on the table, so the object is hidden or blocked. The fix for the first cause is to rewrite the query as SELECT COUNT(*) FROM samples.nyctaxi.trips. For the second, a user with the right authority grants the missing privileges. Running SHOW CATALOGS, SHOW SCHEMAS, and SHOW TABLES from the colleague's account shows which level fails.
Exercise
Write the correct fully qualified reference for a table named order in the schema sales data of the catalog my-catalog.
Show answer
my-catalog.sales data.order. Each part is wrapped in its own backticks, and the parts are joined by dots.