/en-tutor-prep-s6-term2-ict-sql-query-results/
/en-tutor-prep-s6-term2-ict-sql-query-results/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

S6 Term 2 | ICT: SQL Query Results and Join Row Counts – INNER, LEFT, GROUP BY and Normalisation | Tutor Lesson Plan | 45-minute flow, teaching script, common errors, practice with answers

S6 Term 2 | ICT: SQL Query Results and Join Row Counts – INNER, LEFT, GROUP BY and Normalisation | Tutor Lesson Plan | 45-minute flow, teaching script, common errors, practice with answers

  • 2026-12-05

Tutor lesson plan · S6 Term 2 ICT

SQL questions do not test typing; they test whether you can say how many rows come back. This lesson counts join rows, group output and keys in one pass, so every later query question starts with a row count.

Lesson objectives By the end students can: (1) use Student with 10 rows and Enrol with 9 to show INNER JOIN returns 9 rows and LEFT JOIN 10, naming the extra row as S06 with no enrolment; (2) group the 9 matched rows by Class for C1 4, C2 3 and C3 2, with the largest group at 44.4 per cent; (3) state that Enrol uses the composite key (SID, CID), that SName depends on SID alone and is therefore a partial dependency, and that a nested loop costs 90 comparisons against 19 with an index.

01 Prior knowledge check (first 3 minutes)

If any item below is not secure, fix it before this lesson:

Skill Quick check Pass standard
Skill: primary key Which column identifies a row in Student SID; answering SName mixes a descriptor with a key
Skill: matching sets A has 10 and B has 9: which column decides the pairs The shared SID, not the row number
Skill: percentages What percentage of 9 is 4 4 over 9 times 100, which is 44.4

02 Lesson flow (45 minutes)

Time Stage Tutor does Student does
0-5 min Diagnose Ask how many rows each table holds and how many survive the match Estimates, then checks
5-14 min INNER and LEFT Match row by row on SID; S06 has no enrolment, so INNER drops it and LEFT keeps it with NULL Counts 9 and 10
14-23 min GROUP BY Group the 9 rows by Class for 4, 3 and 2; the largest is 4 over 9, or 44.4 per cent Fills the group table and the percentage
23-32 min Keys and normalisation Each table has a key; Enrol uses (SID, CID); SName depends on SID alone Names the partial dependency
32-40 min Query cost A nested loop compares 10 times 9 for 90; with an index it is 10 plus 9 for 19, saving 71 Works out 90 and 19
40-45 min Exit check Collect three items and note the failure type Answers without notes

03 Teaching script (read it aloud)

Act 1: count rows before writing SQL

The number of rows a join returns depends on whether the join column has a match, not on how many rows each table holds. Student has 10 rows, Enrol has 9, and the join column is SID. S06 never appears in Enrol, so INNER JOIN keeps only the 9 matched rows. LEFT JOIN keeps the left table whole, so S06 survives with NULL for the class, giving 10 rows. The one-row gap is that student. Before writing anything, answer one question: do we keep students without a class? Yes means LEFT, no means INNER, and that answer fixes the row count.

Most common misconception: thinking LEFT JOIN always returns more rows. When every left key has a match, the two agree.

Ask the student to repeat: “Student 10, Enrol 9, S06 has none, so INNER gives 9 and LEFT gives 10.”

Act 2: work out the GROUP BY output yourself

Grouping first sorts rows by a column value and then computes one aggregate per group. The 9 matched rows split into C1 with 4, C2 with 3 and C3 with 2, and 4 plus 3 plus 2 is 9, which matches the joined rows and is the best check. The largest group is C1, and 4 over 9 times 100 is 44.4 per cent. After GROUP BY, SELECT may hold only grouping columns and aggregates; a name column would be indeterminate because each group holds several names. That rule is tested often.

Most common misconception: treating COUNT(*) and COUNT(SID) as the same. COUNT(SID) skips NULL while COUNT(*) counts rows.

Ask the student to repeat: “C1 4, C2 3, C3 2, which makes 9; the largest is 44.4 per cent, and grouping allows only group columns and aggregates.”

Act 3: keys, partial dependency and query cost

Each table has a key: Student uses SID, Course uses CID and Enrol uses the composite key (SID, CID), because one student takes several courses and one course holds several students, so neither column alone identifies a row. In Enrol, SID is a foreign key to Student and CID is a foreign key to Course. Merge all three into one table and SName depends on SID alone rather than on the whole key, which is a partial dependency and fails 2NF; splitting Student and Enrol back removes it. Cost is countable too: a nested loop without an index performs 10 times 9, or 90 comparisons, while an index on Enrol.SID needs one lookup per student, so 10 plus 9 gives 19 and saves 71.

Most common misconception: treating normalisation as splitting for its own sake. Tables split along dependencies, or the join can no longer restore the data.

Ask the student to repeat: “Composite key (SID, CID); SName alone on SID is a partial dependency; 90 against 19.”

04 Question types and steps

Type Example Steps Answer
Join row counts Student 10 rows, Enrol 9, S06 has none Match by SID; LEFT pads NULL INNER 9, LEFT 10
GROUP BY The 9 matched rows by Class Count each group; the total must be 9 C1 4, C2 3, C3 2; largest 44.4 per cent
Keys Student, Course, Enrol A composite key uses two columns; foreign keys point to other keys (SID, CID); SID and CID are foreign keys
Query cost 10 students over 9 enrolments No index multiplies, an index adds 90 against 19, saving 71
INNER JOIN 9 rows, LEFT JOIN 10; GROUP BY gives 4, 3 and 2Student (10 rows)S01 ChanS02 LeeS03 WongS04 HoS05 YipS06 NgS07 TsangS08 MaS09 LauS10 ToEnrol (9 rows)S01 C1S02 C1S03 C2S04 C1S05 C2S07 C3S08 C1S09 C2S10 C3S06 never appears in Enrol, so INNER loses itJoin row counts9 rowsINNER10 rowsLEFTThe 1-row gap is the student with no enrolmentGROUP BY Class over the 9 matched rows4C13C22C3C1 is 44.4 per cent

Left: Student holds 10 rows, Enrol 9, and S06 never enrols (orange). Middle: INNER JOIN keeps the 9 matched rows while LEFT JOIN also keeps S06 for 10. Right: grouping the 9 matched rows by class gives C1 4, C2 3 and C3 2, the largest share being 44.4 per cent.

Sanity check (must teach):Check row counts two ways: INNER equals the Enrol row count of 9, and LEFT is 9 plus 1, where the gap must equal the unmatched left rows. Check grouping by the total: 4 plus 3 plus 2 must equal the 9 joined rows, and a mismatch means a lost or double-counted row. Check the percentage by 4 over 9, since 0.444 times 9 returns 4. Check cost with two formulas: without an index it is left rows times right rows, with an index it is left plus right. When a cell disagrees, check whether the NULL rows from LEFT JOIN were counted inside a class group.

05 Three independent practice questions

Question Answer What to observe
Student has 12 rows and Enrol 15, with 3 students holding no enrolment: how many rows for INNER and LEFT? INNER keeps the 15 matched enrolment rows; LEFT returns 15 plus 3, or 18, with NULL classes for the three students who never enrolled. Whether three students were confused with three rows
By the same data, must the class groups add to 15 or to 18, and why? They add to 15, because GROUP BY works on the 15 matched rows; the three NULL classes form their own group, which is excluded when the question asks only for real classes. Whether the NULL group was added in
A 500-row table is joined to a 400-row table: how many comparisons without an index, and with one? Without an index 500 times 400 is 200,000; with an index 500 plus 400 is 900, a saving of 199,100. Whether the indexed case was multiplied

06 Common errors and correction lines

Error Student usually writes Tutor says
LEFT always more rows “LEFT JOIN always returns more than INNER” “When every right key matches, they return the same; extra rows are unmatched left rows.”
Name outside GROUP BY “SELECT SName, Class, COUNT(*) … GROUP BY Class” “SName is neither grouped nor aggregated, so its value is undetermined.”
Wrong primary key “Enrol can use SID as its key” “A student takes several courses, so the key is (SID, CID).”
Cost formulas mixed “An index still multiplies” “With an index it is left plus right, so 10 plus 9 gives 19.”

07 Homework (paper and question numbers)

08 Exit check (last 3 minutes)

Question Answer Verdict
Student 10 rows, Enrol 9, one student without a class: INNER and LEFT rows? INNER 9, LEFT 10 Answering the same for both means the match is unclear: reteach act one
The 9 joined rows split by class: what do the three groups add to? 4 plus 3 plus 2, which is 9 Answering 8 or 10 means a lost or repeated row: reteach act two
What is the primary key of Enrol? The composite key (SID, CID) Answering SID or CID alone misses the many-to-many link: reteach act three

09 Tutor FAQ

Which year is this for?

S6 Term 2 ICT, in the databases and SQL unit. S4 covered relations and normalisation; this lesson fixes query row counts and cost with one method.

Does 45 minutes cover it?

It covers join rows, group output, keys and one cost comparison. Subqueries and views need a separate lesson.

Is there a paper to go with it?

Yes. Homework links to questions 9 to 12 of the S6 ICT paper (set 2), covering keys, GROUP BY, partial dependency and join row counts.

Find a tutor freeTutor sign-up →

©2026 TutorZone · www.tutorzone.com.hk · Terms of use · All rights reserved. Original TutorZone material for personal study and teaching only; not for resale or republication.

Take this guide with you

Reading the full guide is free. Downloading the print version, printing it for students, or taking a whole-level pack needs a tutor account (free).

Registered tutors also get:

Register as a tutor — freeAlready registered? Log in

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →