SQLNB Format Specification
View RawVersion 0.0.1
Overview
SQLNB is a SQL notebook format: plain .sql files with configuration in comments. Cells are delimited by semicolons.
Cell Structure
A notebook is a sequence of cells. Each cell is SQL terminated by a semicolon.
select 1;
select 2;
This produces two cells. Blank lines between cells are ignored.
Notebook Configuration
Configuration at the top of the file, before any SQL. A blank line separates notebook configuration from the first cell.
-- connection: analytics_warehouse
-- cache: 1h
select * from users;
| Option | Type | Default | Description |
|---|---|---|---|
connection | string | default | Default connection for all cells |
cache | duration | 0 | Default cache duration |
Configuration syntax
Single-line: -- key: value
-- connection: warehouse
-- cache: 1h
Block (YAML): For complex configuration, use SQL block comments:
/*
connection: warehouse
cache: 1h
*/
Cell Configuration
Configuration immediately before a cell's SQL (no blank line).
-- connection: replica
select * from logs;
| Option | Type | Description |
|---|---|---|
connection | string | Override notebook connection |
@name | - | Name this cell for dataset reference |
Naming cells
Format: -- @name or -- @name [Optional Title]
The name is used for dataset references. The display title defaults to the name transformed to title case with underscores replaced by spaces (@daily_revenue becomes "Daily Revenue"). Override with brackets: @daily_revenue [Revenue by Day].
Scope
Connection scope is determined by blank lines:
-- connection: warehouse
select * from users;
-- connection: replica
select * from logs;
The first connection is notebook-level (blank line before SQL). The second is cell-level (no blank line).
Directive syntax
Cell configuration also supports directives for visualization:
Single-line: -- directive { key: value, key: value }
-- bar { x: "month", y: "revenue" }
select * from orders;
Directive syntax uses comma-separated key: value pairs inside braces. String values use double quotes. Numbers, booleans, and arrays are unquoted:
-- line { x: "date", y: ["revenue", "profit"], stacked: true }
Block (YAML):
/*
@daily_revenue
bar:
x: date
y: revenue
color: region
*/
select date, region, sum(amount) as revenue from orders group by 1, 2;
Visualization Directives
Directives control how cell results render. Without a directive, results render as a table.
table
-- table { freeze_columns: 2, freeze_rows: 1 }
select * from employees;
| Option | Type | Description |
|---|---|---|
freeze_columns | number | Columns to freeze on left |
freeze_rows | number | Rows to freeze on top |
Charts
Types: bar, line, area, scatter, pie, donut
/*
line:
x: date
y: [revenue, profit]
title: financial performance
legend: right
xFormat: "%Y-%m-%d"
yFormat: ",.0f"
*/
select date, revenue, profit from financial_metrics;
| Option | Type | Description |
|---|---|---|
x | string | Field for X axis |
y | string | string[] | Field(s) for Y axis |
series | ChartSeries[] | Additional series with per-series chart type |
color | string | Field for color encoding |
title | string | Chart title |
stacked | boolean | Stack bars/areas (default: true for bar/area) |
legend | top | bottom | left | right | none | Legend position (default: none) |
xLabel | string | X axis label |
yLabel | string | Y axis label |
xFormat | string | X axis format (d3-time-format) |
yFormat | string | Y axis format (d3-format) |
sort | none | x-asc | x-desc | value-asc | value-desc | Sort order (default: x-asc for bar) |
yScale | linear | log | Y axis scale (default: linear) |
size | string | Field for bubble size (scatter only) |
opacity | string | number | Field for opacity or fixed value (0-1) |
labels | string | Field to display as text labels on marks |
maxSeries | number | Max series before grouping into "Other" (default: 12) |
showOther | boolean | Show "Other" bucket (default: true for stacked/pie) |
ChartSeries
| Option | Type | Description |
|---|---|---|
y | string | Field for this series |
type | bar | line | area | scatter | Chart type for this series |
Format strings: Dates use d3-time-format (%Y-%m-%d, %b %d, %H:%M). Numbers use d3-format (,.0f for thousands, .2f for decimals, $,.2f for currency).
Chart-only cells
Reference a named cell's dataset without executing SQL:
/*
line:
x: date
y: revenue
title: 30-day trend
dataset: daily_revenue
*/
datasetreferences an upstream cell named with@name- No SQL required; the cell renders the referenced dataset
- Chaining supported: cell C can reference cell B which references cell A
Complete Example
-- connection: analytics_warehouse
-- cache: 1h
/*
@daily_revenue
bar:
x: date
y: revenue
title: daily revenue
*/
select
date(order_timestamp) as date,
sum(amount) as revenue
from orders
where order_timestamp >= current_date - interval '30 days'
group by 1
order by 1;
/*
line:
x: date
y: revenue
title: 30-day revenue trend
dataset: daily_revenue
*/
Types Reference
duration
Format: <number><unit> where unit is s (seconds), m (minutes), h (hours), or d (days).
Examples: 30s, 5m, 1h, 7d