Skip to content

SQL Playground and Inline Editor

Ved Gupta edited this page Nov 13, 2026 · 2 revisions

SQL Playground & Inline Editor

The SQL package provides a fast, zero-dependency editor and playground for PostgreSQL 18 and SQLite 3 (~16.5 KB gzipped CSS and JS combined). Because it boots no WebAssembly of its own, you can mount multiple runnable SQL blocks on a single lesson page while keeping mobile performance high, and connect any query engine such as PGlite, SQLite WASI, or an HTTP endpoint.


Importing the SQL Pack

Use the subpath export in bundled apps, or load the standalone UMD script from a CDN:

import {
  createSqlEditor,
  formatSql,
  highlightSql,
  completeSql,
  extractTableAliases,
  parseSqlEngineError
} from '@innovatorved/code-lab/sql';
import '@innovatorved/code-lab/sql.css';

Display Modes

Mode When to Use It
Inline Compact auto-sizing editor with line numbers and optional header or footer slots. Designed for embedding several runnable SQL examples inside a single article without file trees or terminals.
Playground Responsive split-pane or stacked SQL studio with pluggable slots for query results, schema inspection, and visual query plans. Touch-friendly sizing prevents unwanted zoom on mobile Safari.
Workbench Full Guided Lab shell for multi-step SQL challenges with instructions, hints, diffs, and tabular result verification. Single-file SQL labs automatically hide multi-file explorer and shell chrome.

Playground Example with Schema Autocomplete

Pass your database schema to enable table-alias resolution, primary and foreign key badges, row count hints, and inline wavy underlines when the database engine reports a syntax or runtime error:

import { createSqlEditor, formatSql, parseSqlEngineError } from '@innovatorved/code-lab/sql';
import '@innovatorved/code-lab/sql.css';

const editor = createSqlEditor(document.getElementById('sql-studio')!, {
  mode: 'playground',
  dialect: 'postgres',
  theme: 'auto',
  minLines: 4,
  maxLines: 18,
  value: formatSql(
    'select o.id, o.total_cents from customers c join orders o on o.customer_id = c.id where o.total_cents > 1000;',
    { dialect: 'postgres', indent: 2, keywordCase: 'upper' }
  ),
  schema: {
    dialect: 'postgres',
    tables: [
      {
        name: 'customers',
        rowCount: 12000,
        columns: [
          { name: 'id', type: 'bigint', pk: true },
          { name: 'email', type: 'text' }
        ]
      },
      {
        name: 'orders',
        rowCount: 84000,
        columns: [
          { name: 'id', type: 'bigint', pk: true },
          { name: 'customer_id', type: 'bigint', fk: 'customers.id' },
          { name: 'total_cents', type: 'integer' }
        ]
      }
    ]
  },
  async onRun({ effectiveSql }) {
    try {
      editor.clearDiagnostics();
      console.log('Executing query:', effectiveSql);
    } catch (err) {
      editor.setDiagnostics([parseSqlEngineError(err, effectiveSql, 'postgres')]);
    }
  }
});

Dialect Support

  • Standard SQL: Common table expressions (including recursive CTEs), window functions and frame clauses, joins, subqueries, and DDL/DML statements.
  • PostgreSQL 18: Type casts, positional parameters, dollar-quoted strings, lateral joins, returning clauses, aggregate filter clauses, JSONB operators, and buffer-aware explain plans.
  • SQLite 3: Pragmas, strict and without-rowid tables, conflict upsert clauses, glob matching, bracketed or backticked identifiers, and query plan explanations.

Keyboard Shortcuts & Accessibility

Shortcut Action
Cmd / Ctrl + Enter Run the highlighted selection, or the full query when nothing is selected
Shift + Alt + F Format the SQL query using dialect-aware indentation
Cmd / Ctrl + / Toggle SQL line comments on the selected lines
Ctrl + Space Open the accessible schema and keyword completion menu
Esc then Tab Release indentation capture so keyboard focus moves cleanly to the next control on the page

Multi-Step SQL Guided Labs

To run structured SQL exercises inside the Guided Lab workbench, set the language to SQL and provide a query runner function:

import CodeLab from '@innovatorved/code-lab';
import '@innovatorved/code-lab/code-lab.css';

const sqlLab = CodeLab.mount('#sql-guided-lab', {
  lang: 'sql',
  sqlDialect: 'postgres',
  code: 'SELECT id, email FROM customers ORDER BY id;',
  steps: [
    {
      id: 'step-1',
      title: 'Order customers by ID',
      instructions: 'Select id and email from customers sorted in ascending order.'
    }
  ],
  async sqlRunner({ sql, mode }) {
    return {
      columns: ['id', 'email'],
      rows: [
        [1, 'ada@analytical.org'],
        [2, 'grace@compiler.org']
      ],
      rowCount: 2,
      durationMs: 1.2,
      verification: { passed: true, message: 'All rows verified!' }
    };
  }
});

Clone this wiki locally