Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Master SQL INNER and LEFT JOIN in Punggol Tuition

A student writes in a notebook beside a laptop, surrounded by open books in a bright home study space.

PUNGGOL TUITION · PRACTICAL MASTERY GUIDE

A child may ask why an SQL result shows the same student name twice, or why another name disappears completely. The useful first response is to inspect the relationship between the tables: how many submissions match each student ID, and whether unmatched students should still appear.

INNER JOIN retains only pairs for which the join predicate matches. LEFT JOIN retains those matching pairs and adds a row for each left-hand record with no match, filling right-hand columns with NULL. In a fictional example, one learner with two quiz submissions produces two joined rows; another learner with no submission appears only in the LEFT result.

Every example here uses invented small tables, evaluated with a disposable SQLite database and compared with the principles in official PostgreSQL documentation. They are learning exercises about data relationships, not school records, real pupil statistics or claims about a particular Punggol tuition provider.

The worked records and names below are fictional. They are suggested family or learner exercises; they do not describe a particular Punggol school, a physical tuition premises, fees, guaranteed outcomes, or an advertised class timetable. The official technical source is Official PostgreSQL table-expression and JOIN documentation.

Find the right route

For a quick explanation, begin with the model and the first worked case. For a broken project, choose its closest chapter, predict the outcome, and follow the diagnostic route. For practice, complete one contrasting example before consulting the worked answer. The full index is expandable so the long guide remains navigable on a phone.

The wider family-study context is in How Studying Works; local learning resources can be explored through the Punggol education and town-life index.

Route 1: chapter 1 to chapter 4 — Understand match predicates, preserved rows and one-to-many grain.

Route 2: chapter 5 to chapter 8 — Separate ON conditions, WHERE filters, counts and missing rows.

Route 3: chapter 9 to chapter 12 — Choose summaries and prevent false matches.

Route 4: chapter 13 to chapter 16 — Handle shorthand, composite identifiers and special relationships.

Route 5: chapter 17 to chapter 20 — Validate ordering and reconcile every join.

Open all twenty chapter headings
  1. 1. Start with the shared key, not two similar-looking columns
  2. 2. Keep unmatched parents with LEFT JOIN
  3. 3. Explain why one-to-many joins repeat names
  4. 4. Distinguish ON from accidental row-by-row pairing
  5. 5. Put a right-table filter in ON when unmatched left rows must survive
  6. 6. Recognise when WHERE removes LEFT JOIN null-extended rows
  7. 7. Use COUNT of a child key for genuine matches
  8. 8. Find parents without children using IS NULL
  9. 9. Use EXISTS for a presence question without multiplying rows
  10. 10. Audit orphaned child records carefully
  11. 11. Summarise matches per parent at the right grain
  12. 12. Aggregate children before a join when one row per parent is needed
  13. 13. Contrast INNER and LEFT when score conditions are added
  14. 14. Avoid joins on names, labels or other non-unique attributes
  15. 15. Use USING only when both columns truly mean the same key
  16. 16. Match composite identifiers when one column is not enough
  17. 17. Distinguish self joins from two different people
  18. 18. Spot an accidental Cartesian product
  19. 19. Use ORDER BY for presentation without changing membership
  20. 20. Reconcile left counts, matched pairs and missing parents

Start here: the original compact lesson

The following foundation is preserved and developed from the earlier checkpoint for this exact article. Work through it once; the extensive practice chapters below turn its principles into verifiable decisions. If a snippet refers to a local demonstration server or invented records, treat that environment as hypothetical until you set it up yourself.

A learner can write a SQL query that runs without errors and still report the wrong number of pupils or homework submissions. The reason is often a join. A join connects rows from different tables through a stated relationship. To master it, do not begin by asking whether to type INNER JOIN or LEFT JOIN; begin by asking what a row in the final answer is supposed to represent, and which unmatched records must remain visible.

Imagine a small, fictional project ledger with three students and three quiz submissions. Student A has two submissions, B has one, and C has none. An inner join of students to submissions produces three submission-linked rows: A twice and B once. A left join that preserves all students produces four rows: the same three matches plus C paired with null submission fields. Those are different results for different questions. One asks for existing submission matches; the other asks for every student, including the one without a submission. This guide teaches the distinction, why repeated matches multiply rows, how missing data become NULL, and how to audit totals before passing a report to somebody else.

All names and data below are invented for practice. They do not describe real children, Punggol schools, school record systems, grade results or any advertised eduKate tuition service. Use a disposable local database containing only fictional data; never place real pupils’ personal information in an unapproved training dataset.

1. Read table grain before attempting the relationship

The first table contains one row per student. The second contains one row per submitted quiz. These grains differ deliberately.

Students

| student_id | name | |—:|—| | 1 | A | | 2 | B | | 3 | C |

Submissions

| submission_id | student_id | quiz | score | |—:|—:|—|—:| | 101 | 1 | Q1 | 8 | | 102 | 1 | Q2 | 9 | | 103 | 2 | Q1 | 7 |

Before joining, ask how many student rows exist and how many submission rows exist. There are three of each. That does not mean joining will necessarily yield three rows, because a row for student A matches two submission rows. The important feature is not equal table length but the relationship between key values. A student identifier can be unique in the students table and repeated in the submissions table, as expected in a one-to-many relationship.

If a learner says “three students must mean three rows after every join,” help them locate the repeated student_id=1 in the submission table. Ask which of the two submissions should be thrown away if the result is forced into one row. Neither should be discarded without a stated aggregation rule.

2. Build the smallest executable example

The SQL below uses ordinary relational concepts and can be run in a disposable PostgreSQL or SQLite practice database, with small syntax adjustments only when necessary. The CREATE TABLE declarations make the intended keys visible. The foreign key references a student; a successful foreign-key constraint requires the database to enforce it, so test that setting separately when using SQLite.

CREATE TABLE students (
  student_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE submissions (
  submission_id INTEGER PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students(student_id),
  quiz TEXT NOT NULL,
  score INTEGER NOT NULL
);

INSERT INTO students(student_id, name)
VALUES (1, 'A'), (2, 'B'), (3, 'C');

INSERT INTO submissions(submission_id, student_id, quiz, score)
VALUES (101, 1, 'Q1', 8),
       (102, 1, 'Q2', 9),
       (103, 2, 'Q1', 7);

Keep identifiers fictional and local. A real school database involves consent, security, access controls and retention policy well outside this learning exercise. The point is to understand matching logic, not acquire access to any production record system.

3. INNER JOIN keeps only matching pairs

SELECT s.student_id, s.name, u.quiz, u.score
FROM students AS s
INNER JOIN submissions AS u
  ON s.student_id = u.student_id
ORDER BY s.student_id, u.submission_id;

The output consists of (1,A,Q1,8), (1,A,Q2,9), (2,B,Q1,7). There is no row for C because no submission matches student_id=3. The join is not denying that C exists. It is selecting pairs of records for which the predicate in ON is true. Describe the result as three matched submission records, not “three students.”

A useful transfer exercise replaces the student table with a list of borrowed library items and the submission table with borrow events. One library item may have many events. If you join events to items, each event is still a separate record. The general reasoning—one parent row can match many child rows—remains the same.

Practice with an explained answer

If A submits a third quiz with a score of 6, how many rows will the inner join produce? Four. Student A now appears three times and B once. If a dashboard of “number of students” changes from two to four merely because of another quiz submission, the dashboard is counting joined rows, not distinct learners.

4. LEFT JOIN preserves unmatched rows from the left input

SELECT s.student_id, s.name, u.quiz, u.score
FROM students AS s
LEFT JOIN submissions AS u
  ON s.student_id = u.student_id
ORDER BY s.student_id, u.submission_id;

The first three rows match the inner-join output. The fourth row is (3,C,NULL,NULL). The fourth row represents student C with no matched submission record. The null quiz and score do not mean a zero-mark attempt. They mean those values are unavailable because there is no corresponding submission row. Replacing this with a fabricated zero can mislead a teacher or parent.

A learner can trace the left join by going through each student. For A, find two matches and output two rows. For B, find one match and output one row. For C, find none and output a single row with null values on the submission side. The count is 2 + 1 + 1 = 4. There is no requirement that the result count equal the number of left-table rows, only that unmatched left rows remain represented.

5. COUNT(*) and COUNT(child_id) answer different questions

SELECT
  COUNT(*) AS joined_rows,
  COUNT(u.submission_id) AS actual_submissions,
  COUNT(DISTINCT s.student_id) AS distinct_students
FROM students AS s
LEFT JOIN submissions AS u
  ON s.student_id = u.student_id;

The correct outputs are joined_rows=4, actual_submissions=3, and distinct_students=3. COUNT(*) counts the null-extended row created for C; COUNT(u.submission_id) excludes that row because the selected child ID is null. COUNT(DISTINCT s.student_id) counts the three students represented after joining. Each expression is consistent with its own definition. None is an all-purpose “count people” operator.

This is a strong diagnostic because the values are close enough to be easily confused. Ask the learner to explain why COUNT(*) is four even though only three actual submissions exist. If they answer “the database duplicated someone by mistake,” return to the expected one-to-many relationship and the unmatched left row. When a numerical report is surprising, inspect what exactly a counted row means.

6. WHERE can undo the purpose of a LEFT JOIN

This query appears to preserve every student but will drop C:

SELECT s.name, u.quiz
FROM students AS s
LEFT JOIN submissions AS u
  ON s.student_id = u.student_id
WHERE u.quiz = 'Q1';

The left join initially retains C as a null-extended row. The WHERE condition then asks whether its u.quiz equals Q1. For C, that expression is unknown rather than true, so the row does not pass the filter. The result includes A Q1 and B Q1 but not C. If the user wanted “all students and any matching Q1 submission,” put the restriction into the join condition:

SELECT s.name, u.quiz
FROM students AS s
LEFT JOIN submissions AS u
  ON s.student_id = u.student_id AND u.quiz = 'Q1';

Now the result includes A Q1, B Q1 and C NULL. Putting a right-side condition in ON specifies what qualifies as a match; putting it in WHERE filters the output rows after the outer join. This is a semantic decision, not a style preference.

A parent can prompt, “Do we need to display pupils without a Q1 record?” If yes, the second query better matches the intended population. If the task asks to list only actual Q1 submissions, the first query is appropriate. The same SQL keyword is not “right” independently of the reader’s job.

7. Missing matches are not always errors

A student having no submitted quiz could indicate absence, unfinished work, an optional assessment or a data-entry problem. A left join reveals the missing relationship but does not diagnose the cause. The reporting phrase should therefore be “no matching submission in this table” rather than “this pupil did not study” or “this pupil scored zero.” Distinguish a database observation from a behavioural inference.

In a practice data-repair exercise, add student D to the students table but no submission. The left join now produces one additional null-extended row. A group count of students without any submissions increases from one to two. However, the number of existing submissions remains three. The reliable result is a pair of quantities, not just one: four registered students and three recorded quiz attempts across two submitting students.

8. Duplicate keys can cause multiplicative explosions

Consider an invented mistake: a second table named club_memberships contains two memberships for student A and two for B. Joining students to submissions and then to memberships by student_id can produce multiple copies of each submission if there are multiple membership matches. A has two submissions and two memberships, yielding four combined rows for A. This is not random duplication; it is a consequence of the requested combinations.

SELECT s.name, u.quiz, m.club
FROM students AS s
JOIN submissions AS u ON s.student_id = u.student_id
JOIN club_memberships AS m ON s.student_id = m.student_id;

The learner should predict row counts using match multiplicity. For a given student, number of submissions times number of memberships determines that student’s contribution to this join. When a report unexpectedly doubles scores after adding a club table, do not “repair” it with DISTINCT before understanding the cardinality. A deduplicated-looking result can conceal a wrong relationship and drop legitimate records.

9. Aggregating after a join requires choosing a grouping grain

To report one row per student, define the required summary before joining or group carefully after joining:

SELECT s.student_id, s.name,
       COUNT(u.submission_id) AS submission_count,
       AVG(u.score) AS mean_recorded_score
FROM students AS s
LEFT JOIN submissions AS u
  ON s.student_id = u.student_id
GROUP BY s.student_id, s.name
ORDER BY s.student_id;

Student A has two attempts and an average score 8.5; B has one attempt with average 7; C has zero attempts and a null mean. The null for C is not a failure of the calculation. An average of no recorded scores is not meaningfully zero. You can display “No attempt recorded” in the presentation layer rather than tampering with the numeric result.

If the learner uses COUNT(*) instead, C incorrectly appears to have one submission in the left-joined result. This is a common trap. Ask the student to point to the real child identifier being counted and explain why a null-extended row must not count as an actual quiz attempt.

10. Keys should be stable and unambiguous

Names such as “A” or “Student One” are friendly for display but weak as join keys. Two students may share a name; names may be misspelled or updated. Use stable identifiers for the relationship, while retaining readable names for the report. In this toy example the integer student_id is enough. In a real system, key design also involves access control, privacy, migration and lifecycle policies; these are not problems to solve by exposing private school identifiers in a classroom spreadsheet.

A diagnostic challenge asks what would happen if there were two students named A. A join on name could create unintended cross-matches or ambiguity, while a join on distinct student IDs keeps each record connected to the right entity. Reading a schema and noticing uniqueness constraints is a fundamental part of learning SQL.

11. Multi-column keys describe more precise relationships

Suppose two classes both have a student with local roll number 1. Joining submissions to students on roll_number alone could mix the classes. If the actual identity is (class_id, roll_number), then the join must match both columns. One table might have (ClassA, 1) and (ClassB, 1); they are different pupils despite sharing the number one.

ON s.class_id = u.class_id
AND s.roll_number = u.roll_number

Ask the learner to invent the smallest collision case and count the number of false matches under a one-column join. Two parent rows with the same roll number and two child rows can create four combinations, even if only two are legitimate. This is a powerful transfer lesson: the true identifier determines correctness, not how many fields are easiest to type.

12. A parent-friendly debugging route

When a join produces a surprising report, stop before adding another filter. First count the raw rows in each table. Next inspect uniqueness and nulls in the chosen keys. Then manually trace one parent record that has zero matches, one that has exactly one, and one that has several. Predict how many joined rows each will produce. Finally compare the resulting count with the explanation you gave to the end reader.

A useful exercise uses the three-student fictional dataset after a normal Punggol family’s evening study routine. Fifteen focused minutes can cover one inner join, one left join and a two-line explanation of the difference. A student who can correctly explain why C appears only in the left result is ready to test more difficult relationships. A student who cannot explain the fourth row should not yet add more tables; the simplest missing-match example remains the right next task.

Initial practice bank with explained answers

Question 1. Three students, three submissions; why can an inner join return three rows containing only two different students? Because A contributes two matched submissions, B one, and C none.

Question 2. Why does the left join have four rows? The unmatched C row is preserved with null submission fields, while A still has two matching rows.

Question 3. Why does COUNT(*) produce four after the left join? It counts all output rows, including the null-extended C row.

Question 4. How many actual submission IDs are present? Three. COUNT(u.submission_id) excludes C’s null field.

Question 5. If A submits a third quiz, what is the new count of submission-linked rows? Four; the left join then returns five rows including unmatched C.

Question 6. Why might WHERE u.quiz = 'Q1' remove C even after a left join? Because the null value is not true under that comparison, and the WHERE step filters it out.

Question 7. What does placing u.quiz = 'Q1' in the ON clause accomplish? It restricts which submissions match while preserving every student on the left, including C.

Question 8. Is C’s average score zero? Not from this data. No qualifying score exists, so the SQL average is null.

Question 9. Why is joining on names potentially unsafe? Names need not uniquely identify a person; stable keys reduce accidental cross-matches.

Question 10. Why should a surprising row count be investigated before using DISTINCT? Because multiplicative matches may reflect valid one-to-many relationships or an incorrect join predicate. DISTINCT can hide rather than repair either problem.

Read the official PostgreSQL Table Expressions guide for the exact relational semantics; transfer the principles to your chosen SQL engine only after checking engine-specific behaviour. For study routines, eduKatePunggol’s How Studying Works guide is a verified existing resource. None of the examples is a claim about a real school assessment.

CHAPTER 1 OF 20 · Back to contents

1. Start with the shared key, not two similar-looking columns

Start with the answer: An INNER JOIN returns combinations of left and right rows for which the ON predicate is true.

If submissions contains a student_id, match it against the students table primary key. A name, row position or matching display order is not a reliable substitute.

Common tempting error: Joining columns because their first few values seem to line up in a screenshot.

Diagnostic for a parent or project partner: What is the declared relationship between one parent and one child record?

Lab 1: matching student-submission pairs for a family revision tracker

The current problem is start with the shared key, not two similar-looking columns. In a family revision tracker, an imaginary database has 6 student rows and 6 submissions. The question is matching student-submission pairs. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',55,1);
INSERT INTO submissions VALUES(102,1,'Q2',67,2);
INSERT INTO submissions VALUES(103,3,'Q1',74,1);
INSERT INTO submissions VALUES(104,3,'Q2',86,2);
INSERT INTO submissions VALUES(105,5,'Q1',35,1);
INSERT INTO submissions VALUES(106,5,'Q2',51,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the start with the shared key, not two similar-looking columns exercise in a family revision tracker, the SQLite 3 calculation for matching student-submission pairs yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 55}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 67}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “quiz”: “Q1”, “score”: 74}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “quiz”: “Q2”, “score”: 86}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “quiz”: “Q1”, “score”: 35}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “quiz”: “Q2”, “score”: 51}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For start with the shared key, not two similar-looking columns in a family revision tracker, Only matching pairs survive and one student can occur more than once. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. Try this on a small, invented table before using any real schoolwork. For this a family revision tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 2: matching student-submission pairs for a small library-book experiment

The current problem is start with the shared key, not two similar-looking columns. A learner helping with a small library-book experiment expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',48,1);
INSERT INTO submissions VALUES(102,2,'Q1',47,1);
INSERT INTO submissions VALUES(103,2,'Q2',79,2);
INSERT INTO submissions VALUES(104,2,'Q3',85,3);
INSERT INTO submissions VALUES(105,3,'Q1',85,1);
INSERT INTO submissions VALUES(106,4,'Q1',95,1);
INSERT INTO submissions VALUES(107,4,'Q2',61,2);
INSERT INTO submissions VALUES(108,4,'Q3',93,3);
INSERT INTO submissions VALUES(109,5,'Q1',90,1);
INSERT INTO submissions VALUES(110,6,'Q1',87,1);
INSERT INTO submissions VALUES(111,6,'Q2',48,2);
INSERT INTO submissions VALUES(112,6,'Q3',63,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the start with the shared key, not two similar-looking columns exercise in a small library-book experiment, the SQLite 3 calculation for matching student-submission pairs yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 48}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q1”, “score”: 47}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 103, “quiz”: “Q2”, “score”: 79}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q3”, “score”: 85}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 85}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “quiz”: “Q1”, “score”: 95}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “quiz”: “Q2”, “score”: 61}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q3”, “score”: 93}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For start with the shared key, not two similar-looking columns in a small library-book experiment, Only matching pairs survive and one student can occur more than once. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. No real pupil information is needed; the test data are deliberately imaginary. For this a small library-book experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 3: matching student-submission pairs for an after-CCA practice planner

The current problem is start with the shared key, not two similar-looking columns. The activity log for an after-CCA practice planner contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',60,2);
INSERT INTO submissions VALUES(102,2,'Q1',86,2);
INSERT INTO submissions VALUES(103,2,'Q2',63,3);
INSERT INTO submissions VALUES(104,2,'Q3',93,1);
INSERT INTO submissions VALUES(105,3,'Q1',88,2);
INSERT INTO submissions VALUES(106,4,'Q1',48,2);
INSERT INTO submissions VALUES(107,4,'Q2',40,3);
INSERT INTO submissions VALUES(108,4,'Q3',54,1);
INSERT INTO submissions VALUES(109,5,'Q1',70,2);
INSERT INTO submissions VALUES(110,6,'Q1',62,2);
INSERT INTO submissions VALUES(111,6,'Q2',75,3);
INSERT INTO submissions VALUES(112,6,'Q3',82,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the start with the shared key, not two similar-looking columns exercise in an after-CCA practice planner, the SQLite 3 calculation for matching student-submission pairs yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 60}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q1”, “score”: 86}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 103, “quiz”: “Q2”, “score”: 63}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q3”, “score”: 93}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 88}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “quiz”: “Q1”, “score”: 48}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “quiz”: “Q2”, “score”: 40}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q3”, “score”: 54}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For start with the shared key, not two similar-looking columns in an after-CCA practice planner, Only matching pairs survive and one student can occur more than once. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Independent checking beats a polished screen that nobody can explain. For this an after-CCA practice planner, require the learner to describe what one query row represents before publishing a finding.

Lab 4: matching student-submission pairs for a school-holiday project notebook

The current problem is start with the shared key, not two similar-looking columns. A project partner has copied a query for a school-holiday project notebook but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',53,2);
INSERT INTO submissions VALUES(102,2,'Q2',79,3);
INSERT INTO submissions VALUES(103,4,'Q1',63,2);
INSERT INTO submissions VALUES(104,4,'Q2',44,3);
INSERT INTO submissions VALUES(105,6,'Q1',57,2);
INSERT INTO submissions VALUES(106,6,'Q2',86,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the start with the shared key, not two similar-looking columns exercise in a school-holiday project notebook, the SQLite 3 calculation for matching student-submission pairs yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “quiz”: “Q1”, “score”: 53}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q2”, “score”: 79}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “quiz”: “Q1”, “score”: 63}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “quiz”: “Q2”, “score”: 44}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “quiz”: “Q1”, “score”: 57}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 106, “quiz”: “Q2”, “score”: 86}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For start with the shared key, not two similar-looking columns in a school-holiday project notebook, Only matching pairs survive and one student can occur more than once. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Save the example so the learner can rerun it with a fresh number next week. For this a school-holiday project notebook, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe start with the shared key, not two similar-looking columns in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: State the join predicate before writing SELECT fields. Try this on a small, invented table before using any real schoolwork.

CHAPTER 2 OF 20 · Back to contents

2. Keep unmatched parents with LEFT JOIN

Start with the answer: A LEFT JOIN preserves every left-side student row, even when there is no matching submission.

The right-side columns on an unmatched output are NULL. The absence of submissions is a meaningful observation but not proof of a pupil’s motivation or ability.

Common tempting error: Expecting LEFT JOIN to remove students with no submissions.

Diagnostic for a parent or project partner: Which student rows were preserved when no child record existed?

Lab 1: LEFT-preserved students and matches for a fictional class activity log

The current problem is keep unmatched parents with left join. In a fictional class activity log, an imaginary database has 6 student rows and 12 submissions. The question is LEFT-preserved students and matches. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',89,2);
INSERT INTO submissions VALUES(102,1,'Q2',46,3);
INSERT INTO submissions VALUES(103,1,'Q3',57,1);
INSERT INTO submissions VALUES(104,2,'Q1',87,2);
INSERT INTO submissions VALUES(105,3,'Q1',40,2);
INSERT INTO submissions VALUES(106,3,'Q2',81,3);
INSERT INTO submissions VALUES(107,3,'Q3',73,1);
INSERT INTO submissions VALUES(108,4,'Q1',85,2);
INSERT INTO submissions VALUES(109,5,'Q1',54,2);
INSERT INTO submissions VALUES(110,5,'Q2',36,3);
INSERT INTO submissions VALUES(111,5,'Q3',72,1);
INSERT INTO submissions VALUES(112,6,'Q1',94,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the keep unmatched parents with left join exercise in a fictional class activity log, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 89}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 46}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “quiz”: “Q3”, “score”: 57}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q1”, “score”: 87}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 40}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “quiz”: “Q2”, “score”: 81}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “quiz”: “Q3”, “score”: 73}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q1”, “score”: 85}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For keep unmatched parents with left join in a fictional class activity log, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. This is a proposed home exercise rather than a service or school claim. For this a fictional class activity log, require the learner to describe what one query row represents before publishing a finding.

Lab 2: LEFT-preserved students and matches for a coastal ecology observation workbook

The current problem is keep unmatched parents with left join. A learner helping with a coastal ecology observation workbook expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',71,2);
INSERT INTO submissions VALUES(102,1,'Q2',49,3);
INSERT INTO submissions VALUES(103,3,'Q1',82,2);
INSERT INTO submissions VALUES(104,3,'Q2',58,3);
INSERT INTO submissions VALUES(105,5,'Q1',56,2);
INSERT INTO submissions VALUES(106,5,'Q2',88,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the keep unmatched parents with left join exercise in a coastal ecology observation workbook, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 71}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 49}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “quiz”: “Q1”, “score”: 82}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “quiz”: “Q2”, “score”: 58}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “quiz”: “Q1”, “score”: 56}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “quiz”: “Q2”, “score”: 88}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For keep unmatched parents with left join in a coastal ecology observation workbook, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. A useful parent prompt is to request a prediction, not a memorised command. For this a coastal ecology observation workbook, require the learner to describe what one query row represents before publishing a finding.

Lab 3: LEFT-preserved students and matches for a recycled-materials tally

The current problem is keep unmatched parents with left join. The activity log for a recycled-materials tally contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',57,3);
INSERT INTO submissions VALUES(102,1,'Q2',88,1);
INSERT INTO submissions VALUES(103,3,'Q1',52,3);
INSERT INTO submissions VALUES(104,3,'Q2',41,1);
INSERT INTO submissions VALUES(105,5,'Q1',72,3);
INSERT INTO submissions VALUES(106,5,'Q2',81,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the keep unmatched parents with left join exercise in a recycled-materials tally, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 57}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 88}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “quiz”: “Q1”, “score”: 52}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “quiz”: “Q2”, “score”: 41}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “quiz”: “Q1”, “score”: 72}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “quiz”: “Q2”, “score”: 81}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For keep unmatched parents with left join in a recycled-materials tally, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. The next step should be one small controlled variation, not ten simultaneous edits. For this a recycled-materials tally, require the learner to describe what one query row represents before publishing a finding.

Lab 4: LEFT-preserved students and matches for a home garden observation chart

The current problem is keep unmatched parents with left join. A project partner has copied a query for a home garden observation chart but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',71,3);
INSERT INTO submissions VALUES(102,2,'Q1',77,3);
INSERT INTO submissions VALUES(103,2,'Q2',74,1);
INSERT INTO submissions VALUES(104,2,'Q3',84,2);
INSERT INTO submissions VALUES(105,3,'Q1',45,3);
INSERT INTO submissions VALUES(106,4,'Q1',66,3);
INSERT INTO submissions VALUES(107,4,'Q2',58,1);
INSERT INTO submissions VALUES(108,4,'Q3',73,2);
INSERT INTO submissions VALUES(109,5,'Q1',62,3);
INSERT INTO submissions VALUES(110,6,'Q1',49,3);
INSERT INTO submissions VALUES(111,6,'Q2',36,1);
INSERT INTO submissions VALUES(112,6,'Q3',81,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the keep unmatched parents with left join exercise in a home garden observation chart, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 71}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q1”, “score”: 77}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 103, “quiz”: “Q2”, “score”: 74}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q3”, “score”: 84}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 45}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “quiz”: “Q1”, “score”: 66}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “quiz”: “Q2”, “score”: 58}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q3”, “score”: 73}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For keep unmatched parents with left join in a home garden observation chart, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A clear explanation is more valuable than guessing an answer from the display. For this a home garden observation chart, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe keep unmatched parents with left join in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Use LEFT when the question explicitly includes all left-hand records. This is a proposed home exercise rather than a service or school claim.

CHAPTER 3 OF 20 · Back to contents

3. Explain why one-to-many joins repeat names

Start with the answer: One student with three submissions produces three joined rows; this is multiplication by matching child records, not a duplicate bug.

A left row can match more than one right row, so the output grain becomes the pair of matching records. Aggregation may be needed for one row per pupil.

Common tempting error: Deleting legitimate child rows to force the result into one parent row.

Diagnostic for a parent or project partner: Which three distinct submissions explain the repeated name?

Lab 1: LEFT-preserved students and matches for a weekend reading journal

The current problem is explain why one-to-many joins repeat names. In a weekend reading journal, an imaginary database has 6 student rows and 6 submissions. The question is LEFT-preserved students and matches. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',51,3);
INSERT INTO submissions VALUES(102,2,'Q2',85,1);
INSERT INTO submissions VALUES(103,4,'Q1',54,3);
INSERT INTO submissions VALUES(104,4,'Q2',77,1);
INSERT INTO submissions VALUES(105,6,'Q1',51,3);
INSERT INTO submissions VALUES(106,6,'Q2',61,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the explain why one-to-many joins repeat names exercise in a weekend reading journal, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “quiz”: “Q1”, “score”: 51}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q2”, “score”: 85}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “quiz”: “Q1”, “score”: 54}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “quiz”: “Q2”, “score”: 77}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “quiz”: “Q1”, “score”: 51}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For explain why one-to-many joins repeat names in a weekend reading journal, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a weekend reading journal, require the learner to describe what one query row represents before publishing a finding.

Lab 2: LEFT-preserved students and matches for a simulated science-club project

The current problem is explain why one-to-many joins repeat names. A learner helping with a simulated science-club project expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',96,3);
INSERT INTO submissions VALUES(102,1,'Q2',59,1);
INSERT INTO submissions VALUES(103,1,'Q3',44,2);
INSERT INTO submissions VALUES(104,2,'Q1',48,3);
INSERT INTO submissions VALUES(105,3,'Q1',64,3);
INSERT INTO submissions VALUES(106,3,'Q2',49,1);
INSERT INTO submissions VALUES(107,3,'Q3',41,2);
INSERT INTO submissions VALUES(108,4,'Q1',92,3);
INSERT INTO submissions VALUES(109,5,'Q1',73,3);
INSERT INTO submissions VALUES(110,5,'Q2',42,1);
INSERT INTO submissions VALUES(111,5,'Q3',47,2);
INSERT INTO submissions VALUES(112,6,'Q1',72,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the explain why one-to-many joins repeat names exercise in a simulated science-club project, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 96}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 59}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “quiz”: “Q3”, “score”: 44}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q1”, “score”: 48}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 64}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “quiz”: “Q2”, “score”: 49}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “quiz”: “Q3”, “score”: 41}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q1”, “score”: 92}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For explain why one-to-many joins repeat names in a simulated science-club project, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a simulated science-club project, require the learner to describe what one query row represents before publishing a finding.

Lab 3: LEFT-preserved students and matches for a mock group presentation tracker

The current problem is explain why one-to-many joins repeat names. The activity log for a mock group presentation tracker contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',69,1);
INSERT INTO submissions VALUES(102,1,'Q2',81,2);
INSERT INTO submissions VALUES(103,1,'Q3',62,3);
INSERT INTO submissions VALUES(104,2,'Q1',40,1);
INSERT INTO submissions VALUES(105,3,'Q1',82,1);
INSERT INTO submissions VALUES(106,3,'Q2',49,2);
INSERT INTO submissions VALUES(107,3,'Q3',69,3);
INSERT INTO submissions VALUES(108,4,'Q1',82,1);
INSERT INTO submissions VALUES(109,5,'Q1',55,1);
INSERT INTO submissions VALUES(110,5,'Q2',75,2);
INSERT INTO submissions VALUES(111,5,'Q3',48,3);
INSERT INTO submissions VALUES(112,6,'Q1',57,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the explain why one-to-many joins repeat names exercise in a mock group presentation tracker, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 69}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 81}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “quiz”: “Q3”, “score”: 62}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q1”, “score”: 40}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 82}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “quiz”: “Q2”, “score”: 49}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “quiz”: “Q3”, “score”: 69}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q1”, “score”: 82}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For explain why one-to-many joins repeat names in a mock group presentation tracker, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Treat a version-dependent result as a prompt to consult the current official manual. For this a mock group presentation tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 4: LEFT-preserved students and matches for a volunteer-hours planning exercise

The current problem is explain why one-to-many joins repeat names. A project partner has copied a query for a volunteer-hours planning exercise but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',47,1);
INSERT INTO submissions VALUES(102,1,'Q2',66,2);
INSERT INTO submissions VALUES(103,3,'Q1',77,1);
INSERT INTO submissions VALUES(104,3,'Q2',72,2);
INSERT INTO submissions VALUES(105,5,'Q1',81,1);
INSERT INTO submissions VALUES(106,5,'Q2',63,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the explain why one-to-many joins repeat names exercise in a volunteer-hours planning exercise, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 47}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 66}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “quiz”: “Q1”, “score”: 77}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “quiz”: “Q2”, “score”: 72}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “quiz”: “Q1”, “score”: 81}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “quiz”: “Q2”, “score”: 63}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For explain why one-to-many joins repeat names in a volunteer-hours planning exercise, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Try this on a small, invented table before using any real schoolwork. For this a volunteer-hours planning exercise, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe explain why one-to-many joins repeat names in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Name the output grain before building a chart or count. A Punggol family can practise this with fictional numbers after dinner or over a weekend.

CHAPTER 4 OF 20 · Back to contents

4. Distinguish ON from accidental row-by-row pairing

Start with the answer: ON defines which pairs are related; a SQL table has no guaranteed natural row order.

A join on actual foreign and primary keys is about identity, not row number. ORDER BY changes presentation, not the meaning of matching.

Common tempting error: Pairing record 1 with record 1 because they happened to sort similarly.

Diagnostic for a parent or project partner: What will still match if the tables are reinserted in a different order?

Lab 1: LEFT-preserved students and matches for a small library-book experiment

The current problem is distinguish on from accidental row-by-row pairing. In a small library-book experiment, an imaginary database has 6 student rows and 12 submissions. The question is LEFT-preserved students and matches. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',64,1);
INSERT INTO submissions VALUES(102,2,'Q1',42,1);
INSERT INTO submissions VALUES(103,2,'Q2',36,2);
INSERT INTO submissions VALUES(104,2,'Q3',62,3);
INSERT INTO submissions VALUES(105,3,'Q1',45,1);
INSERT INTO submissions VALUES(106,4,'Q1',37,1);
INSERT INTO submissions VALUES(107,4,'Q2',46,2);
INSERT INTO submissions VALUES(108,4,'Q3',70,3);
INSERT INTO submissions VALUES(109,5,'Q1',36,1);
INSERT INTO submissions VALUES(110,6,'Q1',74,1);
INSERT INTO submissions VALUES(111,6,'Q2',43,2);
INSERT INTO submissions VALUES(112,6,'Q3',89,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the distinguish on from accidental row-by-row pairing exercise in a small library-book experiment, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 64}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q1”, “score”: 42}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 103, “quiz”: “Q2”, “score”: 36}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q3”, “score”: 62}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 45}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “quiz”: “Q1”, “score”: 37}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “quiz”: “Q2”, “score”: 46}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q3”, “score”: 70}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish on from accidental row-by-row pairing in a small library-book experiment, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. No real pupil information is needed; the test data are deliberately imaginary. For this a small library-book experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 2: LEFT-preserved students and matches for a fictional student coding challenge

The current problem is distinguish on from accidental row-by-row pairing. A learner helping with a fictional student coding challenge expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',90,1);
INSERT INTO submissions VALUES(102,2,'Q2',42,2);
INSERT INTO submissions VALUES(103,4,'Q1',58,1);
INSERT INTO submissions VALUES(104,4,'Q2',63,2);
INSERT INTO submissions VALUES(105,6,'Q1',50,1);
INSERT INTO submissions VALUES(106,6,'Q2',47,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the distinguish on from accidental row-by-row pairing exercise in a fictional student coding challenge, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “quiz”: “Q1”, “score”: 90}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q2”, “score”: 42}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “quiz”: “Q1”, “score”: 58}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “quiz”: “Q2”, “score”: 63}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “quiz”: “Q1”, “score”: 50}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish on from accidental row-by-row pairing in a fictional student coding challenge, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional student coding challenge, require the learner to describe what one query row represents before publishing a finding.

Lab 3: LEFT-preserved students and matches for a school-holiday project notebook

The current problem is distinguish on from accidental row-by-row pairing. The activity log for a school-holiday project notebook contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',86,2);
INSERT INTO submissions VALUES(102,2,'Q2',76,3);
INSERT INTO submissions VALUES(103,4,'Q1',70,2);
INSERT INTO submissions VALUES(104,4,'Q2',94,3);
INSERT INTO submissions VALUES(105,6,'Q1',48,2);
INSERT INTO submissions VALUES(106,6,'Q2',41,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the distinguish on from accidental row-by-row pairing exercise in a school-holiday project notebook, the SQLite 3 calculation for LEFT-preserved students and matches yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “quiz”: “Q1”, “score”: 86}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “quiz”: “Q2”, “score”: 76}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “quiz”: “Q1”, “score”: 70}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “quiz”: “Q2”, “score”: 94}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “quiz”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “quiz”: “Q1”, “score”: 48}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish on from accidental row-by-row pairing in a school-holiday project notebook, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Save the example so the learner can rerun it with a fresh number next week. For this a school-holiday project notebook, require the learner to describe what one query row represents before publishing a finding.

Lab 4: LEFT-preserved students and matches for a made-up quiz analysis

The current problem is distinguish on from accidental row-by-row pairing. A project partner has copied a query for a made-up quiz analysis but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',62,2);
INSERT INTO submissions VALUES(102,1,'Q2',69,3);
INSERT INTO submissions VALUES(103,1,'Q3',57,1);
INSERT INTO submissions VALUES(104,2,'Q1',76,2);
INSERT INTO submissions VALUES(105,3,'Q1',47,2);
INSERT INTO submissions VALUES(106,3,'Q2',89,3);
INSERT INTO submissions VALUES(107,3,'Q3',59,1);
INSERT INTO submissions VALUES(108,4,'Q1',94,2);
INSERT INTO submissions VALUES(109,5,'Q1',50,2);
INSERT INTO submissions VALUES(110,5,'Q2',52,3);
INSERT INTO submissions VALUES(111,5,'Q3',95,1);
INSERT INTO submissions VALUES(112,6,'Q1',35,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.quiz,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.student_id,u.submission_id;

Worked answer. For the distinguish on from accidental row-by-row pairing exercise in a made-up quiz analysis, the SQLite 3 calculation for LEFT-preserved students and matches yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘quiz’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “quiz”: “Q1”, “score”: 62}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “quiz”: “Q2”, “score”: 69}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “quiz”: “Q3”, “score”: 57}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “quiz”: “Q1”, “score”: 76}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “quiz”: “Q1”, “score”: 47}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “quiz”: “Q2”, “score”: 89}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “quiz”: “Q3”, “score”: 59}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “quiz”: “Q1”, “score”: 94}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish on from accidental row-by-row pairing in a made-up quiz analysis, Every parent appears; a zero-match student gets one row with NULL submission fields. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. This is a proposed home exercise rather than a service or school claim. For this a made-up quiz analysis, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe distinguish on from accidental row-by-row pairing in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Use stable keys, not positional coincidences. No real pupil information is needed; the test data are deliberately imaginary.

CHAPTER 5 OF 20 · Back to contents

5. Put a right-table filter in ON when unmatched left rows must survive

Start with the answer: A condition on the right table inside LEFT JOIN ON restricts its matching child rows while preserving all left parents.

For instance, ON u.student_id=s.student_id AND u.score>=70 retains students even if they have no qualifying score; their child columns become NULL.

Common tempting error: Moving a filter to WHERE and assuming the same preserved-row result.

Diagnostic for a parent or project partner: Should a student with no qualifying mark still appear in the answer?

Lab 1: score-filtered matches in LEFT JOIN ON for a coastal ecology observation workbook

The current problem is put a right-table filter in on when unmatched left rows must survive. In a coastal ecology observation workbook, an imaginary database has 6 student rows and 6 submissions. The question is score-filtered matches in LEFT JOIN ON. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',87,2);
INSERT INTO submissions VALUES(102,1,'Q2',84,3);
INSERT INTO submissions VALUES(103,3,'Q1',43,2);
INSERT INTO submissions VALUES(104,3,'Q2',75,3);
INSERT INTO submissions VALUES(105,5,'Q1',64,2);
INSERT INTO submissions VALUES(106,5,'Q2',88,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id AND u.score>=64 ORDER BY s.student_id,u.submission_id;

Worked answer. For the put a right-table filter in on when unmatched left rows must survive exercise in a coastal ecology observation workbook, the SQLite 3 calculation for score-filtered matches in LEFT JOIN ON yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 87}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “score”: 84}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “score”: 75}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “score”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “score”: 64}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “score”: 88}, {“student_id”: 6, “name”: “Farid”, “submission_id”: null, “score”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For put a right-table filter in on when unmatched left rows must survive in a coastal ecology observation workbook, The score condition restricts right-side matches while preserving all students. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. A useful parent prompt is to request a prediction, not a memorised command. For this a coastal ecology observation workbook, require the learner to describe what one query row represents before publishing a finding.

Lab 2: score-filtered matches in LEFT JOIN ON for an after-CCA practice planner

The current problem is put a right-table filter in on when unmatched left rows must survive. A learner helping with an after-CCA practice planner expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',49,2);
INSERT INTO submissions VALUES(102,2,'Q1',48,2);
INSERT INTO submissions VALUES(103,2,'Q2',59,3);
INSERT INTO submissions VALUES(104,2,'Q3',93,1);
INSERT INTO submissions VALUES(105,3,'Q1',47,2);
INSERT INTO submissions VALUES(106,4,'Q1',59,2);
INSERT INTO submissions VALUES(107,4,'Q2',93,3);
INSERT INTO submissions VALUES(108,4,'Q3',82,1);
INSERT INTO submissions VALUES(109,5,'Q1',49,2);
INSERT INTO submissions VALUES(110,6,'Q1',96,2);
INSERT INTO submissions VALUES(111,6,'Q2',50,3);
INSERT INTO submissions VALUES(112,6,'Q3',72,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id AND u.score>=67 ORDER BY s.student_id,u.submission_id;

Worked answer. For the put a right-table filter in on when unmatched left rows must survive exercise in an after-CCA practice planner, the SQLite 3 calculation for score-filtered matches in LEFT JOIN ON yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “score”: 93}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “score”: 93}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 82}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 110, “score”: 96}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 112, “score”: 72}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For put a right-table filter in on when unmatched left rows must survive in an after-CCA practice planner, The score condition restricts right-side matches while preserving all students. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Independent checking beats a polished screen that nobody can explain. For this an after-CCA practice planner, require the learner to describe what one query row represents before publishing a finding.

Lab 3: score-filtered matches in LEFT JOIN ON for a home garden observation chart

The current problem is put a right-table filter in on when unmatched left rows must survive. The activity log for a home garden observation chart contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',38,3);
INSERT INTO submissions VALUES(102,2,'Q1',39,3);
INSERT INTO submissions VALUES(103,2,'Q2',53,1);
INSERT INTO submissions VALUES(104,2,'Q3',45,2);
INSERT INTO submissions VALUES(105,3,'Q1',45,3);
INSERT INTO submissions VALUES(106,4,'Q1',71,3);
INSERT INTO submissions VALUES(107,4,'Q2',35,1);
INSERT INTO submissions VALUES(108,4,'Q3',95,2);
INSERT INTO submissions VALUES(109,5,'Q1',66,3);
INSERT INTO submissions VALUES(110,6,'Q1',63,3);
INSERT INTO submissions VALUES(111,6,'Q2',95,1);
INSERT INTO submissions VALUES(112,6,'Q3',94,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id AND u.score>=71 ORDER BY s.student_id,u.submission_id;

Worked answer. For the put a right-table filter in on when unmatched left rows must survive exercise in a home garden observation chart, the SQLite 3 calculation for score-filtered matches in LEFT JOIN ON yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “score”: 71}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 95}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 111, “score”: 95}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 112, “score”: 94}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For put a right-table filter in on when unmatched left rows must survive in a home garden observation chart, The score condition restricts right-side matches while preserving all students. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. A clear explanation is more valuable than guessing an answer from the display. For this a home garden observation chart, require the learner to describe what one query row represents before publishing a finding.

Lab 4: score-filtered matches in LEFT JOIN ON for a mock fundraising spreadsheet

The current problem is put a right-table filter in on when unmatched left rows must survive. A project partner has copied a query for a mock fundraising spreadsheet but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',93,3);
INSERT INTO submissions VALUES(102,2,'Q2',38,1);
INSERT INTO submissions VALUES(103,4,'Q1',50,3);
INSERT INTO submissions VALUES(104,4,'Q2',78,1);
INSERT INTO submissions VALUES(105,6,'Q1',45,3);
INSERT INTO submissions VALUES(106,6,'Q2',85,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id AND u.score>=74 ORDER BY s.student_id,u.submission_id;

Worked answer. For the put a right-table filter in on when unmatched left rows must survive exercise in a mock fundraising spreadsheet, the SQLite 3 calculation for score-filtered matches in LEFT JOIN ON yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “score”: 93}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “score”: 78}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 106, “score”: 85}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For put a right-table filter in on when unmatched left rows must survive in a mock fundraising spreadsheet, The score condition restricts right-side matches while preserving all students. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a mock fundraising spreadsheet, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe put a right-table filter in on when unmatched left rows must survive in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Draw the expected unmatched rows before placing a predicate. A useful parent prompt is to request a prediction, not a memorised command.

CHAPTER 6 OF 20 · Back to contents

6. Recognise when WHERE removes LEFT JOIN null-extended rows

Start with the answer: A WHERE condition on a nullable right-column value can remove the unmatched rows produced by LEFT JOIN.

WHERE u.score>=70 filters after joining; NULL fails that condition. This differs from placing the condition in ON, which preserves left rows without qualifying children.

Common tempting error: Believing LEFT JOIN always guarantees every parent after arbitrary WHERE conditions.

Diagnostic for a parent or project partner: Which stage is excluding the NULL-extended row?

Lab 1: score-filtering in WHERE after LEFT JOIN for a simulated science-club project

The current problem is recognise when where removes left join null-extended rows. In a simulated science-club project, an imaginary database has 6 student rows and 12 submissions. The question is score-filtering in WHERE after LEFT JOIN. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',91,3);
INSERT INTO submissions VALUES(102,1,'Q2',63,1);
INSERT INTO submissions VALUES(103,1,'Q3',68,2);
INSERT INTO submissions VALUES(104,2,'Q1',81,3);
INSERT INTO submissions VALUES(105,3,'Q1',64,3);
INSERT INTO submissions VALUES(106,3,'Q2',71,1);
INSERT INTO submissions VALUES(107,3,'Q3',60,2);
INSERT INTO submissions VALUES(108,4,'Q1',77,3);
INSERT INTO submissions VALUES(109,5,'Q1',67,3);
INSERT INTO submissions VALUES(110,5,'Q2',96,1);
INSERT INTO submissions VALUES(111,5,'Q3',69,2);
INSERT INTO submissions VALUES(112,6,'Q1',53,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.score>=65 ORDER BY s.student_id,u.submission_id;

Worked answer. For the recognise when where removes left join null-extended rows exercise in a simulated science-club project, the SQLite 3 calculation for score-filtering in WHERE after LEFT JOIN yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 91}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “score”: 68}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “score”: 81}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “score”: 71}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 77}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 109, “score”: 67}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 110, “score”: 96}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 111, “score”: 69}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For recognise when where removes left join null-extended rows in a simulated science-club project, The WHERE comparison excludes NULL right rows, so unmatched students disappear from this filtered result. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a simulated science-club project, require the learner to describe what one query row represents before publishing a finding.

Lab 2: score-filtering in WHERE after LEFT JOIN for a recycled-materials tally

The current problem is recognise when where removes left join null-extended rows. A learner helping with a recycled-materials tally expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',61,3);
INSERT INTO submissions VALUES(102,1,'Q2',54,1);
INSERT INTO submissions VALUES(103,3,'Q1',50,3);
INSERT INTO submissions VALUES(104,3,'Q2',97,1);
INSERT INTO submissions VALUES(105,5,'Q1',74,3);
INSERT INTO submissions VALUES(106,5,'Q2',94,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.score>=68 ORDER BY s.student_id,u.submission_id;

Worked answer. For the recognise when where removes left join null-extended rows exercise in a recycled-materials tally, the SQLite 3 calculation for score-filtering in WHERE after LEFT JOIN yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “score”: 97}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “score”: 74}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “score”: 94}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For recognise when where removes left join null-extended rows in a recycled-materials tally, The WHERE comparison excludes NULL right rows, so unmatched students disappear from this filtered result. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. The next step should be one small controlled variation, not ten simultaneous edits. For this a recycled-materials tally, require the learner to describe what one query row represents before publishing a finding.

Lab 3: score-filtering in WHERE after LEFT JOIN for a volunteer-hours planning exercise

The current problem is recognise when where removes left join null-extended rows. The activity log for a volunteer-hours planning exercise contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',63,1);
INSERT INTO submissions VALUES(102,1,'Q2',82,2);
INSERT INTO submissions VALUES(103,3,'Q1',41,1);
INSERT INTO submissions VALUES(104,3,'Q2',95,2);
INSERT INTO submissions VALUES(105,5,'Q1',66,1);
INSERT INTO submissions VALUES(106,5,'Q2',95,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.score>=72 ORDER BY s.student_id,u.submission_id;

Worked answer. For the recognise when where removes left join null-extended rows exercise in a volunteer-hours planning exercise, the SQLite 3 calculation for score-filtering in WHERE after LEFT JOIN yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “score”: 82}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “score”: 95}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “score”: 95}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For recognise when where removes left join null-extended rows in a volunteer-hours planning exercise, The WHERE comparison excludes NULL right rows, so unmatched students disappear from this filtered result. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Try this on a small, invented table before using any real schoolwork. For this a volunteer-hours planning exercise, require the learner to describe what one query row represents before publishing a finding.

Lab 4: score-filtering in WHERE after LEFT JOIN for a public-transport timing experiment

The current problem is recognise when where removes left join null-extended rows. A project partner has copied a query for a public-transport timing experiment but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',63,1);
INSERT INTO submissions VALUES(102,2,'Q1',50,1);
INSERT INTO submissions VALUES(103,2,'Q2',91,2);
INSERT INTO submissions VALUES(104,2,'Q3',50,3);
INSERT INTO submissions VALUES(105,3,'Q1',66,1);
INSERT INTO submissions VALUES(106,4,'Q1',73,1);
INSERT INTO submissions VALUES(107,4,'Q2',60,2);
INSERT INTO submissions VALUES(108,4,'Q3',52,3);
INSERT INTO submissions VALUES(109,5,'Q1',78,1);
INSERT INTO submissions VALUES(110,6,'Q1',79,1);
INSERT INTO submissions VALUES(111,6,'Q2',56,2);
INSERT INTO submissions VALUES(112,6,'Q3',46,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.score>=75 ORDER BY s.student_id,u.submission_id;

Worked answer. For the recognise when where removes left join null-extended rows exercise in a public-transport timing experiment, the SQLite 3 calculation for score-filtering in WHERE after LEFT JOIN yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 2, “name”: “Ben”, “submission_id”: 103, “score”: 91}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 109, “score”: 78}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 110, “score”: 79}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For recognise when where removes left join null-extended rows in a public-transport timing experiment, The WHERE comparison excludes NULL right rows, so unmatched students disappear from this filtered result. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. No real pupil information is needed; the test data are deliberately imaginary. For this a public-transport timing experiment, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe recognise when where removes left join null-extended rows in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Compare ON and WHERE variants side by side to choose deliberately. The goal is not to finish a huge worksheet; it is to explain one reliable decision.

CHAPTER 7 OF 20 · Back to contents

7. Use COUNT of a child key for genuine matches

Start with the answer: COUNT(*) counts joined output rows including a preserved unmatched parent row; COUNT(u.submission_id) counts only non-null child keys.

When a student has no submission, a LEFT JOIN group still contains one placeholder row. COUNT(*) may say one while the number of actual submissions is zero.

Common tempting error: Calling the placeholder row a completed activity.

Diagnostic for a parent or project partner: Which expression really counts the related child records?

Lab 1: COUNT(*) versus COUNT(child primary key) for a fictional student coding challenge

The current problem is use count of a child key for genuine matches. In a fictional student coding challenge, an imaginary database has 6 student rows and 6 submissions. The question is COUNT(*) versus COUNT(child primary key). Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',35,1);
INSERT INTO submissions VALUES(102,2,'Q2',42,2);
INSERT INTO submissions VALUES(103,4,'Q1',72,1);
INSERT INTO submissions VALUES(104,4,'Q2',93,2);
INSERT INTO submissions VALUES(105,6,'Q1',79,1);
INSERT INTO submissions VALUES(106,6,'Q2',58,2);

-- Checked query:
SELECT s.student_id,s.name,COUNT(*) AS joined_rows, COUNT(u.submission_id) AS real_submissions FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the use count of a child key for genuine matches exercise in a fictional student coding challenge, the SQLite 3 calculation for COUNT(*) versus COUNT(child primary key) yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘joined_rows’, ‘real_submissions’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “joined_rows”: 1, “real_submissions”: 0}, {“student_id”: 2, “name”: “Ben”, “joined_rows”: 2, “real_submissions”: 2}, {“student_id”: 3, “name”: “Chloe”, “joined_rows”: 1, “real_submissions”: 0}, {“student_id”: 4, “name”: “Dev”, “joined_rows”: 2, “real_submissions”: 2}, {“student_id”: 5, “name”: “Esha”, “joined_rows”: 1, “real_submissions”: 0}, {“student_id”: 6, “name”: “Farid”, “joined_rows”: 2, “real_submissions”: 2}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use count of a child key for genuine matches in a fictional student coding challenge, For unmatched parents, COUNT(*) is one while COUNT(u.submission_id) is zero. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional student coding challenge, require the learner to describe what one query row represents before publishing a finding.

Lab 2: COUNT(*) versus COUNT(child primary key) for a mock group presentation tracker

The current problem is use count of a child key for genuine matches. A learner helping with a mock group presentation tracker expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',91,1);
INSERT INTO submissions VALUES(102,1,'Q2',87,2);
INSERT INTO submissions VALUES(103,1,'Q3',61,3);
INSERT INTO submissions VALUES(104,2,'Q1',57,1);
INSERT INTO submissions VALUES(105,3,'Q1',49,1);
INSERT INTO submissions VALUES(106,3,'Q2',79,2);
INSERT INTO submissions VALUES(107,3,'Q3',76,3);
INSERT INTO submissions VALUES(108,4,'Q1',72,1);
INSERT INTO submissions VALUES(109,5,'Q1',76,1);
INSERT INTO submissions VALUES(110,5,'Q2',65,2);
INSERT INTO submissions VALUES(111,5,'Q3',64,3);
INSERT INTO submissions VALUES(112,6,'Q1',44,1);

-- Checked query:
SELECT s.student_id,s.name,COUNT(*) AS joined_rows, COUNT(u.submission_id) AS real_submissions FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the use count of a child key for genuine matches exercise in a mock group presentation tracker, the SQLite 3 calculation for COUNT(*) versus COUNT(child primary key) yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘joined_rows’, ‘real_submissions’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 2, “name”: “Ben”, “joined_rows”: 1, “real_submissions”: 1}, {“student_id”: 3, “name”: “Chloe”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 4, “name”: “Dev”, “joined_rows”: 1, “real_submissions”: 1}, {“student_id”: 5, “name”: “Esha”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 6, “name”: “Farid”, “joined_rows”: 1, “real_submissions”: 1}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use count of a child key for genuine matches in a mock group presentation tracker, For unmatched parents, COUNT(*) is one while COUNT(u.submission_id) is zero. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Treat a version-dependent result as a prompt to consult the current official manual. For this a mock group presentation tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 3: COUNT(*) versus COUNT(child primary key) for a made-up quiz analysis

The current problem is use count of a child key for genuine matches. The activity log for a made-up quiz analysis contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',85,2);
INSERT INTO submissions VALUES(102,1,'Q2',40,3);
INSERT INTO submissions VALUES(103,1,'Q3',48,1);
INSERT INTO submissions VALUES(104,2,'Q1',93,2);
INSERT INTO submissions VALUES(105,3,'Q1',63,2);
INSERT INTO submissions VALUES(106,3,'Q2',80,3);
INSERT INTO submissions VALUES(107,3,'Q3',81,1);
INSERT INTO submissions VALUES(108,4,'Q1',80,2);
INSERT INTO submissions VALUES(109,5,'Q1',84,2);
INSERT INTO submissions VALUES(110,5,'Q2',81,3);
INSERT INTO submissions VALUES(111,5,'Q3',71,1);
INSERT INTO submissions VALUES(112,6,'Q1',75,2);

-- Checked query:
SELECT s.student_id,s.name,COUNT(*) AS joined_rows, COUNT(u.submission_id) AS real_submissions FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the use count of a child key for genuine matches exercise in a made-up quiz analysis, the SQLite 3 calculation for COUNT(*) versus COUNT(child primary key) yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘joined_rows’, ‘real_submissions’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 2, “name”: “Ben”, “joined_rows”: 1, “real_submissions”: 1}, {“student_id”: 3, “name”: “Chloe”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 4, “name”: “Dev”, “joined_rows”: 1, “real_submissions”: 1}, {“student_id”: 5, “name”: “Esha”, “joined_rows”: 3, “real_submissions”: 3}, {“student_id”: 6, “name”: “Farid”, “joined_rows”: 1, “real_submissions”: 1}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use count of a child key for genuine matches in a made-up quiz analysis, For unmatched parents, COUNT(*) is one while COUNT(u.submission_id) is zero. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. This is a proposed home exercise rather than a service or school claim. For this a made-up quiz analysis, require the learner to describe what one query row represents before publishing a finding.

Lab 4: COUNT(*) versus COUNT(child primary key) for a weather-observation practice sheet

The current problem is use count of a child key for genuine matches. A project partner has copied a query for a weather-observation practice sheet but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',44,2);
INSERT INTO submissions VALUES(102,1,'Q2',51,3);
INSERT INTO submissions VALUES(103,3,'Q1',96,2);
INSERT INTO submissions VALUES(104,3,'Q2',46,3);
INSERT INTO submissions VALUES(105,5,'Q1',48,2);
INSERT INTO submissions VALUES(106,5,'Q2',77,3);

-- Checked query:
SELECT s.student_id,s.name,COUNT(*) AS joined_rows, COUNT(u.submission_id) AS real_submissions FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the use count of a child key for genuine matches exercise in a weather-observation practice sheet, the SQLite 3 calculation for COUNT(*) versus COUNT(child primary key) yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘joined_rows’, ‘real_submissions’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “joined_rows”: 2, “real_submissions”: 2}, {“student_id”: 2, “name”: “Ben”, “joined_rows”: 1, “real_submissions”: 0}, {“student_id”: 3, “name”: “Chloe”, “joined_rows”: 2, “real_submissions”: 2}, {“student_id”: 4, “name”: “Dev”, “joined_rows”: 1, “real_submissions”: 0}, {“student_id”: 5, “name”: “Esha”, “joined_rows”: 2, “real_submissions”: 2}, {“student_id”: 6, “name”: “Farid”, “joined_rows”: 1, “real_submissions”: 0}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use count of a child key for genuine matches in a weather-observation practice sheet, For unmatched parents, COUNT(*) is one while COUNT(u.submission_id) is zero. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A useful parent prompt is to request a prediction, not a memorised command. For this a weather-observation practice sheet, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe use count of a child key for genuine matches in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Show both counts once to teach the distinction. When the result surprises the learner, keep the counterexample in the practice notes.

CHAPTER 8 OF 20 · Back to contents

8. Find parents without children using IS NULL

Start with the answer: A LEFT JOIN followed by WHERE right_table.nonnull_key IS NULL identifies unmatched left rows.

Test a declared non-nullable child primary key, not a nullable score that could be absent on an otherwise real submission.

Common tempting error: Treating a missing score as evidence that there was no submission at all.

Diagnostic for a parent or project partner: How do we know which field must exist for a real child record?

Lab 1: students with no child submission for an after-CCA practice planner

The current problem is find parents without children using is null. In an after-CCA practice planner, an imaginary database has 6 student rows and 12 submissions. The question is students with no child submission. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',55,2);
INSERT INTO submissions VALUES(102,2,'Q1',64,2);
INSERT INTO submissions VALUES(103,2,'Q2',92,3);
INSERT INTO submissions VALUES(104,2,'Q3',61,1);
INSERT INTO submissions VALUES(105,3,'Q1',86,2);
INSERT INTO submissions VALUES(106,4,'Q1',87,2);
INSERT INTO submissions VALUES(107,4,'Q2',82,3);
INSERT INTO submissions VALUES(108,4,'Q3',54,1);
INSERT INTO submissions VALUES(109,5,'Q1',43,2);
INSERT INTO submissions VALUES(110,6,'Q1',66,2);
INSERT INTO submissions VALUES(111,6,'Q2',89,3);
INSERT INTO submissions VALUES(112,6,'Q3',54,1);

-- Checked query:
SELECT s.student_id,s.name FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.submission_id IS NULL ORDER BY s.student_id;

Worked answer. For the find parents without children using is null exercise in an after-CCA practice planner, the SQLite 3 calculation for students with no child submission yields 0 output row(s) with columns [‘student_id’, ‘name’]. The first rows are []. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For find parents without children using is null in an after-CCA practice planner, NULL in a known non-nullable child key identifies a missing match, not merely an ungraded score. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. Independent checking beats a polished screen that nobody can explain. For this an after-CCA practice planner, require the learner to describe what one query row represents before publishing a finding.

Lab 2: students with no child submission for a school-holiday project notebook

The current problem is find parents without children using is null. A learner helping with a school-holiday project notebook expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',67,2);
INSERT INTO submissions VALUES(102,2,'Q2',38,3);
INSERT INTO submissions VALUES(103,4,'Q1',85,2);
INSERT INTO submissions VALUES(104,4,'Q2',68,3);
INSERT INTO submissions VALUES(105,6,'Q1',75,2);
INSERT INTO submissions VALUES(106,6,'Q2',49,3);

-- Checked query:
SELECT s.student_id,s.name FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.submission_id IS NULL ORDER BY s.student_id;

Worked answer. For the find parents without children using is null exercise in a school-holiday project notebook, the SQLite 3 calculation for students with no child submission yields 3 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 1, “name”: “Alya”}, {“student_id”: 3, “name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For find parents without children using is null in a school-holiday project notebook, NULL in a known non-nullable child key identifies a missing match, not merely an ungraded score. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Save the example so the learner can rerun it with a fresh number next week. For this a school-holiday project notebook, require the learner to describe what one query row represents before publishing a finding.

Lab 3: students with no child submission for a mock fundraising spreadsheet

The current problem is find parents without children using is null. The activity log for a mock fundraising spreadsheet contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',66,3);
INSERT INTO submissions VALUES(102,2,'Q2',65,1);
INSERT INTO submissions VALUES(103,4,'Q1',78,3);
INSERT INTO submissions VALUES(104,4,'Q2',80,1);
INSERT INTO submissions VALUES(105,6,'Q1',54,3);
INSERT INTO submissions VALUES(106,6,'Q2',39,1);

-- Checked query:
SELECT s.student_id,s.name FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.submission_id IS NULL ORDER BY s.student_id;

Worked answer. For the find parents without children using is null exercise in a mock fundraising spreadsheet, the SQLite 3 calculation for students with no child submission yields 3 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 1, “name”: “Alya”}, {“student_id”: 3, “name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For find parents without children using is null in a mock fundraising spreadsheet, NULL in a known non-nullable child key identifies a missing match, not merely an ungraded score. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a mock fundraising spreadsheet, require the learner to describe what one query row represents before publishing a finding.

Lab 4: students with no child submission for a toy digital noticeboard

The current problem is find parents without children using is null. A project partner has copied a query for a toy digital noticeboard but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',86,3);
INSERT INTO submissions VALUES(102,1,'Q2',35,1);
INSERT INTO submissions VALUES(103,1,'Q3',97,2);
INSERT INTO submissions VALUES(104,2,'Q1',41,3);
INSERT INTO submissions VALUES(105,3,'Q1',52,3);
INSERT INTO submissions VALUES(106,3,'Q2',43,1);
INSERT INTO submissions VALUES(107,3,'Q3',47,2);
INSERT INTO submissions VALUES(108,4,'Q1',87,3);
INSERT INTO submissions VALUES(109,5,'Q1',86,3);
INSERT INTO submissions VALUES(110,5,'Q2',64,1);
INSERT INTO submissions VALUES(111,5,'Q3',68,2);
INSERT INTO submissions VALUES(112,6,'Q1',50,3);

-- Checked query:
SELECT s.student_id,s.name FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id WHERE u.submission_id IS NULL ORDER BY s.student_id;

Worked answer. For the find parents without children using is null exercise in a toy digital noticeboard, the SQLite 3 calculation for students with no child submission yields 0 output row(s) with columns [‘student_id’, ‘name’]. The first rows are []. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For find parents without children using is null in a toy digital noticeboard, NULL in a known non-nullable child key identifies a missing match, not merely an ungraded score. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a toy digital noticeboard, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe find parents without children using is null in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Verify the right key constraint and compare with NOT EXISTS. Independent checking beats a polished screen that nobody can explain.

CHAPTER 9 OF 20 · Back to contents

9. Use EXISTS for a presence question without multiplying rows

Start with the answer: EXISTS answers whether at least one matching child exists, returning each qualifying parent only once.

A naive INNER JOIN lists one row for each child. If the question is only “Which students submitted anything?”, the semi-join style of EXISTS avoids accidental duplicate parent names.

Common tempting error: Applying DISTINCT after the fact instead of first choosing the right question.

Diagnostic for a parent or project partner: Are we listing matches or merely checking existence?

Lab 1: students with at least one submission for a recycled-materials tally

The current problem is use exists for a presence question without multiplying rows. In a recycled-materials tally, an imaginary database has 6 student rows and 6 submissions. The question is students with at least one submission. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',76,3);
INSERT INTO submissions VALUES(102,1,'Q2',42,1);
INSERT INTO submissions VALUES(103,3,'Q1',39,3);
INSERT INTO submissions VALUES(104,3,'Q2',55,1);
INSERT INTO submissions VALUES(105,5,'Q1',92,3);
INSERT INTO submissions VALUES(106,5,'Q2',58,1);

-- Checked query:
SELECT s.student_id,s.name FROM students s WHERE EXISTS (SELECT 1 FROM submissions u WHERE u.student_id=s.student_id) ORDER BY s.student_id;

Worked answer. For the use exists for a presence question without multiplying rows exercise in a recycled-materials tally, the SQLite 3 calculation for students with at least one submission yields 3 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 1, “name”: “Alya”}, {“student_id”: 3, “name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use exists for a presence question without multiplying rows in a recycled-materials tally, EXISTS returns each parent once regardless of how many child records match. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. The next step should be one small controlled variation, not ten simultaneous edits. For this a recycled-materials tally, require the learner to describe what one query row represents before publishing a finding.

Lab 2: students with at least one submission for a home garden observation chart

The current problem is use exists for a presence question without multiplying rows. A learner helping with a home garden observation chart expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',39,3);
INSERT INTO submissions VALUES(102,2,'Q1',39,3);
INSERT INTO submissions VALUES(103,2,'Q2',75,1);
INSERT INTO submissions VALUES(104,2,'Q3',43,2);
INSERT INTO submissions VALUES(105,3,'Q1',67,3);
INSERT INTO submissions VALUES(106,4,'Q1',36,3);
INSERT INTO submissions VALUES(107,4,'Q2',62,1);
INSERT INTO submissions VALUES(108,4,'Q3',81,2);
INSERT INTO submissions VALUES(109,5,'Q1',46,3);
INSERT INTO submissions VALUES(110,6,'Q1',55,3);
INSERT INTO submissions VALUES(111,6,'Q2',96,1);
INSERT INTO submissions VALUES(112,6,'Q3',68,2);

-- Checked query:
SELECT s.student_id,s.name FROM students s WHERE EXISTS (SELECT 1 FROM submissions u WHERE u.student_id=s.student_id) ORDER BY s.student_id;

Worked answer. For the use exists for a presence question without multiplying rows exercise in a home garden observation chart, the SQLite 3 calculation for students with at least one submission yields 6 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 1, “name”: “Alya”}, {“student_id”: 2, “name”: “Ben”}, {“student_id”: 3, “name”: “Chloe”}, {“student_id”: 4, “name”: “Dev”}, {“student_id”: 5, “name”: “Esha”}, {“student_id”: 6, “name”: “Farid”}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use exists for a presence question without multiplying rows in a home garden observation chart, EXISTS returns each parent once regardless of how many child records match. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. A clear explanation is more valuable than guessing an answer from the display. For this a home garden observation chart, require the learner to describe what one query row represents before publishing a finding.

Lab 3: students with at least one submission for a public-transport timing experiment

The current problem is use exists for a presence question without multiplying rows. The activity log for a public-transport timing experiment contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',83,1);
INSERT INTO submissions VALUES(102,2,'Q1',86,1);
INSERT INTO submissions VALUES(103,2,'Q2',95,2);
INSERT INTO submissions VALUES(104,2,'Q3',88,3);
INSERT INTO submissions VALUES(105,3,'Q1',71,1);
INSERT INTO submissions VALUES(106,4,'Q1',52,1);
INSERT INTO submissions VALUES(107,4,'Q2',96,2);
INSERT INTO submissions VALUES(108,4,'Q3',82,3);
INSERT INTO submissions VALUES(109,5,'Q1',79,1);
INSERT INTO submissions VALUES(110,6,'Q1',91,1);
INSERT INTO submissions VALUES(111,6,'Q2',71,2);
INSERT INTO submissions VALUES(112,6,'Q3',44,3);

-- Checked query:
SELECT s.student_id,s.name FROM students s WHERE EXISTS (SELECT 1 FROM submissions u WHERE u.student_id=s.student_id) ORDER BY s.student_id;

Worked answer. For the use exists for a presence question without multiplying rows exercise in a public-transport timing experiment, the SQLite 3 calculation for students with at least one submission yields 6 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 1, “name”: “Alya”}, {“student_id”: 2, “name”: “Ben”}, {“student_id”: 3, “name”: “Chloe”}, {“student_id”: 4, “name”: “Dev”}, {“student_id”: 5, “name”: “Esha”}, {“student_id”: 6, “name”: “Farid”}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use exists for a presence question without multiplying rows in a public-transport timing experiment, EXISTS returns each parent once regardless of how many child records match. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. No real pupil information is needed; the test data are deliberately imaginary. For this a public-transport timing experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 4: students with at least one submission for a fictional club membership register

The current problem is use exists for a presence question without multiplying rows. A project partner has copied a query for a fictional club membership register but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',79,1);
INSERT INTO submissions VALUES(102,2,'Q2',44,2);
INSERT INTO submissions VALUES(103,4,'Q1',83,1);
INSERT INTO submissions VALUES(104,4,'Q2',45,2);
INSERT INTO submissions VALUES(105,6,'Q1',41,1);
INSERT INTO submissions VALUES(106,6,'Q2',70,2);

-- Checked query:
SELECT s.student_id,s.name FROM students s WHERE EXISTS (SELECT 1 FROM submissions u WHERE u.student_id=s.student_id) ORDER BY s.student_id;

Worked answer. For the use exists for a presence question without multiplying rows exercise in a fictional club membership register, the SQLite 3 calculation for students with at least one submission yields 3 output row(s) with columns [‘student_id’, ‘name’]. The first rows are [{“student_id”: 2, “name”: “Ben”}, {“student_id”: 4, “name”: “Dev”}, {“student_id”: 6, “name”: “Farid”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use exists for a presence question without multiplying rows in a fictional club membership register, EXISTS returns each parent once regardless of how many child records match. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional club membership register, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe use exists for a presence question without multiplying rows in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Choose EXISTS for presence when no child detail is required. The next step should be one small controlled variation, not ten simultaneous edits.

CHAPTER 10 OF 20 · Back to contents

10. Audit orphaned child records carefully

Start with the answer: A child row referring to a missing parent is an integrity issue and may need a separate audit.

Depending on constraints, a toy database can contain an orphan student_id. A LEFT JOIN from the child table shows it; starting from students cannot list nonexistent parents.

Common tempting error: Using a student-left join and concluding no orphan child rows exist.

Diagnostic for a parent or project partner: Which table must be on the left to preserve a record whose parent is missing?

Lab 1: orphan submissions with no parent for a mock group presentation tracker

The current problem is audit orphaned child records carefully. In a mock group presentation tracker, an imaginary database has 6 student rows and 13 submissions. The question is orphan submissions with no parent. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',93,1);
INSERT INTO submissions VALUES(102,1,'Q2',52,2);
INSERT INTO submissions VALUES(103,1,'Q3',72,3);
INSERT INTO submissions VALUES(104,2,'Q1',63,1);
INSERT INTO submissions VALUES(105,3,'Q1',71,1);
INSERT INTO submissions VALUES(106,3,'Q2',63,2);
INSERT INTO submissions VALUES(107,3,'Q3',54,3);
INSERT INTO submissions VALUES(108,4,'Q1',44,1);
INSERT INTO submissions VALUES(109,5,'Q1',70,1);
INSERT INTO submissions VALUES(110,5,'Q2',95,2);
INSERT INTO submissions VALUES(111,5,'Q3',40,3);
INSERT INTO submissions VALUES(112,6,'Q1',75,1);
INSERT INTO submissions VALUES(113,99,'ORPHAN',70,1);

-- Checked query:
SELECT u.submission_id,u.student_id,s.name FROM submissions u LEFT JOIN students s ON s.student_id=u.student_id WHERE s.student_id IS NULL ORDER BY u.submission_id;

Worked answer. For the audit orphaned child records carefully exercise in a mock group presentation tracker, the SQLite 3 calculation for orphan submissions with no parent yields 1 output row(s) with columns [‘submission_id’, ‘student_id’, ‘name’]. The first rows are [{“submission_id”: 113, “student_id”: 99, “name”: null}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For audit orphaned child records carefully in a mock group presentation tracker, Preserving submissions reveals records whose student_id does not exist in the parent table. This experiment starts with 6 left students and 13 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. Treat a version-dependent result as a prompt to consult the current official manual. For this a mock group presentation tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 2: orphan submissions with no parent for a volunteer-hours planning exercise

The current problem is audit orphaned child records carefully. A learner helping with a volunteer-hours planning exercise expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',91,1);
INSERT INTO submissions VALUES(102,1,'Q2',63,2);
INSERT INTO submissions VALUES(103,3,'Q1',40,1);
INSERT INTO submissions VALUES(104,3,'Q2',75,2);
INSERT INTO submissions VALUES(105,5,'Q1',39,1);
INSERT INTO submissions VALUES(106,5,'Q2',95,2);
INSERT INTO submissions VALUES(107,99,'ORPHAN',70,1);

-- Checked query:
SELECT u.submission_id,u.student_id,s.name FROM submissions u LEFT JOIN students s ON s.student_id=u.student_id WHERE s.student_id IS NULL ORDER BY u.submission_id;

Worked answer. For the audit orphaned child records carefully exercise in a volunteer-hours planning exercise, the SQLite 3 calculation for orphan submissions with no parent yields 1 output row(s) with columns [‘submission_id’, ‘student_id’, ‘name’]. The first rows are [{“submission_id”: 107, “student_id”: 99, “name”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For audit orphaned child records carefully in a volunteer-hours planning exercise, Preserving submissions reveals records whose student_id does not exist in the parent table. This experiment starts with 6 left students and 7 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Try this on a small, invented table before using any real schoolwork. For this a volunteer-hours planning exercise, require the learner to describe what one query row represents before publishing a finding.

Lab 3: orphan submissions with no parent for a weather-observation practice sheet

The current problem is audit orphaned child records carefully. The activity log for a weather-observation practice sheet contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',51,2);
INSERT INTO submissions VALUES(102,1,'Q2',41,3);
INSERT INTO submissions VALUES(103,3,'Q1',92,2);
INSERT INTO submissions VALUES(104,3,'Q2',67,3);
INSERT INTO submissions VALUES(105,5,'Q1',82,2);
INSERT INTO submissions VALUES(106,5,'Q2',68,3);
INSERT INTO submissions VALUES(107,99,'ORPHAN',70,1);

-- Checked query:
SELECT u.submission_id,u.student_id,s.name FROM submissions u LEFT JOIN students s ON s.student_id=u.student_id WHERE s.student_id IS NULL ORDER BY u.submission_id;

Worked answer. For the audit orphaned child records carefully exercise in a weather-observation practice sheet, the SQLite 3 calculation for orphan submissions with no parent yields 1 output row(s) with columns [‘submission_id’, ‘student_id’, ‘name’]. The first rows are [{“submission_id”: 107, “student_id”: 99, “name”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For audit orphaned child records carefully in a weather-observation practice sheet, Preserving submissions reveals records whose student_id does not exist in the parent table. This experiment starts with 6 left students and 7 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. A useful parent prompt is to request a prediction, not a memorised command. For this a weather-observation practice sheet, require the learner to describe what one query row represents before publishing a finding.

Lab 4: orphan submissions with no parent for a shared family study calendar

The current problem is audit orphaned child records carefully. A project partner has copied a query for a shared family study calendar but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',63,2);
INSERT INTO submissions VALUES(102,2,'Q1',53,2);
INSERT INTO submissions VALUES(103,2,'Q2',40,3);
INSERT INTO submissions VALUES(104,2,'Q3',76,1);
INSERT INTO submissions VALUES(105,3,'Q1',35,2);
INSERT INTO submissions VALUES(106,4,'Q1',39,2);
INSERT INTO submissions VALUES(107,4,'Q2',90,3);
INSERT INTO submissions VALUES(108,4,'Q3',59,1);
INSERT INTO submissions VALUES(109,5,'Q1',80,2);
INSERT INTO submissions VALUES(110,6,'Q1',76,2);
INSERT INTO submissions VALUES(111,6,'Q2',42,3);
INSERT INTO submissions VALUES(112,6,'Q3',80,1);
INSERT INTO submissions VALUES(113,99,'ORPHAN',70,1);

-- Checked query:
SELECT u.submission_id,u.student_id,s.name FROM submissions u LEFT JOIN students s ON s.student_id=u.student_id WHERE s.student_id IS NULL ORDER BY u.submission_id;

Worked answer. For the audit orphaned child records carefully exercise in a shared family study calendar, the SQLite 3 calculation for orphan submissions with no parent yields 1 output row(s) with columns [‘submission_id’, ‘student_id’, ‘name’]. The first rows are [{“submission_id”: 113, “student_id”: 99, “name”: null}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For audit orphaned child records carefully in a shared family study calendar, Preserving submissions reveals records whose student_id does not exist in the parent table. This experiment starts with 6 left students and 13 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Independent checking beats a polished screen that nobody can explain. For this a shared family study calendar, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe audit orphaned child records carefully in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Keep integrity diagnostics separate from a report about students. Treat a version-dependent result as a prompt to consult the current official manual.

CHAPTER 11 OF 20 · Back to contents

11. Summarise matches per parent at the right grain

Start with the answer: GROUP BY the parent identity to get one reported count or sum per parent after joining.

Choosing only name as a grouping key can combine two distinct people if names repeat. COUNT(u.submission_id) and SUM(u.score) answer different questions.

Common tempting error: Using a non-unique display name as the only grouping key.

Diagnostic for a parent or project partner: What makes one learner identity stable in the practice dataset?

Lab 1: one summary row per student for a school-holiday project notebook

The current problem is summarise matches per parent at the right grain. In a school-holiday project notebook, an imaginary database has 6 student rows and 6 submissions. The question is one summary row per student. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',58,2);
INSERT INTO submissions VALUES(102,2,'Q2',75,3);
INSERT INTO submissions VALUES(103,4,'Q1',53,2);
INSERT INTO submissions VALUES(104,4,'Q2',65,3);
INSERT INTO submissions VALUES(105,6,'Q1',42,2);
INSERT INTO submissions VALUES(106,6,'Q2',55,3);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS real_submissions,SUM(u.score) AS score_total FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the summarise matches per parent at the right grain exercise in a school-holiday project notebook, the SQLite 3 calculation for one summary row per student yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘real_submissions’, ‘score_total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “real_submissions”: 0, “score_total”: null}, {“student_id”: 2, “name”: “Ben”, “real_submissions”: 2, “score_total”: 133}, {“student_id”: 3, “name”: “Chloe”, “real_submissions”: 0, “score_total”: null}, {“student_id”: 4, “name”: “Dev”, “real_submissions”: 2, “score_total”: 118}, {“student_id”: 5, “name”: “Esha”, “real_submissions”: 0, “score_total”: null}, {“student_id”: 6, “name”: “Farid”, “real_submissions”: 2, “score_total”: 97}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For summarise matches per parent at the right grain in a school-holiday project notebook, Grouping on a stable primary key yields one parent summary without collapsing distinct same-name students. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. Save the example so the learner can rerun it with a fresh number next week. For this a school-holiday project notebook, require the learner to describe what one query row represents before publishing a finding.

Lab 2: one summary row per student for a made-up quiz analysis

The current problem is summarise matches per parent at the right grain. A learner helping with a made-up quiz analysis expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',80,2);
INSERT INTO submissions VALUES(102,1,'Q2',59,3);
INSERT INTO submissions VALUES(103,1,'Q3',96,1);
INSERT INTO submissions VALUES(104,2,'Q1',37,2);
INSERT INTO submissions VALUES(105,3,'Q1',77,2);
INSERT INTO submissions VALUES(106,3,'Q2',73,3);
INSERT INTO submissions VALUES(107,3,'Q3',69,1);
INSERT INTO submissions VALUES(108,4,'Q1',92,2);
INSERT INTO submissions VALUES(109,5,'Q1',54,2);
INSERT INTO submissions VALUES(110,5,'Q2',82,3);
INSERT INTO submissions VALUES(111,5,'Q3',46,1);
INSERT INTO submissions VALUES(112,6,'Q1',66,2);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS real_submissions,SUM(u.score) AS score_total FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the summarise matches per parent at the right grain exercise in a made-up quiz analysis, the SQLite 3 calculation for one summary row per student yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘real_submissions’, ‘score_total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “real_submissions”: 3, “score_total”: 235}, {“student_id”: 2, “name”: “Ben”, “real_submissions”: 1, “score_total”: 37}, {“student_id”: 3, “name”: “Chloe”, “real_submissions”: 3, “score_total”: 219}, {“student_id”: 4, “name”: “Dev”, “real_submissions”: 1, “score_total”: 92}, {“student_id”: 5, “name”: “Esha”, “real_submissions”: 3, “score_total”: 182}, {“student_id”: 6, “name”: “Farid”, “real_submissions”: 1, “score_total”: 66}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For summarise matches per parent at the right grain in a made-up quiz analysis, Grouping on a stable primary key yields one parent summary without collapsing distinct same-name students. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. This is a proposed home exercise rather than a service or school claim. For this a made-up quiz analysis, require the learner to describe what one query row represents before publishing a finding.

Lab 3: one summary row per student for a toy digital noticeboard

The current problem is summarise matches per parent at the right grain. The activity log for a toy digital noticeboard contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',72,3);
INSERT INTO submissions VALUES(102,1,'Q2',90,1);
INSERT INTO submissions VALUES(103,1,'Q3',57,2);
INSERT INTO submissions VALUES(104,2,'Q1',42,3);
INSERT INTO submissions VALUES(105,3,'Q1',61,3);
INSERT INTO submissions VALUES(106,3,'Q2',59,1);
INSERT INTO submissions VALUES(107,3,'Q3',74,2);
INSERT INTO submissions VALUES(108,4,'Q1',89,3);
INSERT INTO submissions VALUES(109,5,'Q1',89,3);
INSERT INTO submissions VALUES(110,5,'Q2',42,1);
INSERT INTO submissions VALUES(111,5,'Q3',40,2);
INSERT INTO submissions VALUES(112,6,'Q1',42,3);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS real_submissions,SUM(u.score) AS score_total FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the summarise matches per parent at the right grain exercise in a toy digital noticeboard, the SQLite 3 calculation for one summary row per student yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘real_submissions’, ‘score_total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “real_submissions”: 3, “score_total”: 219}, {“student_id”: 2, “name”: “Ben”, “real_submissions”: 1, “score_total”: 42}, {“student_id”: 3, “name”: “Chloe”, “real_submissions”: 3, “score_total”: 194}, {“student_id”: 4, “name”: “Dev”, “real_submissions”: 1, “score_total”: 89}, {“student_id”: 5, “name”: “Esha”, “real_submissions”: 3, “score_total”: 171}, {“student_id”: 6, “name”: “Farid”, “real_submissions”: 1, “score_total”: 42}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For summarise matches per parent at the right grain in a toy digital noticeboard, Grouping on a stable primary key yields one parent summary without collapsing distinct same-name students. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a toy digital noticeboard, require the learner to describe what one query row represents before publishing a finding.

Lab 4: one summary row per student for a family revision tracker

The current problem is summarise matches per parent at the right grain. A project partner has copied a query for a family revision tracker but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',48,3);
INSERT INTO submissions VALUES(102,1,'Q2',96,1);
INSERT INTO submissions VALUES(103,3,'Q1',73,3);
INSERT INTO submissions VALUES(104,3,'Q2',78,1);
INSERT INTO submissions VALUES(105,5,'Q1',88,3);
INSERT INTO submissions VALUES(106,5,'Q2',76,1);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS real_submissions,SUM(u.score) AS score_total FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the summarise matches per parent at the right grain exercise in a family revision tracker, the SQLite 3 calculation for one summary row per student yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘real_submissions’, ‘score_total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “real_submissions”: 2, “score_total”: 144}, {“student_id”: 2, “name”: “Ben”, “real_submissions”: 0, “score_total”: null}, {“student_id”: 3, “name”: “Chloe”, “real_submissions”: 2, “score_total”: 151}, {“student_id”: 4, “name”: “Dev”, “real_submissions”: 0, “score_total”: null}, {“student_id”: 5, “name”: “Esha”, “real_submissions”: 2, “score_total”: 164}, {“student_id”: 6, “name”: “Farid”, “real_submissions”: 0, “score_total”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For summarise matches per parent at the right grain in a family revision tracker, Grouping on a stable primary key yields one parent summary without collapsing distinct same-name students. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. The next step should be one small controlled variation, not ten simultaneous edits. For this a family revision tracker, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe summarise matches per parent at the right grain in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Include the declared parent key in grouped reports. Save the example so the learner can rerun it with a fresh number next week.

CHAPTER 12 OF 20 · Back to contents

12. Aggregate children before a join when one row per parent is needed

Start with the answer: A child subquery grouped by student_id can be LEFT JOINed to the parent without multiplying that parent.

This is useful when a report needs total submissions and total points per learner rather than every quiz row. Missing child aggregates remain NULL until handled deliberately.

Common tempting error: Joining every detail row and then selecting an arbitrary one from each parent.

Diagnostic for a parent or project partner: Can you confirm the child table has one summary row per parent key?

Lab 1: preaggregated children joined to their parents for a home garden observation chart

The current problem is aggregate children before a join when one row per parent is needed. In a home garden observation chart, an imaginary database has 6 student rows and 12 submissions. The question is preaggregated children joined to their parents. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',51,3);
INSERT INTO submissions VALUES(102,2,'Q1',95,3);
INSERT INTO submissions VALUES(103,2,'Q2',94,1);
INSERT INTO submissions VALUES(104,2,'Q3',86,2);
INSERT INTO submissions VALUES(105,3,'Q1',78,3);
INSERT INTO submissions VALUES(106,4,'Q1',92,3);
INSERT INTO submissions VALUES(107,4,'Q2',92,1);
INSERT INTO submissions VALUES(108,4,'Q3',39,2);
INSERT INTO submissions VALUES(109,5,'Q1',37,3);
INSERT INTO submissions VALUES(110,6,'Q1',72,3);
INSERT INTO submissions VALUES(111,6,'Q2',73,1);
INSERT INTO submissions VALUES(112,6,'Q3',55,2);

-- Checked query:
SELECT s.student_id,s.name,x.n,x.total FROM students s LEFT JOIN (SELECT student_id,COUNT(*) AS n,SUM(score) AS total FROM submissions GROUP BY student_id) x ON x.student_id=s.student_id ORDER BY s.student_id;

Worked answer. For the aggregate children before a join when one row per parent is needed exercise in a home garden observation chart, the SQLite 3 calculation for preaggregated children joined to their parents yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘n’, ‘total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “n”: 1, “total”: 51}, {“student_id”: 2, “name”: “Ben”, “n”: 3, “total”: 275}, {“student_id”: 3, “name”: “Chloe”, “n”: 1, “total”: 78}, {“student_id”: 4, “name”: “Dev”, “n”: 3, “total”: 223}, {“student_id”: 5, “name”: “Esha”, “n”: 1, “total”: 37}, {“student_id”: 6, “name”: “Farid”, “n”: 3, “total”: 200}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For aggregate children before a join when one row per parent is needed in a home garden observation chart, One child summary row per student avoids multiplying parent rows. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. A clear explanation is more valuable than guessing an answer from the display. For this a home garden observation chart, require the learner to describe what one query row represents before publishing a finding.

Lab 2: preaggregated children joined to their parents for a mock fundraising spreadsheet

The current problem is aggregate children before a join when one row per parent is needed. A learner helping with a mock fundraising spreadsheet expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',62,3);
INSERT INTO submissions VALUES(102,2,'Q2',96,1);
INSERT INTO submissions VALUES(103,4,'Q1',39,3);
INSERT INTO submissions VALUES(104,4,'Q2',36,1);
INSERT INTO submissions VALUES(105,6,'Q1',81,3);
INSERT INTO submissions VALUES(106,6,'Q2',94,1);

-- Checked query:
SELECT s.student_id,s.name,x.n,x.total FROM students s LEFT JOIN (SELECT student_id,COUNT(*) AS n,SUM(score) AS total FROM submissions GROUP BY student_id) x ON x.student_id=s.student_id ORDER BY s.student_id;

Worked answer. For the aggregate children before a join when one row per parent is needed exercise in a mock fundraising spreadsheet, the SQLite 3 calculation for preaggregated children joined to their parents yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘n’, ‘total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “n”: null, “total”: null}, {“student_id”: 2, “name”: “Ben”, “n”: 2, “total”: 158}, {“student_id”: 3, “name”: “Chloe”, “n”: null, “total”: null}, {“student_id”: 4, “name”: “Dev”, “n”: 2, “total”: 75}, {“student_id”: 5, “name”: “Esha”, “n”: null, “total”: null}, {“student_id”: 6, “name”: “Farid”, “n”: 2, “total”: 175}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For aggregate children before a join when one row per parent is needed in a mock fundraising spreadsheet, One child summary row per student avoids multiplying parent rows. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a mock fundraising spreadsheet, require the learner to describe what one query row represents before publishing a finding.

Lab 3: preaggregated children joined to their parents for a fictional club membership register

The current problem is aggregate children before a join when one row per parent is needed. The activity log for a fictional club membership register contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',58,1);
INSERT INTO submissions VALUES(102,2,'Q2',94,2);
INSERT INTO submissions VALUES(103,4,'Q1',70,1);
INSERT INTO submissions VALUES(104,4,'Q2',91,2);
INSERT INTO submissions VALUES(105,6,'Q1',77,1);
INSERT INTO submissions VALUES(106,6,'Q2',76,2);

-- Checked query:
SELECT s.student_id,s.name,x.n,x.total FROM students s LEFT JOIN (SELECT student_id,COUNT(*) AS n,SUM(score) AS total FROM submissions GROUP BY student_id) x ON x.student_id=s.student_id ORDER BY s.student_id;

Worked answer. For the aggregate children before a join when one row per parent is needed exercise in a fictional club membership register, the SQLite 3 calculation for preaggregated children joined to their parents yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘n’, ‘total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “n”: null, “total”: null}, {“student_id”: 2, “name”: “Ben”, “n”: 2, “total”: 152}, {“student_id”: 3, “name”: “Chloe”, “n”: null, “total”: null}, {“student_id”: 4, “name”: “Dev”, “n”: 2, “total”: 161}, {“student_id”: 5, “name”: “Esha”, “n”: null, “total”: null}, {“student_id”: 6, “name”: “Farid”, “n”: 2, “total”: 153}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For aggregate children before a join when one row per parent is needed in a fictional club membership register, One child summary row per student avoids multiplying parent rows. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional club membership register, require the learner to describe what one query row represents before publishing a finding.

Lab 4: preaggregated children joined to their parents for a fictional class activity log

The current problem is aggregate children before a join when one row per parent is needed. A project partner has copied a query for a fictional class activity log but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',50,1);
INSERT INTO submissions VALUES(102,1,'Q2',45,2);
INSERT INTO submissions VALUES(103,1,'Q3',80,3);
INSERT INTO submissions VALUES(104,2,'Q1',35,1);
INSERT INTO submissions VALUES(105,3,'Q1',88,1);
INSERT INTO submissions VALUES(106,3,'Q2',73,2);
INSERT INTO submissions VALUES(107,3,'Q3',56,3);
INSERT INTO submissions VALUES(108,4,'Q1',38,1);
INSERT INTO submissions VALUES(109,5,'Q1',36,1);
INSERT INTO submissions VALUES(110,5,'Q2',42,2);
INSERT INTO submissions VALUES(111,5,'Q3',63,3);
INSERT INTO submissions VALUES(112,6,'Q1',54,1);

-- Checked query:
SELECT s.student_id,s.name,x.n,x.total FROM students s LEFT JOIN (SELECT student_id,COUNT(*) AS n,SUM(score) AS total FROM submissions GROUP BY student_id) x ON x.student_id=s.student_id ORDER BY s.student_id;

Worked answer. For the aggregate children before a join when one row per parent is needed exercise in a fictional class activity log, the SQLite 3 calculation for preaggregated children joined to their parents yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘n’, ‘total’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “n”: 3, “total”: 175}, {“student_id”: 2, “name”: “Ben”, “n”: 1, “total”: 35}, {“student_id”: 3, “name”: “Chloe”, “n”: 3, “total”: 217}, {“student_id”: 4, “name”: “Dev”, “n”: 1, “total”: 38}, {“student_id”: 5, “name”: “Esha”, “n”: 3, “total”: 141}, {“student_id”: 6, “name”: “Farid”, “n”: 1, “total”: 54}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For aggregate children before a join when one row per parent is needed in a fictional class activity log, One child summary row per student avoids multiplying parent rows. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Treat a version-dependent result as a prompt to consult the current official manual. For this a fictional class activity log, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe aggregate children before a join when one row per parent is needed in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Check the aggregate first, then the join cardinality. A clear explanation is more valuable than guessing an answer from the display.

CHAPTER 13 OF 20 · Back to contents

13. Contrast INNER and LEFT when score conditions are added

Start with the answer: INNER JOIN with a qualifying-score condition returns only matching qualifying pairs; LEFT JOIN with the condition in ON still preserves every parent.

A mark threshold defines eligible child matches, not the parent’s worth or level. The SQL position of the predicate determines whether unqualified parents remain visible.

Common tempting error: Treating an absence in the filtered report as absence of every recorded submission.

Diagnostic for a parent or project partner: What did the query exclude: pupils, low marks or missing records?

Lab 1: INNER matches passing the score boundary for a volunteer-hours planning exercise

The current problem is contrast inner and left when score conditions are added. In a volunteer-hours planning exercise, an imaginary database has 6 student rows and 6 submissions. The question is INNER matches passing the score boundary. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',57,1);
INSERT INTO submissions VALUES(102,1,'Q2',46,2);
INSERT INTO submissions VALUES(103,3,'Q1',76,1);
INSERT INTO submissions VALUES(104,3,'Q2',83,2);
INSERT INTO submissions VALUES(105,5,'Q1',43,1);
INSERT INTO submissions VALUES(106,5,'Q2',80,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id AND u.score>=72 ORDER BY s.student_id,u.submission_id;

Worked answer. For the contrast inner and left when score conditions are added exercise in a volunteer-hours planning exercise, the SQLite 3 calculation for INNER matches passing the score boundary yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “score”: 76}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “score”: 83}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “score”: 80}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For contrast inner and left when score conditions are added in a volunteer-hours planning exercise, Both matching and qualifying-score conditions must be true; students with no qualifying child are absent. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. Try this on a small, invented table before using any real schoolwork. For this a volunteer-hours planning exercise, require the learner to describe what one query row represents before publishing a finding.

Lab 2: INNER matches passing the score boundary for a public-transport timing experiment

The current problem is contrast inner and left when score conditions are added. A learner helping with a public-transport timing experiment expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',84,1);
INSERT INTO submissions VALUES(102,2,'Q1',53,1);
INSERT INTO submissions VALUES(103,2,'Q2',68,2);
INSERT INTO submissions VALUES(104,2,'Q3',62,3);
INSERT INTO submissions VALUES(105,3,'Q1',35,1);
INSERT INTO submissions VALUES(106,4,'Q1',71,1);
INSERT INTO submissions VALUES(107,4,'Q2',79,2);
INSERT INTO submissions VALUES(108,4,'Q3',95,3);
INSERT INTO submissions VALUES(109,5,'Q1',40,1);
INSERT INTO submissions VALUES(110,6,'Q1',57,1);
INSERT INTO submissions VALUES(111,6,'Q2',44,2);
INSERT INTO submissions VALUES(112,6,'Q3',54,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id AND u.score>=75 ORDER BY s.student_id,u.submission_id;

Worked answer. For the contrast inner and left when score conditions are added exercise in a public-transport timing experiment, the SQLite 3 calculation for INNER matches passing the score boundary yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 84}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 107, “score”: 79}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 95}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For contrast inner and left when score conditions are added in a public-transport timing experiment, Both matching and qualifying-score conditions must be true; students with no qualifying child are absent. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. No real pupil information is needed; the test data are deliberately imaginary. For this a public-transport timing experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 3: INNER matches passing the score boundary for a shared family study calendar

The current problem is contrast inner and left when score conditions are added. The activity log for a shared family study calendar contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',55,2);
INSERT INTO submissions VALUES(102,2,'Q1',71,2);
INSERT INTO submissions VALUES(103,2,'Q2',46,3);
INSERT INTO submissions VALUES(104,2,'Q3',94,1);
INSERT INTO submissions VALUES(105,3,'Q1',79,2);
INSERT INTO submissions VALUES(106,4,'Q1',75,2);
INSERT INTO submissions VALUES(107,4,'Q2',65,3);
INSERT INTO submissions VALUES(108,4,'Q3',69,1);
INSERT INTO submissions VALUES(109,5,'Q1',41,2);
INSERT INTO submissions VALUES(110,6,'Q1',91,2);
INSERT INTO submissions VALUES(111,6,'Q2',73,3);
INSERT INTO submissions VALUES(112,6,'Q3',35,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id AND u.score>=79 ORDER BY s.student_id,u.submission_id;

Worked answer. For the contrast inner and left when score conditions are added exercise in a shared family study calendar, the SQLite 3 calculation for INNER matches passing the score boundary yields 3 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “score”: 94}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “score”: 79}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 110, “score”: 91}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For contrast inner and left when score conditions are added in a shared family study calendar, Both matching and qualifying-score conditions must be true; students with no qualifying child are absent. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Independent checking beats a polished screen that nobody can explain. For this a shared family study calendar, require the learner to describe what one query row represents before publishing a finding.

Lab 4: INNER matches passing the score boundary for a weekend reading journal

The current problem is contrast inner and left when score conditions are added. A project partner has copied a query for a weekend reading journal but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',64,2);
INSERT INTO submissions VALUES(102,2,'Q2',79,3);
INSERT INTO submissions VALUES(103,4,'Q1',85,2);
INSERT INTO submissions VALUES(104,4,'Q2',88,3);
INSERT INTO submissions VALUES(105,6,'Q1',59,2);
INSERT INTO submissions VALUES(106,6,'Q2',63,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s INNER JOIN submissions u ON u.student_id=s.student_id AND u.score>=82 ORDER BY s.student_id,u.submission_id;

Worked answer. For the contrast inner and left when score conditions are added exercise in a weekend reading journal, the SQLite 3 calculation for INNER matches passing the score boundary yields 2 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “score”: 85}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “score”: 88}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For contrast inner and left when score conditions are added in a weekend reading journal, Both matching and qualifying-score conditions must be true; students with no qualifying child are absent. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Save the example so the learner can rerun it with a fresh number next week. For this a weekend reading journal, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe contrast inner and left when score conditions are added in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Describe the selection mechanism without inventing conclusions about real children. Try this on a small, invented table before using any real schoolwork.

CHAPTER 14 OF 20 · Back to contents

14. Avoid joins on names, labels or other non-unique attributes

Start with the answer: Two people may share a name or project label; joining on such a field can create combinations that do not represent real relationships.

Even when a demo seems to work, repeated labels expose the risk. Use stable IDs and review primary-foreign-key constraints rather than trusting the appearance of textual matches.

Common tempting error: Assuming human-readable labels are unique across both tables.

Diagnostic for a parent or project partner: Could two different rows carry the same displayed label?

Lab 1: deliberately unsafe name-based join for a made-up quiz analysis

The current problem is avoid joins on names, labels or other non-unique attributes. In a made-up quiz analysis, an imaginary database has 6 student rows and 12 submissions. The question is deliberately unsafe name-based join. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Alya','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',37,2);
INSERT INTO submissions VALUES(102,1,'Q2',66,3);
INSERT INTO submissions VALUES(103,1,'Q3',67,1);
INSERT INTO submissions VALUES(104,2,'Q1',46,2);
INSERT INTO submissions VALUES(105,3,'Q1',90,2);
INSERT INTO submissions VALUES(106,3,'Q2',86,3);
INSERT INTO submissions VALUES(107,3,'Q3',96,1);
INSERT INTO submissions VALUES(108,4,'Q1',86,2);
INSERT INTO submissions VALUES(109,5,'Q1',77,2);
INSERT INTO submissions VALUES(110,5,'Q2',55,3);
INSERT INTO submissions VALUES(111,5,'Q3',76,1);
INSERT INTO submissions VALUES(112,6,'Q1',97,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u ON s.name=(SELECT name FROM students WHERE student_id=u.student_id) ORDER BY s.student_id,u.submission_id;

Worked answer. For the avoid joins on names, labels or other non-unique attributes exercise in a made-up quiz analysis, the SQLite 3 calculation for deliberately unsafe name-based join yields 16 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 104}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 104}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For avoid joins on names, labels or other non-unique attributes in a made-up quiz analysis, Two parents sharing the same name can be joined to each other’s submissions. It is a counterexample, not a recommended model. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. This is a proposed home exercise rather than a service or school claim. For this a made-up quiz analysis, require the learner to describe what one query row represents before publishing a finding.

Lab 2: deliberately unsafe name-based join for a weather-observation practice sheet

The current problem is avoid joins on names, labels or other non-unique attributes. A learner helping with a weather-observation practice sheet expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Alya','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',77,2);
INSERT INTO submissions VALUES(102,1,'Q2',72,3);
INSERT INTO submissions VALUES(103,3,'Q1',36,2);
INSERT INTO submissions VALUES(104,3,'Q2',64,3);
INSERT INTO submissions VALUES(105,5,'Q1',52,2);
INSERT INTO submissions VALUES(106,5,'Q2',68,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u ON s.name=(SELECT name FROM students WHERE student_id=u.student_id) ORDER BY s.student_id,u.submission_id;

Worked answer. For the avoid joins on names, labels or other non-unique attributes exercise in a weather-observation practice sheet, the SQLite 3 calculation for deliberately unsafe name-based join yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For avoid joins on names, labels or other non-unique attributes in a weather-observation practice sheet, Two parents sharing the same name can be joined to each other’s submissions. It is a counterexample, not a recommended model. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. A useful parent prompt is to request a prediction, not a memorised command. For this a weather-observation practice sheet, require the learner to describe what one query row represents before publishing a finding.

Lab 3: deliberately unsafe name-based join for a family revision tracker

The current problem is avoid joins on names, labels or other non-unique attributes. The activity log for a family revision tracker contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Alya','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',35,3);
INSERT INTO submissions VALUES(102,1,'Q2',97,1);
INSERT INTO submissions VALUES(103,3,'Q1',85,3);
INSERT INTO submissions VALUES(104,3,'Q2',82,1);
INSERT INTO submissions VALUES(105,5,'Q1',81,3);
INSERT INTO submissions VALUES(106,5,'Q2',45,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u ON s.name=(SELECT name FROM students WHERE student_id=u.student_id) ORDER BY s.student_id,u.submission_id;

Worked answer. For the avoid joins on names, labels or other non-unique attributes exercise in a family revision tracker, the SQLite 3 calculation for deliberately unsafe name-based join yields 8 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For avoid joins on names, labels or other non-unique attributes in a family revision tracker, Two parents sharing the same name can be joined to each other’s submissions. It is a counterexample, not a recommended model. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. The next step should be one small controlled variation, not ten simultaneous edits. For this a family revision tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 4: deliberately unsafe name-based join for a small library-book experiment

The current problem is avoid joins on names, labels or other non-unique attributes. A project partner has copied a query for a small library-book experiment but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Alya','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',74,3);
INSERT INTO submissions VALUES(102,2,'Q1',55,3);
INSERT INTO submissions VALUES(103,2,'Q2',44,1);
INSERT INTO submissions VALUES(104,2,'Q3',67,2);
INSERT INTO submissions VALUES(105,3,'Q1',75,3);
INSERT INTO submissions VALUES(106,4,'Q1',44,3);
INSERT INTO submissions VALUES(107,4,'Q2',90,1);
INSERT INTO submissions VALUES(108,4,'Q3',89,2);
INSERT INTO submissions VALUES(109,5,'Q1',74,3);
INSERT INTO submissions VALUES(110,6,'Q1',90,3);
INSERT INTO submissions VALUES(111,6,'Q2',91,1);
INSERT INTO submissions VALUES(112,6,'Q3',63,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u ON s.name=(SELECT name FROM students WHERE student_id=u.student_id) ORDER BY s.student_id,u.submission_id;

Worked answer. For the avoid joins on names, labels or other non-unique attributes exercise in a small library-book experiment, the SQLite 3 calculation for deliberately unsafe name-based join yields 16 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 104}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 2, “name”: “Alya”, “submission_id”: 104}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For avoid joins on names, labels or other non-unique attributes in a small library-book experiment, Two parents sharing the same name can be joined to each other’s submissions. It is a counterexample, not a recommended model. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A clear explanation is more valuable than guessing an answer from the display. For this a small library-book experiment, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe avoid joins on names, labels or other non-unique attributes in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Choose keys defined by the data model, not convenient strings. This is a proposed home exercise rather than a service or school claim.

CHAPTER 15 OF 20 · Back to contents

15. Use USING only when both columns truly mean the same key

Start with the answer: USING(student_id) is a compact equality join when both inputs have the same appropriately named relationship key.

The shorthand does not remove the need to understand cardinality. If key names or meanings differ, use an explicit ON expression.

Common tempting error: Applying USING to fields with the same name but unrelated meaning.

Diagnostic for a parent or project partner: Are the column definitions and identifier domains actually compatible?

Lab 1: USING(student_id) shorthand for a mock fundraising spreadsheet

The current problem is use using only when both columns truly mean the same key. In a mock fundraising spreadsheet, an imaginary database has 6 student rows and 6 submissions. The question is USING(student_id) shorthand. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',81,3);
INSERT INTO submissions VALUES(102,2,'Q2',41,1);
INSERT INTO submissions VALUES(103,4,'Q1',56,3);
INSERT INTO submissions VALUES(104,4,'Q2',51,1);
INSERT INTO submissions VALUES(105,6,'Q1',39,3);
INSERT INTO submissions VALUES(106,6,'Q2',95,1);

-- Checked query:
SELECT student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u USING(student_id) ORDER BY student_id,u.submission_id;

Worked answer. For the use using only when both columns truly mean the same key exercise in a mock fundraising spreadsheet, the SQLite 3 calculation for USING(student_id) shorthand yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 2, “name”: “Ben”, “submission_id”: 101}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 106}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use using only when both columns truly mean the same key in a mock fundraising spreadsheet, USING expresses equality on identically named key columns; it still produces one row per matching pair. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a mock fundraising spreadsheet, require the learner to describe what one query row represents before publishing a finding.

Lab 2: USING(student_id) shorthand for a toy digital noticeboard

The current problem is use using only when both columns truly mean the same key. A learner helping with a toy digital noticeboard expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',73,3);
INSERT INTO submissions VALUES(102,1,'Q2',59,1);
INSERT INTO submissions VALUES(103,1,'Q3',52,2);
INSERT INTO submissions VALUES(104,2,'Q1',44,3);
INSERT INTO submissions VALUES(105,3,'Q1',83,3);
INSERT INTO submissions VALUES(106,3,'Q2',57,1);
INSERT INTO submissions VALUES(107,3,'Q3',81,2);
INSERT INTO submissions VALUES(108,4,'Q1',65,3);
INSERT INTO submissions VALUES(109,5,'Q1',44,3);
INSERT INTO submissions VALUES(110,5,'Q2',51,1);
INSERT INTO submissions VALUES(111,5,'Q3',72,2);
INSERT INTO submissions VALUES(112,6,'Q1',79,3);

-- Checked query:
SELECT student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u USING(student_id) ORDER BY student_id,u.submission_id;

Worked answer. For the use using only when both columns truly mean the same key exercise in a toy digital noticeboard, the SQLite 3 calculation for USING(student_id) shorthand yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use using only when both columns truly mean the same key in a toy digital noticeboard, USING expresses equality on identically named key columns; it still produces one row per matching pair. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a toy digital noticeboard, require the learner to describe what one query row represents before publishing a finding.

Lab 3: USING(student_id) shorthand for a fictional class activity log

The current problem is use using only when both columns truly mean the same key. The activity log for a fictional class activity log contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',87,1);
INSERT INTO submissions VALUES(102,1,'Q2',50,2);
INSERT INTO submissions VALUES(103,1,'Q3',36,3);
INSERT INTO submissions VALUES(104,2,'Q1',39,1);
INSERT INTO submissions VALUES(105,3,'Q1',87,1);
INSERT INTO submissions VALUES(106,3,'Q2',97,2);
INSERT INTO submissions VALUES(107,3,'Q3',46,3);
INSERT INTO submissions VALUES(108,4,'Q1',87,1);
INSERT INTO submissions VALUES(109,5,'Q1',67,1);
INSERT INTO submissions VALUES(110,5,'Q2',78,2);
INSERT INTO submissions VALUES(111,5,'Q3',58,3);
INSERT INTO submissions VALUES(112,6,'Q1',87,1);

-- Checked query:
SELECT student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u USING(student_id) ORDER BY student_id,u.submission_id;

Worked answer. For the use using only when both columns truly mean the same key exercise in a fictional class activity log, the SQLite 3 calculation for USING(student_id) shorthand yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use using only when both columns truly mean the same key in a fictional class activity log, USING expresses equality on identically named key columns; it still produces one row per matching pair. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Treat a version-dependent result as a prompt to consult the current official manual. For this a fictional class activity log, require the learner to describe what one query row represents before publishing a finding.

Lab 4: USING(student_id) shorthand for a coastal ecology observation workbook

The current problem is use using only when both columns truly mean the same key. A project partner has copied a query for a coastal ecology observation workbook but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',91,1);
INSERT INTO submissions VALUES(102,1,'Q2',62,2);
INSERT INTO submissions VALUES(103,3,'Q1',55,1);
INSERT INTO submissions VALUES(104,3,'Q2',88,2);
INSERT INTO submissions VALUES(105,5,'Q1',96,1);
INSERT INTO submissions VALUES(106,5,'Q2',71,2);

-- Checked query:
SELECT student_id,s.name,u.submission_id FROM students s INNER JOIN submissions u USING(student_id) ORDER BY student_id,u.submission_id;

Worked answer. For the use using only when both columns truly mean the same key exercise in a coastal ecology observation workbook, the SQLite 3 calculation for USING(student_id) shorthand yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use using only when both columns truly mean the same key in a coastal ecology observation workbook, USING expresses equality on identically named key columns; it still produces one row per matching pair. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. Try this on a small, invented table before using any real schoolwork. For this a coastal ecology observation workbook, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe use using only when both columns truly mean the same key in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Expand USING into ON as a comprehension check. A Punggol family can practise this with fictional numbers after dinner or over a weekend.

CHAPTER 16 OF 20 · Back to contents

16. Match composite identifiers when one column is not enough

Start with the answer: A relationship can require both student_id and cycle (or project version), and each pair must match simultaneously.

Joining only by student_id across several cycles can pair a September record with an October record. Extra key columns protect the intended one-to-one or one-to-many relationship.

Common tempting error: Omitting the time or version component from a declared composite relationship.

Diagnostic for a parent or project partner: What pair of fields uniquely identifies this logical event?

Lab 1: student identifier with a cycle condition for a public-transport timing experiment

The current problem is match composite identifiers when one column is not enough. In a public-transport timing experiment, an imaginary database has 6 student rows and 12 submissions. The question is student identifier with a cycle condition. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',56,1);
INSERT INTO submissions VALUES(102,2,'Q1',39,1);
INSERT INTO submissions VALUES(103,2,'Q2',39,2);
INSERT INTO submissions VALUES(104,2,'Q3',62,3);
INSERT INTO submissions VALUES(105,3,'Q1',70,1);
INSERT INTO submissions VALUES(106,4,'Q1',62,1);
INSERT INTO submissions VALUES(107,4,'Q2',53,2);
INSERT INTO submissions VALUES(108,4,'Q3',89,3);
INSERT INTO submissions VALUES(109,5,'Q1',38,1);
INSERT INTO submissions VALUES(110,6,'Q1',66,1);
INSERT INTO submissions VALUES(111,6,'Q2',44,2);
INSERT INTO submissions VALUES(112,6,'Q3',67,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.cycle FROM students s LEFT JOIN submissions u ON s.student_id=u.student_id AND u.cycle=1 ORDER BY s.student_id,u.submission_id;

Worked answer. For the match composite identifiers when one column is not enough exercise in a public-transport timing experiment, the SQLite 3 calculation for student identifier with a cycle condition yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘cycle’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “cycle”: 1}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “cycle”: 1}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “cycle”: 1}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 106, “cycle”: 1}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 109, “cycle”: 1}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 110, “cycle”: 1}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For match composite identifiers when one column is not enough in a public-transport timing experiment, An extra predicate restricts matches to one cycle while preserving unmatched parents. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. No real pupil information is needed; the test data are deliberately imaginary. For this a public-transport timing experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 2: student identifier with a cycle condition for a fictional club membership register

The current problem is match composite identifiers when one column is not enough. A learner helping with a fictional club membership register expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',94,1);
INSERT INTO submissions VALUES(102,2,'Q2',93,2);
INSERT INTO submissions VALUES(103,4,'Q1',65,1);
INSERT INTO submissions VALUES(104,4,'Q2',85,2);
INSERT INTO submissions VALUES(105,6,'Q1',62,1);
INSERT INTO submissions VALUES(106,6,'Q2',52,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.cycle FROM students s LEFT JOIN submissions u ON s.student_id=u.student_id AND u.cycle=1 ORDER BY s.student_id,u.submission_id;

Worked answer. For the match composite identifiers when one column is not enough exercise in a fictional club membership register, the SQLite 3 calculation for student identifier with a cycle condition yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘cycle’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “cycle”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “cycle”: 1}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “cycle”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “cycle”: 1}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “cycle”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “cycle”: 1}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For match composite identifiers when one column is not enough in a fictional club membership register, An extra predicate restricts matches to one cycle while preserving unmatched parents. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional club membership register, require the learner to describe what one query row represents before publishing a finding.

Lab 3: student identifier with a cycle condition for a weekend reading journal

The current problem is match composite identifiers when one column is not enough. The activity log for a weekend reading journal contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',60,2);
INSERT INTO submissions VALUES(102,2,'Q2',49,3);
INSERT INTO submissions VALUES(103,4,'Q1',67,2);
INSERT INTO submissions VALUES(104,4,'Q2',42,3);
INSERT INTO submissions VALUES(105,6,'Q1',84,2);
INSERT INTO submissions VALUES(106,6,'Q2',68,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.cycle FROM students s LEFT JOIN submissions u ON s.student_id=u.student_id AND u.cycle=1 ORDER BY s.student_id,u.submission_id;

Worked answer. For the match composite identifiers when one column is not enough exercise in a weekend reading journal, the SQLite 3 calculation for student identifier with a cycle condition yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘cycle’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “cycle”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “cycle”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “cycle”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “cycle”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “cycle”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: null, “cycle”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For match composite identifiers when one column is not enough in a weekend reading journal, An extra predicate restricts matches to one cycle while preserving unmatched parents. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Save the example so the learner can rerun it with a fresh number next week. For this a weekend reading journal, require the learner to describe what one query row represents before publishing a finding.

Lab 4: student identifier with a cycle condition for a simulated science-club project

The current problem is match composite identifiers when one column is not enough. A project partner has copied a query for a simulated science-club project but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',88,2);
INSERT INTO submissions VALUES(102,1,'Q2',69,3);
INSERT INTO submissions VALUES(103,1,'Q3',67,1);
INSERT INTO submissions VALUES(104,2,'Q1',94,2);
INSERT INTO submissions VALUES(105,3,'Q1',85,2);
INSERT INTO submissions VALUES(106,3,'Q2',81,3);
INSERT INTO submissions VALUES(107,3,'Q3',71,1);
INSERT INTO submissions VALUES(108,4,'Q1',90,2);
INSERT INTO submissions VALUES(109,5,'Q1',71,2);
INSERT INTO submissions VALUES(110,5,'Q2',40,3);
INSERT INTO submissions VALUES(111,5,'Q3',52,1);
INSERT INTO submissions VALUES(112,6,'Q1',75,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.cycle FROM students s LEFT JOIN submissions u ON s.student_id=u.student_id AND u.cycle=1 ORDER BY s.student_id,u.submission_id;

Worked answer. For the match composite identifiers when one column is not enough exercise in a simulated science-club project, the SQLite 3 calculation for student identifier with a cycle condition yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘cycle’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “cycle”: 1}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “cycle”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “cycle”: 1}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “cycle”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 111, “cycle”: 1}, {“student_id”: 6, “name”: “Farid”, “submission_id”: null, “cycle”: null}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For match composite identifiers when one column is not enough in a simulated science-club project, An extra predicate restricts matches to one cycle while preserving unmatched parents. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. This is a proposed home exercise rather than a service or school claim. For this a simulated science-club project, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe match composite identifiers when one column is not enough in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Write every required equality in ON and verify a deliberately mismatched cycle. No real pupil information is needed; the test data are deliberately imaginary.

CHAPTER 17 OF 20 · Back to contents

17. Distinguish self joins from two different people

Start with the answer: The same student table can be joined to itself to represent a mentor reference; give each appearance a clear alias.

A mentor_id links one hypothetical member to another. LEFT JOIN preserves people without an assigned mentor; INNER JOIN would show only assigned relationships.

Common tempting error: Comparing a name with itself rather than following the mentor identifier.

Diagnostic for a parent or project partner: Which alias is the learner and which alias is the mentor?

Lab 1: self-joined mentor reference for a weather-observation practice sheet

The current problem is distinguish self joins from two different people. In a weather-observation practice sheet, an imaginary database has 6 student rows and 6 submissions. The question is self-joined mentor reference. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',43,2);
INSERT INTO submissions VALUES(102,1,'Q2',42,3);
INSERT INTO submissions VALUES(103,3,'Q1',94,2);
INSERT INTO submissions VALUES(104,3,'Q2',95,3);
INSERT INTO submissions VALUES(105,5,'Q1',36,2);
INSERT INTO submissions VALUES(106,5,'Q2',63,3);

-- Checked query:
SELECT s.student_id,s.name,m.name AS mentor_name FROM students s LEFT JOIN students m ON m.student_id=s.mentor_id ORDER BY s.student_id;

Worked answer. For the distinguish self joins from two different people exercise in a weather-observation practice sheet, the SQLite 3 calculation for self-joined mentor reference yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘mentor_name’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “mentor_name”: null}, {“student_id”: 2, “name”: “Ben”, “mentor_name”: “Alya”}, {“student_id”: 3, “name”: “Chloe”, “mentor_name”: null}, {“student_id”: 4, “name”: “Dev”, “mentor_name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”, “mentor_name”: “Dev”}, {“student_id”: 6, “name”: “Farid”, “mentor_name”: null}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish self joins from two different people in a weather-observation practice sheet, Each table alias plays a different role; a missing mentor produces NULL but the learner row survives. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. A useful parent prompt is to request a prediction, not a memorised command. For this a weather-observation practice sheet, require the learner to describe what one query row represents before publishing a finding.

Lab 2: self-joined mentor reference for a shared family study calendar

The current problem is distinguish self joins from two different people. A learner helping with a shared family study calendar expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',54,2);
INSERT INTO submissions VALUES(102,2,'Q1',65,2);
INSERT INTO submissions VALUES(103,2,'Q2',56,3);
INSERT INTO submissions VALUES(104,2,'Q3',77,1);
INSERT INTO submissions VALUES(105,3,'Q1',42,2);
INSERT INTO submissions VALUES(106,4,'Q1',84,2);
INSERT INTO submissions VALUES(107,4,'Q2',85,3);
INSERT INTO submissions VALUES(108,4,'Q3',36,1);
INSERT INTO submissions VALUES(109,5,'Q1',94,2);
INSERT INTO submissions VALUES(110,6,'Q1',57,2);
INSERT INTO submissions VALUES(111,6,'Q2',86,3);
INSERT INTO submissions VALUES(112,6,'Q3',46,1);

-- Checked query:
SELECT s.student_id,s.name,m.name AS mentor_name FROM students s LEFT JOIN students m ON m.student_id=s.mentor_id ORDER BY s.student_id;

Worked answer. For the distinguish self joins from two different people exercise in a shared family study calendar, the SQLite 3 calculation for self-joined mentor reference yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘mentor_name’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “mentor_name”: null}, {“student_id”: 2, “name”: “Ben”, “mentor_name”: “Alya”}, {“student_id”: 3, “name”: “Chloe”, “mentor_name”: null}, {“student_id”: 4, “name”: “Dev”, “mentor_name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”, “mentor_name”: “Dev”}, {“student_id”: 6, “name”: “Farid”, “mentor_name”: null}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish self joins from two different people in a shared family study calendar, Each table alias plays a different role; a missing mentor produces NULL but the learner row survives. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Independent checking beats a polished screen that nobody can explain. For this a shared family study calendar, require the learner to describe what one query row represents before publishing a finding.

Lab 3: self-joined mentor reference for a small library-book experiment

The current problem is distinguish self joins from two different people. The activity log for a small library-book experiment contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',42,3);
INSERT INTO submissions VALUES(102,2,'Q1',41,3);
INSERT INTO submissions VALUES(103,2,'Q2',53,1);
INSERT INTO submissions VALUES(104,2,'Q3',58,2);
INSERT INTO submissions VALUES(105,3,'Q1',50,3);
INSERT INTO submissions VALUES(106,4,'Q1',79,3);
INSERT INTO submissions VALUES(107,4,'Q2',77,1);
INSERT INTO submissions VALUES(108,4,'Q3',37,2);
INSERT INTO submissions VALUES(109,5,'Q1',90,3);
INSERT INTO submissions VALUES(110,6,'Q1',52,3);
INSERT INTO submissions VALUES(111,6,'Q2',45,1);
INSERT INTO submissions VALUES(112,6,'Q3',61,2);

-- Checked query:
SELECT s.student_id,s.name,m.name AS mentor_name FROM students s LEFT JOIN students m ON m.student_id=s.mentor_id ORDER BY s.student_id;

Worked answer. For the distinguish self joins from two different people exercise in a small library-book experiment, the SQLite 3 calculation for self-joined mentor reference yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘mentor_name’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “mentor_name”: null}, {“student_id”: 2, “name”: “Ben”, “mentor_name”: null}, {“student_id”: 3, “name”: “Chloe”, “mentor_name”: “Ben”}, {“student_id”: 4, “name”: “Dev”, “mentor_name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”, “mentor_name”: null}, {“student_id”: 6, “name”: “Farid”, “mentor_name”: “Esha”}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish self joins from two different people in a small library-book experiment, Each table alias plays a different role; a missing mentor produces NULL but the learner row survives. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. A clear explanation is more valuable than guessing an answer from the display. For this a small library-book experiment, require the learner to describe what one query row represents before publishing a finding.

Lab 4: self-joined mentor reference for a fictional student coding challenge

The current problem is distinguish self joins from two different people. A project partner has copied a query for a fictional student coding challenge but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',73,3);
INSERT INTO submissions VALUES(102,2,'Q2',40,1);
INSERT INTO submissions VALUES(103,4,'Q1',76,3);
INSERT INTO submissions VALUES(104,4,'Q2',80,1);
INSERT INTO submissions VALUES(105,6,'Q1',44,3);
INSERT INTO submissions VALUES(106,6,'Q2',39,1);

-- Checked query:
SELECT s.student_id,s.name,m.name AS mentor_name FROM students s LEFT JOIN students m ON m.student_id=s.mentor_id ORDER BY s.student_id;

Worked answer. For the distinguish self joins from two different people exercise in a fictional student coding challenge, the SQLite 3 calculation for self-joined mentor reference yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘mentor_name’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “mentor_name”: null}, {“student_id”: 2, “name”: “Ben”, “mentor_name”: null}, {“student_id”: 3, “name”: “Chloe”, “mentor_name”: “Ben”}, {“student_id”: 4, “name”: “Dev”, “mentor_name”: “Chloe”}, {“student_id”: 5, “name”: “Esha”, “mentor_name”: null}, {“student_id”: 6, “name”: “Farid”, “mentor_name”: “Esha”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For distinguish self joins from two different people in a fictional student coding challenge, Each table alias plays a different role; a missing mentor produces NULL but the learner row survives. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a fictional student coding challenge, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe distinguish self joins from two different people in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Use role-based aliases and never interpret NULL as a negative personal judgement. A useful parent prompt is to request a prediction, not a memorised command.

CHAPTER 18 OF 20 · Back to contents

18. Spot an accidental Cartesian product

Start with the answer: Without a meaningful join condition, a CROSS JOIN returns every combination of left and right rows.

A six-student toy table crossed with two shift choices yields twelve proposed pairings; none is automatically a real attendance record. The explosion illustrates why predicates matter.

Common tempting error: Mistaking generated combinations for observed relationships.

Diagnostic for a parent or project partner: Which rows represent possibilities rather than observed events?

Lab 1: all fictional student-by-shift combinations for a toy digital noticeboard

The current problem is spot an accidental cartesian product. In a toy digital noticeboard, an imaginary database has 6 student rows and 12 submissions. The question is all fictional student-by-shift combinations. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',72,3);
INSERT INTO submissions VALUES(102,1,'Q2',40,1);
INSERT INTO submissions VALUES(103,1,'Q3',65,2);
INSERT INTO submissions VALUES(104,2,'Q1',83,3);
INSERT INTO submissions VALUES(105,3,'Q1',96,3);
INSERT INTO submissions VALUES(106,3,'Q2',61,1);
INSERT INTO submissions VALUES(107,3,'Q3',49,2);
INSERT INTO submissions VALUES(108,4,'Q1',91,3);
INSERT INTO submissions VALUES(109,5,'Q1',48,3);
INSERT INTO submissions VALUES(110,5,'Q2',72,1);
INSERT INTO submissions VALUES(111,5,'Q3',62,2);
INSERT INTO submissions VALUES(112,6,'Q1',52,3);

-- Checked query:
SELECT s.student_id,s.name,t.shift FROM students s CROSS JOIN (SELECT 'AM' AS shift UNION ALL SELECT 'PM') t ORDER BY s.student_id,t.shift;

Worked answer. For the spot an accidental cartesian product exercise in a toy digital noticeboard, the SQLite 3 calculation for all fictional student-by-shift combinations yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘shift’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “shift”: “AM”}, {“student_id”: 1, “name”: “Alya”, “shift”: “PM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “AM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “PM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “AM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “PM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “AM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “PM”}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For spot an accidental cartesian product in a toy digital noticeboard, CROSS JOIN generates possible pairings, not observed attendance; six students by two shifts produces twelve rows. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a toy digital noticeboard, require the learner to describe what one query row represents before publishing a finding.

Lab 2: all fictional student-by-shift combinations for a family revision tracker

The current problem is spot an accidental cartesian product. A learner helping with a family revision tracker expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',90,3);
INSERT INTO submissions VALUES(102,1,'Q2',76,1);
INSERT INTO submissions VALUES(103,3,'Q1',42,3);
INSERT INTO submissions VALUES(104,3,'Q2',84,1);
INSERT INTO submissions VALUES(105,5,'Q1',62,3);
INSERT INTO submissions VALUES(106,5,'Q2',88,1);

-- Checked query:
SELECT s.student_id,s.name,t.shift FROM students s CROSS JOIN (SELECT 'AM' AS shift UNION ALL SELECT 'PM') t ORDER BY s.student_id,t.shift;

Worked answer. For the spot an accidental cartesian product exercise in a family revision tracker, the SQLite 3 calculation for all fictional student-by-shift combinations yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘shift’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “shift”: “AM”}, {“student_id”: 1, “name”: “Alya”, “shift”: “PM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “AM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “PM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “AM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “PM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “AM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “PM”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For spot an accidental cartesian product in a family revision tracker, CROSS JOIN generates possible pairings, not observed attendance; six students by two shifts produces twelve rows. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. The next step should be one small controlled variation, not ten simultaneous edits. For this a family revision tracker, require the learner to describe what one query row represents before publishing a finding.

Lab 3: all fictional student-by-shift combinations for a coastal ecology observation workbook

The current problem is spot an accidental cartesian product. The activity log for a coastal ecology observation workbook contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',65,1);
INSERT INTO submissions VALUES(102,1,'Q2',63,2);
INSERT INTO submissions VALUES(103,3,'Q1',36,1);
INSERT INTO submissions VALUES(104,3,'Q2',65,2);
INSERT INTO submissions VALUES(105,5,'Q1',78,1);
INSERT INTO submissions VALUES(106,5,'Q2',66,2);

-- Checked query:
SELECT s.student_id,s.name,t.shift FROM students s CROSS JOIN (SELECT 'AM' AS shift UNION ALL SELECT 'PM') t ORDER BY s.student_id,t.shift;

Worked answer. For the spot an accidental cartesian product exercise in a coastal ecology observation workbook, the SQLite 3 calculation for all fictional student-by-shift combinations yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘shift’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “shift”: “AM”}, {“student_id”: 1, “name”: “Alya”, “shift”: “PM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “AM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “PM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “AM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “PM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “AM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “PM”}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For spot an accidental cartesian product in a coastal ecology observation workbook, CROSS JOIN generates possible pairings, not observed attendance; six students by two shifts produces twelve rows. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. Try this on a small, invented table before using any real schoolwork. For this a coastal ecology observation workbook, require the learner to describe what one query row represents before publishing a finding.

Lab 4: all fictional student-by-shift combinations for an after-CCA practice planner

The current problem is spot an accidental cartesian product. A project partner has copied a query for an after-CCA practice planner but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',38,1);
INSERT INTO submissions VALUES(102,2,'Q1',61,1);
INSERT INTO submissions VALUES(103,2,'Q2',60,2);
INSERT INTO submissions VALUES(104,2,'Q3',75,3);
INSERT INTO submissions VALUES(105,3,'Q1',95,1);
INSERT INTO submissions VALUES(106,4,'Q1',77,1);
INSERT INTO submissions VALUES(107,4,'Q2',52,2);
INSERT INTO submissions VALUES(108,4,'Q3',41,3);
INSERT INTO submissions VALUES(109,5,'Q1',37,1);
INSERT INTO submissions VALUES(110,6,'Q1',84,1);
INSERT INTO submissions VALUES(111,6,'Q2',65,2);
INSERT INTO submissions VALUES(112,6,'Q3',57,3);

-- Checked query:
SELECT s.student_id,s.name,t.shift FROM students s CROSS JOIN (SELECT 'AM' AS shift UNION ALL SELECT 'PM') t ORDER BY s.student_id,t.shift;

Worked answer. For the spot an accidental cartesian product exercise in an after-CCA practice planner, the SQLite 3 calculation for all fictional student-by-shift combinations yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘shift’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “shift”: “AM”}, {“student_id”: 1, “name”: “Alya”, “shift”: “PM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “AM”}, {“student_id”: 2, “name”: “Ben”, “shift”: “PM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “AM”}, {“student_id”: 3, “name”: “Chloe”, “shift”: “PM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “AM”}, {“student_id”: 4, “name”: “Dev”, “shift”: “PM”}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For spot an accidental cartesian product in an after-CCA practice planner, CROSS JOIN generates possible pairings, not observed attendance; six students by two shifts produces twelve rows. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. No real pupil information is needed; the test data are deliberately imaginary. For this an after-CCA practice planner, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe spot an accidental cartesian product in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Use CROSS JOIN only when generating combinations is the explicit task. The goal is not to finish a huge worksheet; it is to explain one reliable decision.

CHAPTER 19 OF 20 · Back to contents

19. Use ORDER BY for presentation without changing membership

Start with the answer: JOIN alone does not promise a convenient output order; ORDER BY expresses an intended display sequence.

Sorting can help readers compare student IDs or scores but does not repair a wrong join predicate or missing matches. A report should state any ordering choice explicitly.

Common tempting error: Treating the first row as the earliest or strongest result without a sort.

Diagnostic for a parent or project partner: Which query property determines the order shown to a parent?

Lab 1: explicitly sorted LEFT JOIN output for a fictional club membership register

The current problem is use order by for presentation without changing membership. In a fictional club membership register, an imaginary database has 6 student rows and 6 submissions. The question is explicitly sorted LEFT JOIN output. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,2,'Q1',37,1);
INSERT INTO submissions VALUES(102,2,'Q2',85,2);
INSERT INTO submissions VALUES(103,4,'Q1',93,1);
INSERT INTO submissions VALUES(104,4,'Q2',42,2);
INSERT INTO submissions VALUES(105,6,'Q1',65,1);
INSERT INTO submissions VALUES(106,6,'Q2',41,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.name,u.score DESC,u.submission_id;

Worked answer. For the use order by for presentation without changing membership exercise in a fictional club membership register, the SQLite 3 calculation for explicitly sorted LEFT JOIN output yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: null, “score”: null}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 102, “score”: 85}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 101, “score”: 37}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: null, “score”: null}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 103, “score”: 93}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 104, “score”: 42}, {“student_id”: 5, “name”: “Esha”, “submission_id”: null, “score”: null}, {“student_id”: 6, “name”: “Farid”, “submission_id”: 105, “score”: 65}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use order by for presentation without changing membership in a fictional club membership register, Sorting presentation does not change which records matched or how many rows survived. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 6 matched child records; a legitimate change should be traceable to the specific added row. When the result surprises the learner, keep the counterexample in the practice notes. For this a fictional club membership register, require the learner to describe what one query row represents before publishing a finding.

Lab 2: explicitly sorted LEFT JOIN output for a fictional class activity log

The current problem is use order by for presentation without changing membership. A learner helping with a fictional class activity log expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 0 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','North',NULL);
INSERT INTO students VALUES(2,'Ben','South',1);
INSERT INTO students VALUES(3,'Chloe','Waterway',2);
INSERT INTO students VALUES(4,'Dev','North',NULL);
INSERT INTO students VALUES(5,'Esha','South',4);
INSERT INTO students VALUES(6,'Farid','Waterway',5);
INSERT INTO submissions VALUES(101,1,'Q1',78,1);
INSERT INTO submissions VALUES(102,1,'Q2',61,2);
INSERT INTO submissions VALUES(103,1,'Q3',97,3);
INSERT INTO submissions VALUES(104,2,'Q1',89,1);
INSERT INTO submissions VALUES(105,3,'Q1',39,1);
INSERT INTO submissions VALUES(106,3,'Q2',70,2);
INSERT INTO submissions VALUES(107,3,'Q3',44,3);
INSERT INTO submissions VALUES(108,4,'Q1',78,1);
INSERT INTO submissions VALUES(109,5,'Q1',72,1);
INSERT INTO submissions VALUES(110,5,'Q2',35,2);
INSERT INTO submissions VALUES(111,5,'Q3',74,3);
INSERT INTO submissions VALUES(112,6,'Q1',86,1);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.name,u.score DESC,u.submission_id;

Worked answer. For the use order by for presentation without changing membership exercise in a fictional class activity log, the SQLite 3 calculation for explicitly sorted LEFT JOIN output yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “score”: 97}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 78}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “score”: 61}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “score”: 89}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “score”: 70}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “score”: 44}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “score”: 39}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 78}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use order by for presentation without changing membership in a fictional class activity log, Sorting presentation does not change which records matched or how many rows survived. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Treat a version-dependent result as a prompt to consult the current official manual. For this a fictional class activity log, require the learner to describe what one query row represents before publishing a finding.

Lab 3: explicitly sorted LEFT JOIN output for a simulated science-club project

The current problem is use order by for presentation without changing membership. The activity log for a simulated science-club project contains 12 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',90,2);
INSERT INTO submissions VALUES(102,1,'Q2',94,3);
INSERT INTO submissions VALUES(103,1,'Q3',72,1);
INSERT INTO submissions VALUES(104,2,'Q1',77,2);
INSERT INTO submissions VALUES(105,3,'Q1',49,2);
INSERT INTO submissions VALUES(106,3,'Q2',65,3);
INSERT INTO submissions VALUES(107,3,'Q3',42,1);
INSERT INTO submissions VALUES(108,4,'Q1',97,2);
INSERT INTO submissions VALUES(109,5,'Q1',68,2);
INSERT INTO submissions VALUES(110,5,'Q2',48,3);
INSERT INTO submissions VALUES(111,5,'Q3',51,1);
INSERT INTO submissions VALUES(112,6,'Q1',96,2);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.name,u.score DESC,u.submission_id;

Worked answer. For the use order by for presentation without changing membership exercise in a simulated science-club project, the SQLite 3 calculation for explicitly sorted LEFT JOIN output yields 12 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “score”: 94}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 90}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 103, “score”: 72}, {“student_id”: 2, “name”: “Ben”, “submission_id”: 104, “score”: 77}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 106, “score”: 65}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 105, “score”: 49}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 107, “score”: 42}, {“student_id”: 4, “name”: “Dev”, “submission_id”: 108, “score”: 97}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use order by for presentation without changing membership in a simulated science-club project, Sorting presentation does not change which records matched or how many rows survived. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. This is a proposed home exercise rather than a service or school claim. For this a simulated science-club project, require the learner to describe what one query row represents before publishing a finding.

Lab 4: explicitly sorted LEFT JOIN output for a recycled-materials tally

The current problem is use order by for presentation without changing membership. A project partner has copied a query for a recycled-materials tally but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',54,2);
INSERT INTO submissions VALUES(102,1,'Q2',74,3);
INSERT INTO submissions VALUES(103,3,'Q1',97,2);
INSERT INTO submissions VALUES(104,3,'Q2',52,3);
INSERT INTO submissions VALUES(105,5,'Q1',68,2);
INSERT INTO submissions VALUES(106,5,'Q2',72,3);

-- Checked query:
SELECT s.student_id,s.name,u.submission_id,u.score FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id ORDER BY s.name,u.score DESC,u.submission_id;

Worked answer. For the use order by for presentation without changing membership exercise in a recycled-materials tally, the SQLite 3 calculation for explicitly sorted LEFT JOIN output yields 9 output row(s) with columns [‘student_id’, ‘name’, ‘submission_id’, ‘score’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “submission_id”: 102, “score”: 74}, {“student_id”: 1, “name”: “Alya”, “submission_id”: 101, “score”: 54}, {“student_id”: 2, “name”: “Ben”, “submission_id”: null, “score”: null}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 103, “score”: 97}, {“student_id”: 3, “name”: “Chloe”, “submission_id”: 104, “score”: 52}, {“student_id”: 4, “name”: “Dev”, “submission_id”: null, “score”: null}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 106, “score”: 72}, {“student_id”: 5, “name”: “Esha”, “submission_id”: 105, “score”: 68}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For use order by for presentation without changing membership in a recycled-materials tally, Sorting presentation does not change which records matched or how many rows survived. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 3 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. A useful parent prompt is to request a prediction, not a memorised command. For this a recycled-materials tally, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe use order by for presentation without changing membership in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Choose a display sort only after membership and cardinality are correct. When the result surprises the learner, keep the counterexample in the practice notes.

CHAPTER 20 OF 20 · Back to contents

20. Reconcile left counts, matched pairs and missing parents

Start with the answer: A final join audit can compare input parent count, qualifying child count, matched-pair count and unmatched-parent count.

For a straightforward one-to-many model, a LEFT JOIN result has one row for each matched pair plus one placeholder row for every unmatched left parent. This explains why output row counts may exceed parent counts.

Common tempting error: Expecting every legitimate join to preserve exactly the original number of parents as rows.

Diagnostic for a parent or project partner: Can the learner account for each output row from the two input tables?

Lab 1: one-row-per-parent match audit for a shared family study calendar

The current problem is reconcile left counts, matched pairs and missing parents. In a shared family study calendar, an imaginary database has 6 student rows and 12 submissions. The question is one-row-per-parent match audit. Predict which parent identities and how many child matches appear. Decide whether the query needs all parents or only qualifying pairs before running it. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,1,'Q1',86,2);
INSERT INTO submissions VALUES(102,2,'Q1',59,2);
INSERT INTO submissions VALUES(103,2,'Q2',90,3);
INSERT INTO submissions VALUES(104,2,'Q3',72,1);
INSERT INTO submissions VALUES(105,3,'Q1',38,2);
INSERT INTO submissions VALUES(106,4,'Q1',90,2);
INSERT INTO submissions VALUES(107,4,'Q2',81,3);
INSERT INTO submissions VALUES(108,4,'Q3',62,1);
INSERT INTO submissions VALUES(109,5,'Q1',38,2);
INSERT INTO submissions VALUES(110,6,'Q1',58,2);
INSERT INTO submissions VALUES(111,6,'Q2',81,3);
INSERT INTO submissions VALUES(112,6,'Q3',76,1);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS matches FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the reconcile left counts, matched pairs and missing parents exercise in a shared family study calendar, the SQLite 3 calculation for one-row-per-parent match audit yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘matches’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “matches”: 1}, {“student_id”: 2, “name”: “Ben”, “matches”: 3}, {“student_id”: 3, “name”: “Chloe”, “matches”: 1}, {“student_id”: 4, “name”: “Dev”, “matches”: 3}, {“student_id”: 5, “name”: “Esha”, “matches”: 1}, {“student_id”: 6, “name”: “Farid”, “matches”: 3}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For reconcile left counts, matched pairs and missing parents in a shared family study calendar, Sum of reported matches equals actual child records with valid parent matches. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Start by identifying the unit of observation, then apply only the rule relevant to that unit..

Check the boundary. To expose the difference between a row and a relationship, add a new submission for an existing student and inspect whether the new result grows by one matching pair, a group count, or a value within an aggregate. The current input has 12 matched child records; a legitimate change should be traceable to the specific added row. Independent checking beats a polished screen that nobody can explain. For this a shared family study calendar, require the learner to describe what one query row represents before publishing a finding.

Lab 2: one-row-per-parent match audit for a weekend reading journal

The current problem is reconcile left counts, matched pairs and missing parents. A learner helping with a weekend reading journal expects exactly 6 output rows because the parent table has that many people. Test this assumption with the join below, inspecting the one-to-many relationship and the 3 student(s) without a submission. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','South',NULL);
INSERT INTO students VALUES(2,'Ben','Waterway',1);
INSERT INTO students VALUES(3,'Chloe','North',NULL);
INSERT INTO students VALUES(4,'Dev','South',3);
INSERT INTO students VALUES(5,'Esha','Waterway',4);
INSERT INTO students VALUES(6,'Farid','North',NULL);
INSERT INTO submissions VALUES(101,2,'Q1',63,2);
INSERT INTO submissions VALUES(102,2,'Q2',41,3);
INSERT INTO submissions VALUES(103,4,'Q1',77,2);
INSERT INTO submissions VALUES(104,4,'Q2',91,3);
INSERT INTO submissions VALUES(105,6,'Q1',73,2);
INSERT INTO submissions VALUES(106,6,'Q2',57,3);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS matches FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the reconcile left counts, matched pairs and missing parents exercise in a weekend reading journal, the SQLite 3 calculation for one-row-per-parent match audit yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘matches’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “matches”: 0}, {“student_id”: 2, “name”: “Ben”, “matches”: 2}, {“student_id”: 3, “name”: “Chloe”, “matches”: 0}, {“student_id”: 4, “name”: “Dev”, “matches”: 2}, {“student_id”: 5, “name”: “Esha”, “matches”: 0}, {“student_id”: 6, “name”: “Farid”, “matches”: 2}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For reconcile left counts, matched pairs and missing parents in a weekend reading journal, Sum of reported matches equals actual child records with valid parent matches. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Check the case that would make a tempting shortcut wrong..

Check the boundary. Now remove one submission linked to student ID 1, preserving all other records. The number of students remains 6. Compare the INNER and LEFT forms and explain why a student with zero remaining matches is retained only by the preserving join or an explicit parent-based summary. Save the example so the learner can rerun it with a fresh number next week. For this a weekend reading journal, require the learner to describe what one query row represents before publishing a finding.

Lab 3: one-row-per-parent match audit for a fictional student coding challenge

The current problem is reconcile left counts, matched pairs and missing parents. The activity log for a fictional student coding challenge contains 6 child records referring to the current student table. State whether the requested answer is about original pupils or matching event pairs. Examine the query and justify its row count independently. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,2,'Q1',42,3);
INSERT INTO submissions VALUES(102,2,'Q2',40,1);
INSERT INTO submissions VALUES(103,4,'Q1',82,3);
INSERT INTO submissions VALUES(104,4,'Q2',81,1);
INSERT INTO submissions VALUES(105,6,'Q1',37,3);
INSERT INTO submissions VALUES(106,6,'Q2',79,1);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS matches FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the reconcile left counts, matched pairs and missing parents exercise in a fictional student coding challenge, the SQLite 3 calculation for one-row-per-parent match audit yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘matches’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “matches”: 0}, {“student_id”: 2, “name”: “Ben”, “matches”: 2}, {“student_id”: 3, “name”: “Chloe”, “matches”: 0}, {“student_id”: 4, “name”: “Dev”, “matches”: 2}, {“student_id”: 5, “name”: “Esha”, “matches”: 0}, {“student_id”: 6, “name”: “Farid”, “matches”: 2}]. Of the original submissions, 6 refer to a parent in the six-row student table, and 3 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For reconcile left counts, matched pairs and missing parents in a fictional student coding challenge, Sum of reported matches equals actual child records with valid parent matches. This experiment starts with 6 left students and 6 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Cross-check with an independent hand calculation using the smallest workable values..

Check the boundary. Challenge the output by changing one child student_id to 99 in the toy database. That creates an orphan in this intentionally unenforced fixture. An INNER student-to-submission join loses the orphan pair; a child-left join can expose it. Do not perform such a change in a real school database. A Punggol family can practise this with fictional numbers after dinner or over a weekend. For this a fictional student coding challenge, require the learner to describe what one query row represents before publishing a finding.

Lab 4: one-row-per-parent match audit for a mock group presentation tracker

The current problem is reconcile left counts, matched pairs and missing parents. A project partner has copied a query for a mock group presentation tracker but cannot explain whether it preserves unmatched members. Run the small fixture, list the first several output rows, and account for the pairs created by the ON condition. One student can match up to three fictional quiz records; the database is a toy model rather than an actual school system.

-- Disposable SQLite practice: fictional names only.
CREATE TABLE students (student_id INTEGER PRIMARY KEY,name TEXT NOT NULL, house TEXT, mentor_id INTEGER);
CREATE TABLE submissions (submission_id INTEGER PRIMARY KEY, student_id INTEGER NOT NULL, quiz TEXT, score INTEGER, cycle INTEGER);
INSERT INTO students VALUES(1,'Alya','Waterway',NULL);
INSERT INTO students VALUES(2,'Ben','North',NULL);
INSERT INTO students VALUES(3,'Chloe','South',2);
INSERT INTO students VALUES(4,'Dev','Waterway',3);
INSERT INTO students VALUES(5,'Esha','North',NULL);
INSERT INTO students VALUES(6,'Farid','South',5);
INSERT INTO submissions VALUES(101,1,'Q1',86,3);
INSERT INTO submissions VALUES(102,1,'Q2',55,1);
INSERT INTO submissions VALUES(103,1,'Q3',56,2);
INSERT INTO submissions VALUES(104,2,'Q1',75,3);
INSERT INTO submissions VALUES(105,3,'Q1',69,3);
INSERT INTO submissions VALUES(106,3,'Q2',97,1);
INSERT INTO submissions VALUES(107,3,'Q3',51,2);
INSERT INTO submissions VALUES(108,4,'Q1',66,3);
INSERT INTO submissions VALUES(109,5,'Q1',61,3);
INSERT INTO submissions VALUES(110,5,'Q2',93,1);
INSERT INTO submissions VALUES(111,5,'Q3',78,2);
INSERT INTO submissions VALUES(112,6,'Q1',40,3);

-- Checked query:
SELECT s.student_id,s.name,COUNT(u.submission_id) AS matches FROM students s LEFT JOIN submissions u ON u.student_id=s.student_id GROUP BY s.student_id,s.name ORDER BY s.student_id;

Worked answer. For the reconcile left counts, matched pairs and missing parents exercise in a mock group presentation tracker, the SQLite 3 calculation for one-row-per-parent match audit yields 6 output row(s) with columns [‘student_id’, ‘name’, ‘matches’]. The first rows are [{“student_id”: 1, “name”: “Alya”, “matches”: 3}, {“student_id”: 2, “name”: “Ben”, “matches”: 1}, {“student_id”: 3, “name”: “Chloe”, “matches”: 3}, {“student_id”: 4, “name”: “Dev”, “matches”: 1}, {“student_id”: 5, “name”: “Esha”, “matches”: 3}, {“student_id”: 6, “name”: “Farid”, “matches”: 1}]. Of the original submissions, 12 refer to a parent in the six-row student table, and 0 students have no submissions. The listed SQL—not an assumed one-row-per-student rule—determines the observed count.

What makes that answer true. For reconcile left counts, matched pairs and missing parents in a mock group presentation tracker, Sum of reported matches equals actual child records with valid parent matches. This experiment starts with 6 left students and 12 child records. An INNER JOIN emits matching pairs; a LEFT JOIN from students emits those pairs plus one NULL-extended row per unmatched student, unless later filters remove such rows. For this chapter the particular predicate must be read literally; a score condition, NULL check, EXISTS test or aggregate can change the output shape. Write down the unsuccessful interpretation and explain why it fails..

Check the boundary. Ask whether a report needs every quiz or just a presence flag. In this fixture 0 parents lack children. EXISTS answers the presence question once per parent, while an ordinary INNER JOIN retains multiplicity. This is a difference in the reader’s question, not a presentation preference. The goal is not to finish a huge worksheet; it is to explain one reliable decision. For this a mock group presentation tracker, require the learner to describe what one query row represents before publishing a finding.

Explain it without the screen

At the end of this chapter, remove the working code from view. Ask the learner to describe reconcile left counts, matched pairs and missing parents in a new example, identify the input that matters, and state which visible evidence could falsify the first answer. A parent can listen for the causal step rather than attempting to memorise library or application syntax. The best checkpoint is a tiny counterexample saved alongside the successful case.

Chapter decision: Save the invariant with the query and rerun it when data changes. Independent checking beats a polished screen that nobody can explain.

Parent decision route: what should happen next?

Step 1. If a learner cannot identify which column is a key, practise the table model and one equality predicate. A join becomes meaningful only when the record relationship is clear.

Step 2. If the result repeats names, ask how many child records belong to that parent. Do not delete legitimate submissions merely to make the answer one row long.

Step 3. If a LEFT JOIN seems to have lost students, inspect WHERE conditions that reference right-side columns. Move a filter to ON only when that produces the intended question and preservation rule.

Step 4. If counts disagree, compare COUNT(*) with COUNT(child_key) and manually count the real child rows for one unmatched parent.

Step 5. Keep exercises in an isolated local database with fictional values. Do not put identifiable student data, passwords or actual school system access into a public tutoring demonstration.

Step 6. Ask for a small before-and-after query with written predictions. A person who can explain both INNER and LEFT on the same tiny fixture is ready to learn group summaries, integrity checks and more specialised SQL.

Frequently asked questions

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN outputs only matching row pairs. LEFT JOIN also preserves an unmatched row from the left table with NULL values for missing right-side columns.

Why do student names repeat after joining?

One student may match several submissions. The joined result then has one row per matching pair. Repetition is expected for a genuine one-to-many relationship.

Does LEFT JOIN always keep every student?

It preserves left rows at the join step, but a later WHERE predicate on right-side fields can filter away the NULL-extended rows.

Should I count rows with COUNT(*)?

COUNT(*) counts joined rows, including a placeholder for a student with no submission. COUNT(non_null_child_key) counts actual matched child records.

How do I find students with no submissions?

LEFT JOIN students to submissions and filter where a non-nullable submission primary key is NULL, or use NOT EXISTS.

Is joining on student name acceptable?

A human-readable name can repeat or change. Join on properly defined stable identifiers where possible.

How do I find orphan child records?

Start from the child table, LEFT JOIN the parent, and look for missing parent keys; database foreign-key enforcement may prevent orphans in production.

Does SQL guarantee the first row will be the earliest?

No. State an ORDER BY on the appropriate time or position field when order matters.

Can I use JOIN on two columns?

Yes. Put both equalities in ON, or use an appropriate matching composite-key design. Test an intentionally mismatched record.

Is one row per student always better?

Not if the question asks for each submission. For a per-student summary, aggregate the child table deliberately and then join.

Is this a compulsory SEAB SQL syllabus topic?

This is digital and computational enrichment, not a claim about the requirements of a specific exam cohort. Check official subject documents for any course-specific requirement.

How should a parent judge the next step?

Listen for an explanation of the join predicate, expected row count, unmatched behaviour and one real counterexample. Support should address whichever of those the learner cannot yet justify.

Primary sources and scope

Primary documentation: Official PostgreSQL table-expression and JOIN documentation. Read the current version notes or compatibility statements before using a different software version from these examples.

Further verified reference: SQLite SELECT syntax and JOIN processing.

Further verified reference: SQLite foreign-key reference.

This educational casebook was reviewed 17 October 2026. Invented datasets and classroom scenarios are shown explicitly as examples, not reports about actual Punggol students. Singapore curriculum details are not attributed to a syllabus without current official MOE or SEAB source checks. These techniques are coding, digital literacy and problem-solving enrichment, not claims that a particular programming API is assessed at a particular level.

Continue from here: Start Here · Tuition · Education · Pathways · Parenting 101 · All Site Routes

eduKate Punggol

Contact

83 Punggol Central, Singapore 828761

edu|Kate Bukit Timah

8 Fourth Avenue, Singapore 268674

By Appointment +65 8823 1234
admin@edukatesg.com

Email Us

When a child finally understands, school becomes less frightening and the future opens wider. Email us for the latest schedules and fees.

← 返回

感谢您的回复。 ✨