Skip to content

Commit 3307553

Browse files
committed
add sql + plot example
1 parent 001ee14 commit 3307553

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

docs/sql.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ For complex destructuring patterns, you may need to quote the `id` directive. Fo
9999
For dynamic or interactive queries that respond to user input, you can interpolate values into SQL queries using inline expressions `${…}`. For example, to show the stars around a given brightness:
100100

101101
```js echo
102-
const mag = view(Inputs.range([6, 20], {label: "Magnitude"}));
102+
const mag = view(Inputs.range([6, 22], {label: "Magnitude"}));
103103
```
104104

105105
```sql echo
@@ -151,7 +151,25 @@ const rows = await sql`SELECT random() AS random`;
151151
rows[0].random
152152
```
153153

154-
The `sql` tagged template literal is available by default in Markdown, but you can also import it explicitly as:
154+
The `sql` tag is useful for querying data within JavaScript, such as to query data for visualization without needing to create a separate SQL code block and giving the data a name. For example, below we use DuckDB to bin stars by brightness, and then visualize the bins as a histogram using a [rect mark](https://observablehq.com/plot/marks/rect).
155+
156+
```js echo
157+
Plot.plot({
158+
x: {round: true, label: "phot_g_mean_mag"},
159+
marks: [
160+
Plot.axisY({tickFormat: (d) => d / 1000, label: "count (thousands)"}),
161+
Plot.rectY(await sql`
162+
SELECT
163+
FLOOR(phot_g_mean_mag / 0.2) * 0.2 AS mag1
164+
, mag1 + 0.2 AS mag2
165+
, COUNT() AS count
166+
FROM gaia GROUP BY 1
167+
`, {x1: "mag1", x2: "mag2", y: "count", tip: true})
168+
]
169+
})
170+
```
171+
172+
The `sql` tag is available by default in Markdown. You can also import it explicitly as:
155173

156174
```js echo
157175
import {sql} from "npm:@observablehq/duckdb";

0 commit comments

Comments
 (0)