The Databricks UI includes a SQL editor that you can use to author queries, collaborate with colleagues, browse available data, and create visualizations. This page explains how to use the SQL editor to write, run, manage, and share queries.
This article explains how to use the new SQL editor. To learn about working with the legacy SQL editor, see Write queries and explore data in the legacy SQL editor.
Open the SQL editorâTo open the SQL editor in the Databricks UI, click SQL Editor in the sidebar.
The SQL editor opens to your last open query. If no query exists, or all of your queries have been explicitly closed, the SQL editor landing page opens.
Turn on the new SQL editorâUse the New SQL editor toggle, to the right of the catalog and schema drop-down selectors, to turn the new editor on. On narrow displays, you can find the New SQL editor toggle by clicking the kebab menu to the right of the catalog and schema selectors.
Enable the new editor for any query that you own:
The new SQL editor cannot be used for all queries. You cannot switch to the new SQL editor for a query if any of the following are true:
When you turn on the new SQL editor, your query reopens in the new UI. The result set is empty until you run the query.
note
Query results are shared with all collaborators and are limited to 64,000 rows.
OrientationâThis section highlights key differences in the UI and explains how to find and use available features.
You must have at least CAN USE permissions on a running SQL warehouse to run queries. You can use the drop-down near the top of the editor to see available options. To filter the list, enter text in the text box.
The first time you create a query, the list of available SQL warehouses appears alphabetically. The last used SQL warehouse is selected the next time you create a query.
The icon next to the SQL warehouse indicates the status:
note
If there are no SQL warehouses in the list, contact your workspace administrator.
The selected SQL warehouse will restart automatically when you run your query. See Start a SQL warehouse to learn other ways to start a SQL warehouse.
Browse assets and get helpâUse the left pane in the SQL editor to browse workspace files, view data objects, and get help from Databricks Assitant.
Browse workspace filesâClick the folder icon to open your workspace user folder. You can navigate across all of the workspace files you have access to from this part of the UI.
Browse data objectsâIf you have metadata read permission, the schema browser in the SQL editor shows the available databases and tables. You can also browse data objects from Catalog Explorer.
You can navigate Unity Catalog-governed database objects in Catalog Explorer without active compute. To explore data in the hive_metastore
and other catalogs not governed by Unity Catalog, you must attach to compute with appropriate privileges. See Data governance with Databricks.
note
If no data objects exist in the schema browser or Catalog Explorer, contact your workspace administrator.
Click near the top of the schema browser to refresh the schema. You can type in the search bar to filter assets by name. Click the filter icon to filter objects by type.
Click the name of an object in the browser to see more details on the object. For example, click a schema name to show the tables in that schema. Click a table name to show the columns in that table.
Get help from Databricks AssistantâClick the Assistant icon to open a chat window with Databricks Assistant. Click a suggested question or enter your own question to interact with Databricks Assistant.
Create a queryâYou can enter text to create a query in the SQL editor. You can insert elements from the schema browser to reference catalogs and tables.
Type your query in the SQL editor.
The SQL editor supports autocomplete. As you type, autocomplete suggests completions. For example, if a valid completion at the cursor location is a column, autocomplete suggests a column name. If you type select * from table_name as t where t.
, autocomplete recognizes that t
is an alias for table_name
and suggests the columns inside table_name
. You can also use autocomplete to reference query snippets.
(Optional) When you are done editing, click Save. By default, the query is saved to your user home folder. To save the query to a different location, select the target folder and click Move.
note
New queries are automatically named New query with the creation timestamp appended in the title. By default, new queries created without a specific folder context are created in the Drafts folder in your home directory. When new queries are saved or renamed, they are removed from Drafts.
Query data sourcesâYou can identify a query source using a fully-qualified table name in the query itself or by selecting a combination of catalog and schema from the drop-down selectors along with the table name in the query. A fully-qualified table name in the query overrides the catalog and schema selectors in the SQL editor. If a table or column name includes spaces, wrap those identifiers in backticks in your SQL queries.
The following examples demonstrate how to query various table-like objects that you can store in a catalog.
Query a standard table or viewâThe following example queries a table from the samples
catalog.
SQL
SELECT
o_orderdate,
o_orderkey,
o_custkey,
o_totalprice,
o_shippriority
FROM
samples.tpch.orders
Query a metric viewâ
The following example queries a metric view that uses a table from the samples catalog as its source. It evaluates the three listed measures and aggregates over Order Month
and Order Status
. It returns results sorted by Order Month
. To create a similiar metric view in your workspace, see Create a metric view.
All measure evaluations must be wrapped in the MEASURE
function. See measure
aggregate function.
SQL
SELECT
`Order Month`,
`Order Status`,
MEASURE(`Order Count`),
MEASURE(`Total Revenue`),
MEASURE(`Total Revenue per Customer`)
FROM
orders_metric_view
GROUP BY ALL
ORDER BY 1 ASC;
Optimize a query with Databricks Assistantâ
Click the Assistant icon on the right side of the editor to get inline help and suggestions when writing queries. The /optimize
slash command prompts Assistant to evaluate and optimize queries. For more information, see Optimize Python, PySpark, and SQL code.
By default, the SQL editor uses tabs so you can open and edit multiple queries simultaneously. To open a new tab, click +, then select Create new query or Open existing query. Click Open existing query to see a list of queries. The For you tab offeres a curated list of sugestions based on your usage. Use the All tab to find any query that you have access to.
Run a single query or multi-statement queriesâYou can write multi-statement queries into a single editor tab. To use multiple query statements in a single tab, end each statement with a semicolon (;
).
To run a single query:
note
By default, Run all statements and Limit 1000 are selected.
LIMIT
clause in your query. The maximum number of rows returned in a table is 64,000.To terminate a query while it is running, click Cancel. An administrator can stop an running query that another user started by viewing the Terminate an executing query.
The new SQL editor handles permissions in two parts:
Interactive execution: Queries in the new SQL editor always run using Run as viewer credentials. Query results are accessible to anyone who has access to the query. Users with CAN EDIT permissions or higher can modify the shared query draft. Additionally, unsaved edits are visible to anyone with CAN VIEW permissions or above. Users must have at least CAN USE permission on the associated warehouse to run queries.
Credential settings: The permission settings for legacy alerts, Workflows, and legacy dashboards are the same as those for the legacy SQL editor. Depending on your preferences, these assets can be run using either the owner's or the viewer's credentials.
See Configure query permissions to learn about Run as owner and Run as viewer credential settings. See Query ACLs to learn how to manage access to the query object.
Collaborate on queries in real timeâAfter sharing the query, you can coauthor or debug queries live through collaborative editing. Editors can also comment on individual lines of code to ask questions or add suggestions.
Source control a queryâDatabricks SQL query files (extension: .dbquery.ipynb
) are supported in Databricks Git folders. You can use a Git folder to source control your query files and to share them in other workspaces with Git folders that access the same Git repository. If you choose to opt out of the new SQL editor after committing or cloning a query in a Databricks Git folder, delete and reclone that Git folder to avoid unexpected behaviors.
Query content in the new SQL editor is continuously autosaved. The Save button controls whether the draft query content should be applied to related assets, like legacy dashboards, workflows, or legacy alerts. If the query is shared with the Run as owner credential, only the query owner can use the Save button to propagate changes. If the credential is set to Run as viewer, any user with at least CAN MANAGE
permission can save the query.
You can compare the output from each statement with multiple result statements. When you run multiple SQL statements from a single edit window, you can use the UI in the Output panel near the bottom of the screen to scroll through the different result sets.
You can interact with and explore your query results using the result pane. The result pane includes the following features for exploring results.
VisualizationsâClick the to add a visualization. Visualizations can help explore the result set. See Visualization types for a complete list of available visualization types.
Click the on a visualization tab to show additional options.
Click Edit to edit the visualization.
Click Add to dashboard to copy the query and visualization to a new AI/BI dashboard.
Select Create new dashboard or Add to existing dashboard.
You can download results as a CSV or Excel file. CSV downloads support up to approximately 5GB of data. Click the in a results tab to view download options. The final file download size might be slightly more or less than 5GB, as the 5GB limit is applied to an earlier step than the final file download.
note
If you cannot download a query, your workspace administrator has disabled download for your workspace.
FiltersâFilters allow you to limit the result set after a query has run. You can apply filters to selectively show different subsets of the data. Use the Assistant to help you add a filter for results.
To add a filter:
To edit a filter:
Parameters allow you to limit the result set by substituting values into a query at runtime. See Work with query parameters to learn how to apply parameters.
Parameter syntax changesâQueries written in the legacy SQL editor that use double curly braces ({{}}) to mark parameters must be modified to run in the new SQL editor. The new SQL editor uses the same parameter syntax as other Databricks tools, like AI/BI dashboards, AI/BI Genie, and notebooks.
Parameters in the new SQL editor are defined using named parameter syntax. Named parameters are parameters in a query that are prefixed with a colon, for example :parameter_name
.
See Named parameter markers for the SQL referenece documentation. See Named parameter syntax examples for examples of converting parameter syntax from the classic SQL editor to named parameter markers.
Supported filter typesâThe following table shows filter types supported in the new SQL editor. The second column indicates whether the filter also works in the legacy SQL editor.
Use keyboard shortcutsâThe new SQL editor includes many built-in keyboard shortcuts to help you work quickly. The following table identifies commonly used keyboard shortcuts.
Disable the new SQL editorâTo disable the new SQL editor, use the following steps:
Workspace admins can turn off this feature at the workspace level by visiting the preview portal and searching for SQL editor. See Manage Databricks Previews.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4