Interactive SQL examples in JavaScript

This was my early attempt to provide interactive SQL snippets in the browser. Later, I created Codapi — an open-source tool for embedding interactive code examples into any kind of technical writing. Give it a try!

Reading about SQL is fun, but trying out live examples is even better! So I created JavaScript widgets that turn static SQL code in your articles into interactive examples.

Here is a working example:

select * from employees
limit 5;

And here are the four steps to creating executable SQL examples in your own articles or documentation:

1. Include the widgets

You'll need three JavaScript files:

  • sqlite3.js — SQLite compiled for the browser.
  • sqlime-db.js — the database web component.
  • sqlime-examples.js — the interactive example web component.

Include them from CDN or (better) download and host locally:

<script src="https://unpkg.com/@antonz/sqlite@3.40.0/dist/sqlite3.js"></script>
<script src="https://unpkg.com/sqlime@0.1.2/dist/sqlime-db.js"></script>
<script src="https://unpkg.com/sqlime@0.1.2/dist/sqlime-examples.js"></script>

You'll also need to download and serve the SQLite WebAssembly file if you're hosting locally:

https://unpkg.com/@antonz/sqlite@3.40.0/dist/sqlite3.wasm

sqlite3.wasm is used internally by the sqlite3.js script, so place them in the same folder.

I suggest you host SQLite locally because it weighs ≈1Mb, and CDNs tend to be quite slow with such large files.

You can install all of these using npm:

npm install @antonz/sqlite
npm install sqlime

Note. @antonz/sqlite is a copy of the official SQLite Wasm build, provided as an NPM package for convenience. You can download and use the build from the SQLite website if you prefer.

2. Write an article as usual

Suppose you are writing a short post about ranking data in SQL:

<p>To rank data in SQL, we use the
<code>rank()</code> window function:</p>

<pre class="example">select
  rank() over w as "rank",
  name, department, salary
from employees
window w as (order by salary desc)
order by "rank", id;</pre>

<p>the article goes on...</p>

Which renders as ordinary HTML:

To rank data in SQL, we use the rank() window function:

select
  rank() over w as "rank",
  name, department, salary
from employees
window w as (order by salary desc)
order by "rank", id;

the article goes on...

3. Load the database

You can create a database from a binary SQLite file or SQL script. I'll go with the latter and use employees.sql, which creates the employees table and populates it with data.

Load the database using the sqlime-db web component:

<sqlime-db name="employees" path="./employees.sql"></sqlime-db>
  • name is the name we'll use later to refer to the database.
  • path is the URL path to the SQL (or binary) database file.

4. Init the examples

The only thing left is to convert your HTML pre code snippets into interactive examples. Use the sqlime-examples web component to do this:

<sqlime-examples db="employees" selector="pre.example" editable></sqlime-examples>
  • db is the name of the database we defined earlier.
  • selector is the CSS selector for your SQL code snippets.
  • editable makes the examples editable (remove for read-only).

And that's it!

To rank data in SQL, we use the rank() window function:

select
  rank() over w as "rank",
  name, department, salary
from employees
window w as (order by salary desc)
order by "rank", id;

the article goes on...

sqlime-examples converts all the snippets with the specified selector, so you only need to include it once (unless you have multiple databases to run your examples on).

Summary

Executable SQL examples are an excellent fit for any kind of documentation:

  • They are more informative than static snippets.
  • They increase engagement and encourage experimentation,
  • They are lightweight, easy to set up, and do not require a server.

Try adding interactive SQL to your articles, or ask a question on GitHub if you have one.

──

Interactive examples in this post are powered by codapi — an open source tool I'm building. Use it to embed live code snippets into your product docs, online course or blog.

 Subscribe to keep up with new posts.