an online SQL playground

Interactive SQL Examples in JavaScript

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. Give it a try:

select * from employees
limit 5;

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

1. Include Sqlime JS widgets

You'll need three JavaScript files:

  1. sqlite3.js — SQLite compiled for the browser.
  2. sqlime-db.js — the database web component.
  3. sqlime-examples.js — the interactive examples web component.

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

<script src="https://unpkg.com/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.js"></script>
<script src="https://unpkg.com/sqlime/dist/sqlime-db.js"></script>
<script src="https://unpkg.com/sqlime/dist/sqlime-examples.js"></script>

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

https://unpkg.com/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/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 @sqlite.org/sqlite-wasm
npm install sqlime

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>

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>

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:

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

Last but not least:

⭐️ Star the project on GitHub if you like it

🚀 Follow @ohmypy on Twitter to keep up with new features

🍋 Use Sqlime to debug and share SQL snippets