Sub topic 6
Getting around the SQL editor
This chapter assumes you know basic SQL and can find your way around the Databricks workspace. It also assumes you have a SQL warehouse to run on, which earlier chapters covered. Here you learn the layout of the SQL editor and the workflow for writing and running a first query.
What the SQL editor is
The SQL editor is the workspace page where you write SQL, run it on a SQL warehouse, and look at the results. It does not run queries itself. It sends each statement to the warehouse you have selected, so no query runs until a warehouse is attached.
The layout
The editor has four main areas:
- Schema browser (left side): a tree of catalogs, schemas, and tables you can access. Expand a table to see its columns and types. Clicking a name inserts it into your query.
- Query pane (center): where you type SQL. It offers autocomplete for catalog, schema, table, and column names.
- Warehouse selector (top right): shows which warehouse will run the query, and whether it is running or stopped.
- Results pane (below the query): shows the result table, any visualizations, and messages such as errors.
A first workflow
Follow these steps for a first query.
- Open the SQL editor and create a new query.
- Pick a warehouse in the selector. If it is stopped, it starts when you run a query, which can add a short delay.
- Check which catalogs you can see.
- Read a few rows from a sample table.
Paste this into the query pane:
SHOW CATALOGS;
SELECT * FROM samples.nyctaxi.trips LIMIT 10;Press the Run button or use the keyboard shortcut Ctrl+Enter (Cmd+Enter on macOS). The results pane shows the output of the statement that ran. The table name samples.nyctaxi.trips is a full three-level name, which the next chapter explains.
Running part of a script
The query pane can hold several statements separated by semicolons. By default, Run executes the whole text. If you select some text first, only the selection runs. Use this to test one statement at a time without deleting the others.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Run button is disabled or the query will not start | No warehouse is selected | Choose a warehouse in the selector |
| Table or view not found | The name is not fully qualified, or you lack access | Use `catalog.schema.table` and check the schema browser |
| Long wait before results | The warehouse was stopped and is starting | Wait for startup, or use a warehouse that starts faster |
| Permission denied | You lack `CAN USE` on the warehouse or access to the data | Ask an administrator for access |
Exercise
You select part of a script that contains three statements and press Run. What runs, and why is that useful?
Show answer
Only the selected text runs. This lets you test one statement at a time without removing the others from the editor.
Exercise
A new query will not run, and the warehouse selector is empty. What should you check first?
Show answer
Select a warehouse. The editor sends statements to a warehouse, so nothing runs until one is attached. If none is listed, you may lack the CAN USE permission.