Skip to content

SQL block

A SQL block on a doc page lets you edit and run SQL, view run results, view column statistics, and create charts. Add one from a doc page by typing / and selecting SQL block.

Key features

References

In the SQL editor, type / to reference the following.

Reference targetInsert exampleDescription
Another SQL block/sql/Expands the referenced SQL as a subquery.
Table/table/Lets you select a table from the catalog.
Parameter/param/Embeds a parameter's value.
Saved query/query/Embeds the SQL at the time of insertion. When the source is updated, select Update embedded query to apply the change. See Query for details.

You can also see reference relationships in the notebook's dependency diagram. Open Show dependencies from a SQL block's menu.

Referencing SQL blocks

When you reference another SQL block, the referenced SQL is expanded as a subquery at run time. This lets you factor out and reuse common extraction logic, or build up a query while checking intermediate results block by block.

  • You can reference SQL blocks on the same page, as well as SQL blocks on other doc pages within the same notebook.
  • References to other pages are shown as Page name.SQL block name.
  • Circular references result in an error.

Inserting

  1. In the SQL editor, type /sql/, or choose Reference another SQL block's SQL from the / suggestions.
  2. Select the SQL block you want to reference.

Expansion settings

Hovering over a reference chip shows a popup where Settings lets you change the following.

ItemDescription
Don't wrap in parenthesesOff by default. When off, the expanded result is wrapped in ( ). Turning it on omits the wrapping.
Add alias at the endOnly selectable when Don't wrap in parentheses is off. Appends AS and the SQL block name at the end of the expanded result.

Expanding and overriding parameters

If the referenced SQL block contains a parameter reference, the value used for expansion is as follows.

Parameter used by the reference targetValue used for expansion
Notebook-wideThe value held by the referencing source (the page containing this SQL block).
Page-specificIf the referencing page has no definition with the same name, the value from the reference target's page.

If you want to decouple the parameter dependency on the referencing page, use Override internal parameter references in the reference chip's popup.

MethodDescription
Override with another parameterReplaces the value with another parameter of the same type.
Override with a fixed valueSpecifies a fixed value.
Don't overrideExpands according to the rules above.

You can override the same way when referencing from a grid page's SQL run result or chart. See Overriding parameters for details.

Adding a chart

Once SQL runs successfully, select Chart in the run result header to open the chart wizard. Configure dimensions, metrics, and so on to add a chart directly below the run result.

  • See Chart for details on chart types and settings.
  • You can also add a chart from Explorer.
  • An added chart can also be placed on a grid page using Add to grid page.
  • You can also insert a Block chart via / to reference the run result of a SQL block on the same page (see Doc page).

Adding column stats

Select Column stats in the run result header to add the distribution and summary statistics of a result column. Selecting a target column issues a separate SQL query and adds a column stats block.

The items shown vary depending on the data type.

ItemDescription
Data typeThe column's data type.
Number of NULL valuesThe count and percentage of NULLs.
Unique count*The number of unique values.
Max / Min / AverageShown for numeric, time, and similar types.
Percentiles*The 25th, 50th, and 75th percentiles.
Character length*The distribution of string lengths.
Frequency*The most frequently occurring values.
Histogram*The distribution for numeric, time, and similar types.

Values marked with * are calculated using an approximation function.

Available connections are BigQuery, Snowflake, and Databricks. Not available for Redshift.

If the SQL content or parameters change, you need to re-run the stats.

Display mode

The icon at the top left of a SQL block lets you switch the display mode. This switch isn't synced to other users. You can change the default setting from the SQL block menu (horizontal three-dot icon) > Default display.

ModeDescription
Show allShows SQL, the run result, and the chart.
Show result onlyShows the run result and the chart.
Hide allHides SQL, the run result, and the chart.

Save SQL

Use Save SQL in the SQL block's top-right menu to register the block's content as a saved query.

  • References to other SQL blocks or saved queries are saved fully expanded.
  • Parameters are saved with their value at that point substituted in.

Parameter embedding and references to other queries aren't available on the saved query side.

Code snippets

Use Manage code snippets in the SQL editor to create and edit snippets for autocomplete. Code snippet autocomplete is enabled by default. You can disable it by turning off Enable code snippet autocomplete in SQL editor preferences.

ItemDescription
Personal snippet / Shared snippetChoose whether it's personal (per user) or shared across the workspace.
Autocomplete keyTyping this key lets you call up the snippet from autocomplete suggestions.
Code snippet to insertThe text inserted when the suggestion is selected. You can specify the cursor position after insertion with $$.
Display name / descriptionOptional.

Cache

A SQL block's run result is cached for display purposes. This reuses the past successful result of the same SQL; the job itself is retained for 24 hours by default.

  • Up to 1,000 rows of run result are retained for display.
  • When editing a notebook, running a new job is skipped by default if the cache is within 24 hours, and the cache is used instead.
  • To run without using the cache, use a SQL block's Run with latest data, or the notebook's Run all with latest data.

The handling differs by access path, as follows.

PathDisplay cacheOverriding the reuse period
Editing a notebook24 hours by default. Can be bypassed with Run with latest data / Run all with latest data.None.
Viewing a reportSame as above.Report settings' Cache duration (24 hours by default; 0 disables it; maximum 24 hours, in 10-minute increments).
Signed embedSame as above.cache_max_age at token issuance (86400 seconds by default and at maximum; 0 disables it). See Signed embed for details.

Warehouse-side caching (such as the DWH's native cache for identical SQL) can't be controlled from the connection settings.

Constraints

Column suggestions and similar are limited to references to tables or other SQL blocks. They aren't suggested for string table IDs or saved query references.