|
| 1 | +--- |
| 2 | +title: "AlaSQL for LiaScript: Teach SQL and Query CSV Data in the Browser" |
| 3 | +slug: "alasql-sql-in-liascript" |
| 4 | +date: 2026-05-27 |
| 5 | +draft: false |
| 6 | +author: "André Dietrich" |
| 7 | +image: "https://jquery-plugins.net/image/plugin/alasql-javascript-sql-database-library.png" |
| 8 | +categories: |
| 9 | + - Template |
| 10 | + - Tutorial |
| 11 | +tags: |
| 12 | + - Templates |
| 13 | + - Database |
| 14 | + - SQL |
| 15 | + - Interactive Exercises |
| 16 | + - No Server |
| 17 | + - Data Science |
| 18 | + |
| 19 | +description: "Use the AlaSQL template to run SQL queries directly inside LiaScript courses — with support for CSV data, JSON, and JavaScript arrays. No server, no setup." |
| 20 | +--- |
| 21 | + |
| 22 | +SQL is not only for relational databases. |
| 23 | +With the [AlaSQL template](https://github.com/liaTemplates/AlaSQL), you can run SQL queries inside LiaScript courses against in-memory tables, JavaScript arrays, and CSV files — entirely in the browser. |
| 24 | + |
| 25 | +No backend, no installation, no accounts. |
| 26 | +Just import the template and start writing SQL. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## What is AlaSQL? |
| 31 | + |
| 32 | +[AlaSQL](http://alasql.org) is a lightweight, JavaScript-native SQL interpreter. |
| 33 | +Unlike traditional database engines ported to WebAssembly, AlaSQL is written directly in JavaScript and runs without any binary runtime. |
| 34 | + |
| 35 | +It supports a large subset of standard SQL — `CREATE TABLE`, `INSERT`, `SELECT`, `JOIN`, `GROUP BY`, `ORDER BY`, subqueries — and extends it with the ability to query JavaScript arrays, JSON objects, CSV files, `localStorage`, and `IndexedDB`. |
| 36 | + |
| 37 | +The LiaScript AlaSQL template wraps this into two focused macros that integrate cleanly with LiaScript code blocks. |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Quick Start |
| 42 | + |
| 43 | +Add this line to your course header: |
| 44 | + |
| 45 | +``` markdown |
| 46 | +<!-- |
| 47 | +import: https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md |
| 48 | +--> |
| 49 | +``` |
| 50 | + |
| 51 | +All macros are now available throughout the document. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Basic SQL: `@AlaSQL.eval` |
| 56 | + |
| 57 | +Add `@AlaSQL.eval` after any SQL code block to make it executable and editable. |
| 58 | +The block is sent to the AlaSQL interpreter and the result is displayed below as formatted JSON. |
| 59 | + |
| 60 | +```` markdown |
| 61 | +``` sql |
| 62 | +CREATE TABLE students (id INT, name STRING, grade INT); |
| 63 | + |
| 64 | +INSERT INTO students VALUES (1, 'Alice', 92); |
| 65 | +INSERT INTO students VALUES (2, 'Bob', 78); |
| 66 | +INSERT INTO students VALUES (3, 'Carol', 85); |
| 67 | + |
| 68 | +SELECT * FROM students ORDER BY grade DESC; |
| 69 | +``` |
| 70 | +@AlaSQL.eval |
| 71 | +```` |
| 72 | + |
| 73 | +Try it live: |
| 74 | + |
| 75 | +{{< liascript mode="preview" >}} |
| 76 | +<!-- |
| 77 | +import: https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md |
| 78 | +--> |
| 79 | + |
| 80 | +# AlaSQL Demo |
| 81 | + |
| 82 | +``` sql |
| 83 | +CREATE TABLE students (id INT, name STRING, grade INT); |
| 84 | + |
| 85 | +INSERT INTO students VALUES (1, 'Alice', 92); |
| 86 | +INSERT INTO students VALUES (2, 'Bob', 78); |
| 87 | +INSERT INTO students VALUES (3, 'Carol', 85); |
| 88 | + |
| 89 | +SELECT * FROM students ORDER BY grade DESC; |
| 90 | +``` |
| 91 | +@AlaSQL.eval |
| 92 | +{{< /liascript >}} |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## Persistent State Across Blocks |
| 97 | + |
| 98 | +AlaSQL maintains its own runtime environment within a course page. |
| 99 | +Tables created in one block are available in all subsequent blocks on the same page — no need to recreate data for every query. |
| 100 | + |
| 101 | +```` markdown |
| 102 | +``` sql |
| 103 | +CREATE TABLE products (id INT, name STRING, price FLOAT, category STRING); |
| 104 | + |
| 105 | +INSERT INTO products VALUES (1, 'Laptop', 999.00, 'Electronics'); |
| 106 | +INSERT INTO products VALUES (2, 'Desk', 249.00, 'Furniture'); |
| 107 | +INSERT INTO products VALUES (3, 'Monitor', 399.00, 'Electronics'); |
| 108 | +INSERT INTO products VALUES (4, 'Chair', 189.00, 'Furniture'); |
| 109 | +INSERT INTO products VALUES (5, 'Tablet', 549.00, 'Electronics'); |
| 110 | +``` |
| 111 | +@AlaSQL.eval |
| 112 | + |
| 113 | +``` sql |
| 114 | +-- Subsequent block — table is already there |
| 115 | +SELECT category, COUNT(*) AS items, ROUND(AVG(price), 2) AS avg_price |
| 116 | +FROM products |
| 117 | +GROUP BY category |
| 118 | +ORDER BY avg_price DESC; |
| 119 | +``` |
| 120 | +@AlaSQL.eval |
| 121 | +```` |
| 122 | + |
| 123 | +This is useful for multi-step exercises where each block builds on the previous one. |
| 124 | + |
| 125 | +{{< liascript mode="preview" >}} |
| 126 | +<!-- |
| 127 | +import: https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md |
| 128 | +--> |
| 129 | + |
| 130 | +# AlaSQL – Persistent State |
| 131 | + |
| 132 | +``` sql |
| 133 | +CREATE TABLE products (id INT, name STRING, price FLOAT, category STRING); |
| 134 | + |
| 135 | +INSERT INTO products VALUES (1, 'Laptop', 999.00, 'Electronics'); |
| 136 | +INSERT INTO products VALUES (2, 'Desk', 249.00, 'Furniture'); |
| 137 | +INSERT INTO products VALUES (3, 'Monitor', 399.00, 'Electronics'); |
| 138 | +INSERT INTO products VALUES (4, 'Chair', 189.00, 'Furniture'); |
| 139 | +INSERT INTO products VALUES (5, 'Tablet', 549.00, 'Electronics'); |
| 140 | +``` |
| 141 | +@AlaSQL.eval |
| 142 | + |
| 143 | +``` sql |
| 144 | +SELECT category, COUNT(*) AS items, ROUND(AVG(price), 2) AS avg_price |
| 145 | +FROM products |
| 146 | +GROUP BY category |
| 147 | +ORDER BY avg_price DESC; |
| 148 | +``` |
| 149 | +@AlaSQL.eval |
| 150 | +{{< /liascript >}} |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## CSV Import: `@AlaSQL.eval_with_csv` |
| 155 | + |
| 156 | +`@AlaSQL.eval_with_csv` is the standout feature of this template. |
| 157 | +It accepts two consecutive code blocks: the first contains a SQL statement (use `?` as a placeholder for the data), the second contains the raw CSV with a header row. |
| 158 | +[PapaParse](https://www.papaparse.com) handles the CSV parsing automatically. |
| 159 | + |
| 160 | +```` markdown |
| 161 | +``` sql |
| 162 | +CREATE TABLE sales; |
| 163 | +INSERT INTO sales SELECT * FROM ?; |
| 164 | +``` |
| 165 | +``` text -data.csv |
| 166 | +Region,Country,Item,Units,Revenue |
| 167 | +Europe,Germany,Laptop,12,11988.00 |
| 168 | +Europe,France,Tablet,8,4392.00 |
| 169 | +Asia,Japan,Monitor,15,5985.00 |
| 170 | +Americas,USA,Laptop,22,21978.00 |
| 171 | +Asia,India,Tablet,19,10431.00 |
| 172 | +Americas,Canada,Desk,7,1743.00 |
| 173 | +Europe,Spain,Chair,11,2079.00 |
| 174 | +``` |
| 175 | +@AlaSQL.eval_with_csv |
| 176 | +```` |
| 177 | + |
| 178 | +After the import you can query the table in any following block with `@AlaSQL.eval`: |
| 179 | + |
| 180 | +```` markdown |
| 181 | +``` sql |
| 182 | +SELECT Region, SUM(Revenue) AS total |
| 183 | +FROM sales |
| 184 | +GROUP BY Region |
| 185 | +ORDER BY total DESC; |
| 186 | +``` |
| 187 | +@AlaSQL.eval |
| 188 | +```` |
| 189 | + |
| 190 | +Try the full flow live: |
| 191 | + |
| 192 | +{{< liascript mode="preview" >}} |
| 193 | +<!-- |
| 194 | +import: https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md |
| 195 | +--> |
| 196 | + |
| 197 | +# AlaSQL – CSV Import |
| 198 | + |
| 199 | +``` sql |
| 200 | +CREATE TABLE sales; |
| 201 | +INSERT INTO sales SELECT * FROM ?; |
| 202 | +``` |
| 203 | +``` text -data.csv |
| 204 | +Region,Country,Item,Units,Revenue |
| 205 | +Europe,Germany,Laptop,12,11988.00 |
| 206 | +Europe,France,Tablet,8,4392.00 |
| 207 | +Asia,Japan,Monitor,15,5985.00 |
| 208 | +Americas,USA,Laptop,22,21978.00 |
| 209 | +Asia,India,Tablet,19,10431.00 |
| 210 | +Americas,Canada,Desk,7,1743.00 |
| 211 | +Europe,Spain,Chair,11,2079.00 |
| 212 | +``` |
| 213 | +@AlaSQL.eval_with_csv |
| 214 | + |
| 215 | +``` sql |
| 216 | +SELECT Region, SUM(Revenue) AS total |
| 217 | +FROM sales |
| 218 | +GROUP BY Region |
| 219 | +ORDER BY total DESC; |
| 220 | +``` |
| 221 | +@AlaSQL.eval |
| 222 | +{{< /liascript >}} |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +## Full Template Demo |
| 227 | + |
| 228 | +The complete AlaSQL README is itself a self-documenting LiaScript course — explore all macros and examples live: |
| 229 | + |
| 230 | +{{< liascript-show "https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md" >}} |
| 231 | + |
| 232 | +--- |
| 233 | + |
| 234 | +## Use Cases |
| 235 | + |
| 236 | +**SQL fundamentals** — Teach `SELECT`, `WHERE`, `JOIN`, `GROUP BY`, and aggregation functions with live, editable exercises. |
| 237 | +No DBMS to install, no accounts to create — students open the course URL and write SQL immediately. |
| 238 | + |
| 239 | +**Data literacy with CSV** — Load a realistic CSV dataset directly into the course and let students explore it with SQL queries. |
| 240 | +This is a natural fit for courses on business data, statistics, or research methods where data arrives as spreadsheet exports. |
| 241 | + |
| 242 | +**Introductory data analysis** — Combine SQL filtering and aggregation with LiaScript's quiz blocks for guided, self-paced data exploration exercises. |
| 243 | + |
| 244 | +**Lightweight NoSQL-style queries** — AlaSQL can query JavaScript arrays and JSON objects as if they were tables. |
| 245 | +This makes it useful for explaining the conceptual bridge between structured data formats and SQL queries. |
| 246 | + |
| 247 | +**Courses without internet dependency** — Because AlaSQL is JavaScript-native (no WASM download required), the template starts faster than WASM-based alternatives and works well in low-bandwidth environments. |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +## Technical Facts |
| 252 | + |
| 253 | +| | | |
| 254 | +|---|---| |
| 255 | +| **Runs in browser** | Yes — pure JavaScript, no backend | |
| 256 | +| **Database engine** | AlaSQL (JavaScript-native) | |
| 257 | +| **SQL support** | Standard SQL: SELECT, JOIN, GROUP BY, subqueries, and more | |
| 258 | +| **CSV support** | Yes — via PapaParse, with automatic header detection | |
| 259 | +| **External dependency** | AlaSQL and PapaParse loaded from CDN on first use | |
| 260 | +| **State persistence** | Yes — tables persist across blocks on the same page | |
| 261 | +| **Offline capable** | After first load (browser cache) | |
| 262 | +| **Named databases** | No — single shared AlaSQL runtime per page | |
| 263 | +| **License** | MIT (AlaSQL: MIT, PapaParse: MIT) | |
| 264 | +| **Maintained** | Yes | |
| 265 | + |
| 266 | +--- |
| 267 | + |
| 268 | +## Try It |
| 269 | + |
| 270 | +{{< button link="https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md" label="Try in LiaScript" >}} |
| 271 | + |
| 272 | +{{< button link="https://liascript.github.io/LiveEditor/?/show/file/https://raw.githubusercontent.com/liaTemplates/AlaSQL/master/README.md" label="Open in LiveEditor" >}} |
| 273 | + |
| 274 | +{{< button link="https://github.com/liaTemplates/AlaSQL" label="View on GitHub" >}} |
| 275 | + |
| 276 | +--- |
| 277 | + |
| 278 | +## Related Templates |
| 279 | + |
| 280 | +- [**SQLite**](https://github.com/liaTemplates/SQLite) — full SQLite engine via WebAssembly, with named persistent databases, EXPORT/IMPORT, and support for `.db` file loading |
| 281 | +- [**DuckDB**](https://github.com/LiaTemplates/DuckDB) — analytical SQL in the browser with DuckDB/WASM, optimised for large datasets and OLAP queries |
| 282 | +- [**PGlite**](https://github.com/LiaTemplates/PGlite) — full PostgreSQL in the browser, for courses that target Postgres-specific syntax |
| 283 | +- [**Pyodide**](https://github.com/LiaTemplates/Pyodide) — run Python with pandas in the browser; pairs well with SQL for data science courses |
0 commit comments