Lab 08: Advanced SQLite

Objective

Push SQLite to its limits: window functions (RANK, SUM OVER, LAG), full-text search with FTS5, recursive CTEs, composite indexes, WAL mode, partial indexes, and a Unit-of-Work transaction pattern.

Background

SQLite supports nearly full SQL:2011 including window functions and CTEs. Combined with WAL (Write-Ahead Logging) for concurrent reads, FTS5 for full-text search, and proper indexing, it can handle millions of rows and complex analytics queries — no server required.

Time

35 minutes

Prerequisites

  • Practitioner Lab 08 (SQLite basics)

Tools

  • Docker: zchencow/innozverse-python:latest


Lab Instructions

Step 1: Schema Design — Indexes, Constraints & WAL

💡 PRAGMA journal_mode=WAL switches SQLite from rollback journal to Write-Ahead Logging. In WAL mode, readers don't block writers and writers don't block readers — critical for web apps with concurrent requests. A partial index (WHERE stock > 0) is smaller than a full index because SQLite only stores rows matching the condition, making in-stock queries faster.

📸 Verified Output:


Step 2: Window Functions — RANK, SUM OVER, LAG

📸 Verified Output:


Steps 3–8: CTEs, FTS5, Unit of Work, Explain Query Plan, JSON, Capstone

📸 Verified Output:


Summary

Feature
SQL
Use case

Window rank

RANK() OVER(PARTITION BY ... ORDER BY ...)

Leaderboards

Running total

SUM(x) OVER(ORDER BY ...)

Cumulative metrics

Row comparison

LAG(x) OVER(ORDER BY ...)

Period-over-period

CTE

WITH name AS (SELECT ...)

Readable sub-queries

Recursive CTE

WITH RECURSIVE name AS (... UNION ALL ...)

Trees, hierarchies

FTS5

CREATE VIRTUAL TABLE ... USING fts5(...)

Full-text search

JSON

json_extract(col, '$.field')

Semi-structured data

Partial index

CREATE INDEX ... WHERE condition

Filtered fast lookups

Further Reading

Last updated