Learning SQL with in-memory relations
Relational databases store rows in named tables with typed columns. Primary and foreign keys express references between entities—orders point to customers, line items point to products. SELECT describes which columns to project, which rows pass predicates, how tables combine, and how groups aggregate.
A browser sandbox builds tiny tables from CREATE TABLE and INSERT, then evaluates read-only SELECT locally. That mirrors the logical model students use in courses and interviews without provisioning MySQL or PostgreSQL—while remaining a teaching engine, not a substitute for EXPLAIN, transactions, or vendor-specific functions.
Query practice — background
Tables, keys, and rows
- CREATE TABLE declares column names and coarse types; each INSERT adds one row to the heap-like store.
- Primary keys uniquely identify rows within a table; foreign keys reference another table’s key column.
- NULL marks missing values; comparisons and JOINs treat NULL with three-valued logic unless filtered explicitly.
- Semicolon-separated scripts run in order—later SELECT statements see rows inserted earlier in the same session.
How JOINs combine relations
- INNER JOIN keeps rows where the ON predicate matches pairs from both sides; non-matching rows disappear from the result.
- LEFT JOIN preserves every row from the left table and fills missing right-side columns with NULL when no match exists.
- Join order affects performance on real servers; for learning, focus on whether the predicate expresses the intended relationship.
- Cartesian products appear when JOIN is omitted or ON is always true—row counts multiply and filters become essential.
Filters, sort, and aggregation
- WHERE removes rows before grouping; only non-aggregated columns belong in simple WHERE clauses.
- GROUP BY collapses rows sharing key values; aggregate functions (COUNT, SUM, AVG) summarize each group.
- HAVING filters groups after aggregation—use it for conditions on SUM or COUNT, not raw row columns.
- ORDER BY sorts the final projection; LIMIT caps rows returned for pagination patterns.
How SELECT becomes rows: parser, planner, executor
- The parser builds a query tree from SQL text; syntax errors fail before any relation is touched.
- The planner chooses join order and access paths (sequential scan vs index) using statistics and cost models.
- The executor pulls tuples through the plan; projection and filtering happen as operators consume child rows.
- A result set is unordered unless ORDER BY is present—relational algebra treats relations as sets or bags, not lists.
Where a sandbox helps
JOIN and GROUP BY drills
Two-table schemas (products and order lines) make join keys tangible—change ON or add GROUP BY and compare row sets without installing a server.
Homework and interview checks
Paste the problem’s CREATE/INSERT, write the expected SELECT, and compare JSON output to the rubric before opening a corporate database client.