Sub topic 6
Getting around the SQL editor
The SQL editor is the web page where you write, run, and save queries against a SQL warehouse. This chapter covers the editor's layout and the workflow you repeat all day: attach compute, pick a default location, write a statement, run it, and read the result. It assumes you can write basic SQL (SELECT, WHERE, JOIN, GROUP BY) and have used the Databricks workspace sidebar. It also assumes you know what a SQL warehouse is, from earlier chapters. The next chapters cover Unity Catalog names, result handling, and parameters in more depth.
Opening the editor
Select SQL Editor in the workspace sidebar. You need two things before a query can run:
- A warehouse you can use. You need at least the Can use permission on it.
- Access to the data. Unity Catalog privileges on the catalog, schema, and tables decide what you can read. The warehouse does not grant data access.
If the sidebar does not show SQL Editor, your account probably lacks Databricks SQL access. A workspace admin controls that entitlement.
The main areas of the editor
The editor has the same few areas in every workspace. Exact labels and positions change between releases.
| Area | What it does |
|---|---|
| Left panel | Switches between the schema browser, saved queries, and query history. |
| Tab bar | Holds one tab per query. A tab keeps its own text, warehouse, and results. |
| Compute selector | Chooses which SQL warehouse runs the tab's statements. |
| Catalog and schema selector | Sets the default catalog and schema for unqualified names. |
| Editing pane | The text area with syntax highlighting and autocomplete. |
| Run button | Runs the selection or the statement at the cursor. A menu offers other run options. |
| Results pane | Shows the result table, visualizations, messages, and a link to the query profile. |
Tabs are independent sessions
Each tab has its own warehouse and default catalog and schema. Two tabs can run on different warehouses at the same time. This is useful for comparing a small warehouse with a large one, but it also means a stale setting in one tab can surprise you. Check the selectors in the tab you are working in before you run anything.
A new tab is an unsaved draft. Saving it creates a query object that you can organize and share, as described in a later chapter.
Attaching a warehouse
Choose a warehouse from the compute selector. The list shows each warehouse's state, such as Running, Starting, or Stopped.
If you run a query on a stopped warehouse with auto stop enabled, the warehouse starts automatically. The query waits for startup. A serverless warehouse typically starts in seconds. Pro and classic warehouses take longer, usually a few minutes. Startup time is the usual reason the first query of the day is slow while later ones are fast.
Setting a default catalog and schema
Unity Catalog uses a three-level name: catalog.schema.table. The catalog and schema selector sets the defaults, so a name like trips resolves to <default catalog>.<default schema>.trips. A fully qualified name always works regardless of the selector, so it is the safer choice in queries you plan to share.
You can also set the defaults in SQL:
USE CATALOG samples;
USE SCHEMA nyctaxi;
SELECT * FROM trips LIMIT 10;The USE statements affect the current session for that tab. If a colleague opens your saved query, they get their own tab and selector values, so an unqualified name may resolve to something different or fail. That is one more reason to use fully qualified names in saved queries.
Using the schema browser
The schema browser lists the catalogs you can see, then schemas, then tables and views. Use it to:
- Expand a table to see column names and types.
- Open a table's details page to see sample data, owner, and permissions.
- Insert a name into the editor, so you do not mistype it.
The browser only lists objects you have privileges to see. If a table you expect is missing, check your grants first. Do not assume it was dropped.
To confirm access from the editor, run this:
SHOW CATALOGS;
SELECT * FROM samples.nyctaxi.trips LIMIT 10;The first statement lists the catalogs visible to you. The second reads ten rows from a sample table that Databricks provides in the samples catalog, using the full three-level name.
Writing queries
The editor offers help while you type:
- Autocomplete suggests keywords, functions, and objects from Unity Catalog. It reads metadata for the catalog and schema you have selected, so it works best when you set them.
- Syntax highlighting and bracket matching catch unbalanced parentheses and quotes.
- Comments:
--starts a line comment and/* ... */a block comment. The editor also has a keyboard shortcut to toggle comments on selected lines. - Formatting: a format command rewraps and indents the query text.
- Databricks Assistant, if your workspace has it enabled, can draft or explain SQL from a prompt. Treat its output as a draft and read it before you run it.
The editor lists its keyboard shortcuts in a help dialog. Check it, because shortcuts differ between editor versions and operating systems. Run is normally Ctrl+Enter on Windows and Linux and Cmd+Enter on macOS.
Running a selection, a statement, or everything
You can keep several statements in one tab. What runs depends on your cursor and selection:
- Text selected: only the selected text runs.
- Nothing selected: the statement at the cursor runs. Statements are separated by semicolons.
- Run all: a menu option next to the Run button executes every statement in the tab in order.
If you select part of a statement by accident, you run a fragment, and you get a syntax error that looks unrelated to your query. When a query fails with an odd parse error, check whether something is highlighted.
Separate statements with semicolons. A missing semicolon can make the editor treat two statements as one.
The row limit
The editor can apply a row limit to queries it runs, commonly 1,000 rows. The limit is a protection for the browser. It does not change the query semantics on the warehouse. If your result shows exactly 1,000 rows, check whether the limit is on before you conclude that is all the data there is. Aggregates such as COUNT(*) are unaffected, because they return a single row.
The first-query workflow
The loop in the diagram is the one you repeat in every session.
Work through it once with a real query. This one aggregates the sample taxi data:
SELECT
pickup_zip,
COUNT(*) AS trips,
ROUND(AVG(fare_amount), 2) AS avg_fare
FROM samples.nyctaxi.trips
GROUP BY pickup_zip
ORDER BY trips DESC
LIMIT 20;After it runs, look at three things in the results pane: the row count, the execution time, and any messages. The time shown covers the whole statement, including time spent waiting for a warehouse to start or for a cluster to free up. Later chapters show how to separate those from actual execution time using the query profile.
Common errors
| Symptom | Likely cause | What to check |
|---|---|---|
| `TABLE_OR_VIEW_NOT_FOUND` | Wrong default catalog or schema, a typo, or missing privileges | The selector values, then the full name, then your grants |
| Syntax error near an odd token | A partial selection ran | Click in the editor to clear the highlight |
| Query seems to hang at the start | The warehouse is starting | The warehouse state in the compute selector |
| Query waits though the warehouse is Running | The warehouse is busy and queries are queued | Query history and the warehouse monitoring tab |
| Result stops at 1,000 rows | The editor row limit is on | The limit setting beside Run |
| Query works for you but not a colleague | Unqualified names or different privileges | Use full names and compare grants |
Exercise
A colleague opens your saved query that selects from trips, with no catalog or schema in the name, and gets TABLEORVIEWNOTFOUND. In your tab it ran fine. What is the most likely cause, and how do you fix it?
Show answer
Your tab had a default catalog and schema set (through the selector or USE CATALOG and USE SCHEMA), so trips resolved correctly. Your colleague's tab has different defaults, so the name does not resolve. Fix it by using the fully qualified name, for example samples.nyctaxi.trips. If the error persists, check that your colleague has privileges on that table.
Exercise
You have three statements in one tab and you press Run with the cursor inside the second statement and nothing selected. Which statements run? Which option runs all three?
Show answer
Only the second statement runs, because with no selection the editor runs the statement at the cursor. To run all three in order, use the Run all option in the menu next to the Run button.