SQL and Databases Lab
Real queries on an in-browser SQLite database with auto-checked challenges.
- 3lessons
- 1experiments
- 5terms
Scientific introduction
SQL is the language of relational data: querying, aggregating, joining and analysing. The exercises run on a real SQLite engine (sql.js) in your browser, so nothing reaches a server and no database is affected.
Learning objectives
- Write SELECT queries with filtering and ordering
- Aggregate with GROUP BY and HAVING
- Use the join types
- Use window functions and subqueries
- Understand the relational model and keys
Topics
- SELECT
- JOIN
- GROUP BY
- Window functions
Core concepts
- Primary key
- One or more columns that uniquely identify each row.
- JOIN
- Combining rows of two tables on a matching condition over one or more columns.
- GROUP BY
- Splitting rows into groups to apply aggregate functions to each.
- Window function
- A function computed over a set of rows related to the current row without collapsing them.
- NULL
- A marker for an unknown value; it does not equal itself in comparisons.
Theory
SELECT, filtering and aggregation
Query shape
The logical order of evaluation is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.
SELECT region, SUM(revenue) AS total
FROM sales
WHERE year = 2024
GROUP BY region
HAVING SUM(revenue) > 1000
ORDER BY total DESC;Aggregates (COUNT, SUM, AVG) ignore NULLs except COUNT(*).
Joins
Types
- INNER JOIN: only rows that match in both tables.
- LEFT JOIN: every row of the left table, with NULL where nothing matches.
- CROSS JOIN: every combination (careful: Cartesian product).
Common mistakes: joining on a non-unique key so rows multiply, and aggregating after a one-to-many join so values are counted twice. Always compare row counts before and after the join.
Window functions
The idea
A window function computes a value for each row from a set of related rows without collapsing them into one row, unlike GROUP BY.
SELECT region, month, revenue,
SUM(revenue) OVER (PARTITION BY region ORDER BY month) AS running_total,
RANK() OVER (PARTITION BY month ORDER BY revenue DESC) AS rnk
FROM sales;Useful for running totals, ranking and comparison with the previous row (LAG).
Worked examples
WHERE versus HAVING
WHERE filters rows before aggregation; HAVING filters groups after it. A condition on a group statistic cannot go in WHERE.
Interactive experiments
SQL playground
Learning goal: Write real queries against a SQLite database and solve auto-checked challenges.
Explanation and scientific documentation
You work on synthetic tables (customers, orders, products) in browser memory; you can rebuild the database. No query reaches the server.
Data: Synthetic data generated in your browser with an editable seed; the relationships are planted on purpose for learning and are not real data.
The engine is SQLite compiled to WebAssembly (sql.js) inside a Web Worker with an execution timeout.
Limits: Small data; not a substitute for performance testing on real databases.
Related algorithms and methods
Inner join
Keeps rows that have a match in both tables.
Window functions
Compute over a window of rows without collapsing them (e.g. ROW_NUMBER, SUM OVER).
Suitable datasets
- Sales (synthetic) Synthetic, generated in the browser
Practical projects
- SQL analytics practice Intermediate
Assessment questions
Sources and references
- Codd, E. F. (1970). A relational model of data for large shared data banks. Communications of the ACM, 13(6), 377-387 DOI (bibliographic data entered by the editor, not machine-checked)
- Kleppmann, M. (2017). Designing Data-Intensive Applications. O'Reilly Media (bibliographic data entered by the editor, not machine-checked)
