Method · Statistics Library

SQL query console

The SQL console runs a read-only SELECT/WITH/VALUES query against your loaded tables entirely on your own machine, saving the result as a new table you can chart, test, or query again, without any network access or data leaving the computer.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

Sometimes reshaping data (joining three tables together, computing several grouped averages at once, or ranking rows within a group) is much more natural to write as a SQL query than to build step by step through point-and-click menus. This console gives you exactly that: type a standard SQL SELECT statement referencing your loaded tables by name, and the result becomes its own new table, ready to chart or run a statistical test on.

Everything happens locally on your own computer: there is no server, no account, no network call involved in running a query over your own data. The console is also deliberately READ-ONLY: it can only look at your loaded tables and produce a new table from what it finds; it can never modify, delete, or overwrite anything you already have loaded.

One classic trap worth knowing before you write conditions: SQL treats comparisons against a missing (NULL) value specially. A filter like WHERE column != 'x' will silently DROP every row where that column is blank, because comparing NULL to anything (even with !=) never evaluates to true in SQL's three-valued logic: it evaluates to 'unknown', which a WHERE clause treats the same as false. Test for missingness explicitly with IS NULL / IS NOT NULL rather than relying on an ordinary comparison to catch it.

The SQL console runs each query with DuckDB, a small database engine built into KernelStats, so a DuckDB query is simply SQL typed here.

Practitioner explanation

The engine is DuckDB (MIT-licensed, embedded, no server process) with Python's stdlib sqlite3 module as an automatic offline fallback if DuckDB is ever unavailable, consistent with the product's stdlib-first, no-new-dependency posture elsewhere. Only a SINGLE read-only statement is accepted (SELECT, a WITH common-table-expression, or VALUES); multiple semicolon-separated statements or a write operation (DELETE, DROP, INSERT, and similar) are rejected with a clear message before anything runs, since this console is architecturally incapable of modifying a loaded table in place.

The query is SANDBOXED specifically to the tables visible in the table picker: DuckDB's file- and URL-reading functions (read_csv('path'), read_text(...), glob(...), an https:// path) are switched off at the engine level, and that setting is LOCKED so a query cannot re-enable it from inside SQL; this is what makes 'your data never leaves this machine' a mechanically enforced guarantee rather than merely a stated policy. Excluded rows (the row-state 'excluded' flag) are invisible to the query exactly as they are to every other analysis in KernelStats, preserving the product's invariant that row-state exclusions apply uniformly everywhere.

SQL queries run from the SQL Console, which can read every open table, rather than from the ordinary analysis form. A dedicated launch_event routes the Inspector to the SQL Console surface specifically, rather than the generic form, because that surface is the only one that correctly pops the pending result into a new table.

When to use it

Consider it when

You need to join, filter, aggregate, or reshape one or more loaded tables in a way that is tedious to express through point-and-click steps: a multi-table join, a GROUP BY with several aggregates at once, or a window function (running total, rank within group).

Use caution when

The task is a single, common cleanup step (pivot, melt, filter, dedupe, a simple join) already covered by the point-and-click Wrangling & Import Recipe panel, or you need to write to an external file or database (the console is read-only over loaded tables and cannot reach the filesystem or network at all, by locked design).

Assumptions and checks

  • A SQL query only returns what it asks for; a wrong JOIN condition, an unintended filter, or a missing GROUP BY can silently produce a result table that looks plausible but does not represent the intended population or aggregation.

    In KernelStats: Not checked by KernelStats: there is no statistical assumption to violate here; the query executes exactly as written against the data source, and the resulting table should be inspected (row count, a few sample rows) before using it in further analysis.

Alternatives

  • Descriptive Summary. The task is simply summarizing an already-loaded table rather than reshaping or joining multiple tables together.

Worked example

Scenario

Joining a loaded orders table to a loaded customers table on customer_id, filtering to orders over $100, and computing total spend per region.

Result

A new table with one row per region, ordered by total spend descending, saved as its own queryable table in the project.

Interpretation

The join and aggregation that would require several separate wrangling steps in a point-and-click panel are expressed in one readable query, and the resulting table can now be charted or fed into a hypothesis test exactly like any table loaded directly from a file.

Common misconceptions

Misconception

A WHERE clause like column != 'x' correctly includes every row that isn't 'x', including rows where the column is blank/missing.

Correction

SQL's three-valued logic makes NULL != 'x' evaluate to UNKNOWN, not TRUE, so rows with a NULL in that column are silently DROPPED by such a filter rather than included. Test for missingness explicitly with IS NULL / IS NOT NULL, and use COALESCE(column, fallback) to substitute a default before comparing, whenever a column might contain NULLs.

Misconception

Because DuckDB can normally read arbitrary files and URLs from inside a SQL query, the SQL console could be used to pull in outside data or leak loaded data externally.

Correction

DuckDB's file- and URL-reading functions are switched off at the engine level specifically for this console, and that setting is LOCKED so no query can re-enable it from within SQL. The console can see only the tables already loaded into KernelStats and nothing else on the computer or network: this is a mechanically enforced sandbox, not merely a usage guideline.

References

  1. DuckDB Documentation, DuckDB Foundation (2024).