IB Computer Science SQL, DDL and DML, and Data Warehouses: Tutor Prep Guide (five join steps, HAVING, star schema)
- 2026-11-27
Students can write SELECT star FROM one table, then hand in an aggregate with no GROUP BY, or forget the ON and return 192,000 rows. This lesson fixes SQL into five positions, so every statement can be read out loud from left to right.
01 Prerequisites (3-minute check)
If any of these fails, fix it before the lesson starts.
| Skill | Quick check | Pass mark |
|---|---|---|
| Primary and foreign keys | Which table does the enrolment student code point to | The student table, and the value must exist |
| The idea of an aggregate | The difference between COUNT and SUM | Counting rows against adding values |
| Filtering | To keep only enrolments after September, where does the condition go | WHERE or HAVING, and say which |
02 Lesson flow (60 minutes)
| Time | Stage | Tutor does | Student does |
|---|---|---|---|
| 0-8 min | Opening check | Ask why one table queries work and two tables fail | Write an answer, compare at the end |
| 8-20 min | DDL against DML | Write one CREATE and one INSERT side by side | Sort eight SQL statements |
| 20-40 min | Five join steps | No ON gives 192,000 rows against 4,840 steps | Read the five steps aloud |
| 40-50 min | GROUP BY and HAVING | 12 courses above 150, holding 2,400 rows | Write the group filter |
| 50-58 min | NoSQL and warehouses (HL) | Star schema 15 times faster, 4 shards | Choose a model and justify it |
| 58-60 min | Exit check | Collect and mark three items | Work independently |
03 Teaching script (read it out)
Scene 1: DDL against DML: one changes structure, the other content
Write three DDL statements: CREATE TABLE enrolment with a student code of eight characters, a course code of six, a term of four, a primary key over all three, and two foreign keys back to student and course; ALTER TABLE to add a score column; DROP TABLE to remove a whole table. Four DML statements: SELECT to read, INSERT to add a row, UPDATE to change rows, DELETE to remove rows.
Draw the distinction the exam likes: DROP removes the object and DELETE removes rows; DELETE can carry a WHERE and remove part of a table, while DROP cannot. And before any UPDATE or DELETE, run the same condition as a SELECT first, so the row count is known before anything changes.
Student task: sort eight statements into DDL and DML, and say which mistake is hardest to undo.
Scene 2: Two-table joins: five positions, and a missing one explodes
Cost the missing ON first: the enrolment table has 4,800 rows and the course table 40, so the Cartesian product is 192,000 rows, with every enrolment paired to all forty courses. With ON, the engine builds 40 hash buckets and scans 4,800 rows, which is 4,840 steps, 39.67 times fewer. That arithmetic is the mark for explaining why ON is compulsory.
Three modifiers carry most of the marks. DISTINCT removes duplicates: 4,800 enrolments involve only 1,020 students, so SELECT DISTINCT student code keeps 21.25 per cent of the rows. LIKE with the per cent wildcard matches patterns: LIKE Chan followed by the wildcard finds 96 students, 8.0 per cent of the roll. BETWEEN includes both ends: a term of 111 days runs from 1 September to 20 December and keeps both dates.
Then WHERE against HAVING: WHERE filters rows before grouping and cannot hold an aggregate, while HAVING filters groups after, so COUNT belongs there. 12 courses hold more than 150 enrolments (2,400 rows at 200 each) and the other 28 hold 2,400 (85.71 each), which is exactly what HAVING COUNT above 150 separates.
Student task: fill a five-box frame for the query that returns course name and enrolment count for every course above 150, then read it aloud and say which box holds the aggregate.
Scene 3: NoSQL and warehouses: the same data, two different questions (HL)
A warehouse solves a different problem. The daily system (OLTP) must change one row quickly, while analysis (OLAP) must scan millions of rows and summarise, and one model cannot be fast at both. A star schema puts one fact table in the middle (4,800,000 enrolment events) and 4 dimension tables around it (student, course, time, place), so each fact stores only foreign keys and measures, and a summary scans the fact table and looks up four dimensions.
Two numbers: at 20,000 rows per second the load takes 240 seconds. The same monthly enrolment summary joins four or five tables in the normalised model and measures 12.0 seconds, against 0.8 seconds in the star schema, 15 times faster. The price is duplicated data and a nightly sync, so a warehouse always reports up to last night.
Finally scale: when one machine cannot hold it, shard 4,800,000 rows into 4 pieces of 1,200,000, and accept that a cross-shard summary must merge again, which is the extra cost of distribution.
Student task: for four cases (food delivery orders, friend recommendations, sensor readings, annual financial reporting), pick relational or a NoSQL family or a warehouse, and say why the alternative is worse.
04 Question types and worked steps
| Type | Example | Steps | Answer |
|---|---|---|---|
| Writing a DDL statement | An enrolment table with three columns, a composite primary key and two foreign keys | Write columns and types, then PRIMARY KEY, then each FOREIGN KEY with REFERENCES | CREATE TABLE with three columns, one primary key and two foreign keys |
| Writing a two-table join with an aggregate | Course name and count for every course above 150, largest first | Five positions: SELECT with COUNT, FROM enrolment, JOIN course ON, GROUP BY course, HAVING above 150, ORDER BY DESC | 12 courses, 2,400 rows in total |
| Classifying a SQL statement as DDL or DML | ALTER adds a column, INSERT adds a row, DROP removes a table, UPDATE changes a row | Ask whether the shape or the content changed | Two DDL and two DML |
| Choosing a NoSQL family or a warehouse model (HL) | Time series readings against annual reporting | Judge the data shape and the read write ratio, then choose | Column family or time series; warehouse with star schema and ETL |
05 Independent practice (3 questions)
| Question | Answer | What to watch |
|---|---|---|
| 4,800 enrolments against 40 courses: how many rows without ON, and how many with it? | 192,000 rows without ON; 4,800 rows with it | Whether pairing and filtering were kept apart |
| 4,800 enrolments involve 1,020 students. What percentage of rows remain after SELECT DISTINCT student code? | 21.25 per cent (1,020 rows) | Whether the original enrolment count was the denominator |
| 12 of 40 courses exceed 150 enrolments, holding 2,400 rows. What is the average of the remaining courses? | 85.71 (2,400 divided by 28) | Whether both groups were added back to the total |
06 Common mistakes and what to say
| Mistake | Student writes | Say this |
|---|---|---|
| Putting an aggregate in WHERE | Writing WHERE COUNT star above 150 | “COUNT exists only after grouping, so the condition goes in HAVING. WHERE works one row at a time; HAVING works one group at a time.” |
| Forgetting ON in a join | Writing JOIN course GROUP BY course name with no ON | “Without ON the result is a 192,000 row Cartesian product, so every number is far too large. A join always carries ON with two matching keys.” |
| Using an asterisk with LIKE | Writing WHERE surname LIKE Chan followed by an asterisk | “The SQL wildcard is the per cent sign, not the asterisk. The asterisk selects every column, and inside LIKE it is just a character.” |
| Assuming NoSQL is always faster | Answering that NoSQL beats relational, so everything should move | “NoSQL is fast for one shape and a heavy write load, at the cost of cross-table transactions and a fixed schema. For exact financial figures the relational model is still safer.” |
07 Homework (paper, section and question numbers)
- IB Computer Science chapter index — All chapters in this subject
- Relational databases, ERDs and schema levels — The other chapter: 75 per cent saved, cardinality, composite keys
- Tutor prep guide index — Matching chapters in other subjects
08 Exit check (last 5 minutes)
| Question | Answer | Decision |
|---|---|---|
| DDL against DML | Structure against content | Not separating DROP from DELETE means replay act one |
| Five join steps | FROM, JOIN ON, WHERE, GROUP BY with HAVING, ORDER BY | No HAVING above 150 means replay act two |
| Warehouse benefit | The same query, as a ratio | Answering 15 seconds faster means replay act three |
09 Tutor FAQ
How much SQL must be memorised?
A3.3.2 names SELECT, DISTINCT, FROM, WHERE, BETWEEN, ORDER BY, GROUP BY, HAVING, ASC, DESC, JOIN, LIKE with the per cent wildcard, AND, OR and NOT. Writing a two-table join with GROUP BY and HAVING is enough; subqueries and window functions are extensions and are not examined.
Syntax differs between database systems, so what do we teach?
IB states that syntax may vary. Teach one standard form in class and tell students to follow the syntax given in the question, because the concepts transfer: HAVING always follows GROUP BY, whatever the product.
A3.4 is HL only, so do SL students write SQL?
Yes. A3.3 is for both levels, so SL students write two-table queries as well. The difference is that SL does not cover A3.4 on alternative databases and warehouses, so no ETL and no sharding.
Find a tutor — freeTutor Registration →
A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.



