๐๏ธ SQL for beginners ยท Class 12 ยท Lesson 18 of 18 ยท Free
Capstone: School Database System in SQL
โฑ About 50 minutes ยท โถ run real SQL here โ nothing to install ยท no sign-in
In this lesson you will
- Extend the school database with a new table that has proper keys and checks
- Combine JOINs, GROUP BY, CASE, views and window functions into real reports
- Record data safely with a transaction and a trigger, and check data quality
The principal of our practice school wants one system for everything: report cards, class ranks, fee dues, attendance and a quick health check of the data. You already know every tool needed, from JOINs to window functions. In this capstone you put them together on the school database you have used all course.
What tables does a school database system need?
Our school already has five tables: students, subjects, teachers, marks and fees. Marks and fees point to students with foreign keys (Lesson 5).
The school now wants attendance. One row per student per day, so the key is (student_id, day). present can only be 0 or 1, and the CHECK makes sure of it.
The last query lists every table, from SQLite's own sqlite_master (Lesson 10). Try it: add a column note TEXT to attendance and run again.
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
Table attendance created. โโโโโโโโโโโโโโ โ name โ โโโโโโโโโโโโโโค โ attendance โ โ fees โ โ marks โ โ students โ โ subjects โ โ teachers โ โโโโโโโโโโโโโโ (6 rows)
How do I make a report card with SQL?
A report card needs each student's subjects, average and a grade. LEFT JOIN (Lesson 7) keeps Rohan, who has no marks yet. GROUP BY (Lesson 8) gives one row per student.
CASE (Lesson 2) turns the average into a grade. The first WHEN checks for NULL, so a student without marks gets a clear message instead of a wrong grade.
Try it: change the A grade's 85 to 90 and run again. Who drops to B?
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
โโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโโโ โ class โ name โ subjects โ average โ grade โ โโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโค โ 11 โ Meera Iyer โ 5 โ 90.8 โ A โ โ 11 โ Anaya Joshi โ 3 โ 84 โ B โ โ 11 โ Dev Patil โ 4 โ 70.3 โ B โ โ 12 โ Riya Sharma โ 5 โ 87.6 โ A โ โ 12 โ Sara Khan โ 5 โ 80.2 โ B โ โ 12 โ Aman Verma โ 5 โ 77.2 โ B โ โ 12 โ Kabir Singh โ 4 โ 58.3 โ C โ โ 12 โ Rohan Das โ 0 โ NULL โ No marks yet โ โโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโโโโ (8 rows)
How do I rank students inside each class in SQL?
The principal wants a rank list for each class. A CTE (Lesson 3) first works out each student's average. Then RANK() OVER (PARTITION BY class โฆ) from Lesson 16 restarts the ranking for every class.
Students without marks have no average, so the INNER JOIN leaves them out of the rank list. That is fair: you cannot rank someone with no marks.
Try it: add WHERE class = 12 to the outer query and run again.
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
โโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโ โ class โ name โ average โ class_rank โ โโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโค โ 11 โ Meera Iyer โ 90.8 โ 1 โ โ 11 โ Anaya Joshi โ 84 โ 2 โ โ 11 โ Dev Patil โ 70.3 โ 3 โ โ 12 โ Riya Sharma โ 87.6 โ 1 โ โ 12 โ Sara Khan โ 80.2 โ 2 โ โ 12 โ Aman Verma โ 77.2 โ 3 โ โ 12 โ Kabir Singh โ 58.3 โ 4 โ โโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโโ (7 rows)
How do I build a fee dashboard with a view?
The accounts office checks fees every day. A view (Lesson 10) called fee_status joins fees with students once, so later reports stay short.
The dashboard then adds up money per class. SUM with CASE splits collected and pending in one pass, without two separate queries.
Try it: pay Aman's July fee with UPDATE fees SET paid = 1 WHERE student_id = 2 AND month = '2026-07'; before the SELECT, and run again.
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
View fee_status created. โโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโ โ class โ collected โ pending โ โโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโค โ 11 โ 12000 โ 4000 โ โ 12 โ 22500 โ 9000 โ โโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโ (2 rows)
How do I record attendance safely in SQL?
Attendance for a day should be saved together or not at all, so it goes inside a transaction (Lesson 12). A trigger (Lesson 14) blocks entries for a Sunday. strftime('%w', day) gives the weekday, and '0' means Sunday.
The report then works out each student's attendance percentage. 100.0 (not 100) makes SQLite divide with decimals.
Try it: change one date to '2026-12-06', a Sunday, and run again. The trigger stops it with its message.
๐ Tables in this example: students, subjects, teachers, marks, fees, attendance โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1); CREATE TABLE attendance ( student_id INTEGER NOT NULL REFERENCES students(id), day TEXT NOT NULL, present INTEGER NOT NULL CHECK (present IN (0, 1)), PRIMARY KEY (student_id, day) );
Trigger no_sunday created. Transaction started. 9 rows inserted. Transaction committed (saved). โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโ โ name โ present_days โ percent โ โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโโค โ Riya Sharma โ 3 โ 100 โ โ Aman Verma โ 2 โ 66.7 โ โ Sara Khan โ 2 โ 66.7 โ โโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโโ (3 rows)
How do I check a database for missing data?
Before a report goes to parents, check the data. LEFT JOIN โฆ WHERE โฆ IS NULL (Lesson 7) finds rows with no partner: a student with no marks, a subject with no teacher.
UNION ALL stacks the results of several SELECTs into one list, as long as they have the same number of columns. So all problems show in one table.
As the school grows, add an index on columns these reports filter and join on, like marks(subject_id) (Lesson 11). Try it: delete the last UNION ALL block and run again.
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
โโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโ โ problem โ item โ โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโค โ Student with no marks โ Rohan Das โ โ Teacher with no subject โ Mrs. Nair โ โ Subject with no teacher โ Chemistry โ โโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโ (3 rows)
๐ค Predict the output
Think like SQL: read the code and choose what it prints. You get the reason straight away.
Question 1. There are 31 marks rows and 8 students. Rohan has no marks. How many rows does the LEFT JOIN give?
๐ The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT COUNT(*) FROM students st LEFT JOIN marks m ON m.student_id = st.id;
Question 2. Kabir (student 6) scored 55, 62, 49 and 67. Which grade?
๐ The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT CASE
WHEN AVG(marks) >= 85 THEN 'A'
WHEN AVG(marks) >= 70 THEN 'B'
WHEN AVG(marks) >= 55 THEN 'C'
ELSE 'D'
END
FROM marks
WHERE student_id = 6;Question 3. How much fee is pending for Class 12?
๐ The tables (tap to see them)
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
SELECT SUM(f.amount) FROM fees f JOIN students st ON st.id = f.student_id WHERE st.class = 12 AND f.paid = 0;
๐งฉ Your challenge
Build the principal's one-screen dashboard: for each class, the number of students, the average of all its marks (1 decimal) and the fees pending. Use one CTE per number, then join them on class. Expected: 11 | 3 | 82.3 | 4000 and 12 | 5 | 76.7 | 9000.
โโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ โ class โ students โ avg_marks โ fees_pending โ โโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโโโโโค โ 11 โ 3 โ 82.3 โ 4000 โ โ 12 โ 5 โ 76.7 โ 9000 โ โโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโโโ (2 rows)
๐ Tables in this example: students, subjects, teachers, marks, fees โ tap to see how they are made
-- TalentJR practice school (made-up data for SQL lessons) CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, class INTEGER NOT NULL, section TEXT NOT NULL, city TEXT, joined TEXT NOT NULL ); INSERT INTO students VALUES (1, 'Riya Sharma', 12, 'A', 'Pune', '2019-06-10'), (2, 'Aman Verma', 12, 'A', 'Mumbai', '2020-06-15'), (3, 'Sara Khan', 12, 'B', 'Pune', '2019-06-10'), (4, 'Dev Patil', 11, 'A', 'Nagpur', '2021-06-14'), (5, 'Meera Iyer', 11, 'B', 'Chennai', '2021-06-14'), (6, 'Kabir Singh', 12, 'B', 'Delhi', '2022-07-01'), (7, 'Anaya Joshi', 11, 'A', 'Pune', '2023-06-12'), (8, 'Rohan Das', 12, 'A', NULL, '2023-06-12'); CREATE TABLE subjects ( id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE ); INSERT INTO subjects VALUES (1, 'Maths'), (2, 'Physics'), (3, 'Chemistry'), (4, 'Computer Science'), (5, 'English'); CREATE TABLE teachers ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, subject_id INTEGER REFERENCES subjects(id) ); INSERT INTO teachers VALUES (1, 'Mrs. Kulkarni', 1), (2, 'Mr. Rao', 2), (3, 'Ms. Fernandes', 4), (4, 'Mr. Gupta', 5), (5, 'Mrs. Nair', NULL); CREATE TABLE marks ( student_id INTEGER NOT NULL REFERENCES students(id), subject_id INTEGER NOT NULL REFERENCES subjects(id), marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100), PRIMARY KEY (student_id, subject_id) ); INSERT INTO marks VALUES (1,1,92),(1,2,88),(1,3,79),(1,4,95),(1,5,84), (2,1,75),(2,2,68),(2,3,72),(2,4,81),(2,5,90), (3,1,81),(3,2,77),(3,3,85),(3,4,70),(3,5,88), (4,1,64),(4,2,58),(4,4,88),(4,5,71), (5,1,95),(5,2,91),(5,3,89),(5,4,93),(5,5,86), (6,1,55),(6,2,62),(6,3,49),(6,5,67), (7,1,73),(7,4,99),(7,5,80); CREATE TABLE fees ( student_id INTEGER NOT NULL REFERENCES students(id), month TEXT NOT NULL, amount INTEGER NOT NULL, paid INTEGER NOT NULL DEFAULT 0 ); INSERT INTO fees VALUES (1,'2026-06',4500,1),(1,'2026-07',4500,1),(2,'2026-06',4500,1),(2,'2026-07',4500,0), (3,'2026-06',4500,1),(4,'2026-06',4000,1),(4,'2026-07',4000,0),(5,'2026-06',4000,1), (6,'2026-06',4500,0),(7,'2026-06',4000,1),(8,'2026-06',4500,1);
โ ๏ธ Common mistakes (and how to fix them)
โ Wrong
SELECT st.name, COUNT(m.marks) AS subjects FROM students st JOIN marks m ON m.student_id = st.id WHERE st.class = 12 GROUP BY st.id ORDER BY st.name;
Runs, but gives the wrong result: โโโโโโโโโโโโโโโฌโโโโโโโโโโโ โ name โ subjects โ โโโโโโโโโโโโโโโผโโโโโโโโโโโค โ Aman Verma โ 5 โ โ Kabir Singh โ 4 โ โ Riya Sharma โ 5 โ โ Sara Khan โ 5 โ โโโโโโโโโโโโโโโดโโโโโโโโโโโ
โ Right
SELECT st.name, COUNT(m.marks) AS subjects FROM students st LEFT JOIN marks m ON m.student_id = st.id WHERE st.class = 12 GROUP BY st.id ORDER BY st.name;
An INNER JOIN drops students with no marks, so Rohan silently vanishes from the list. LEFT JOIN keeps him with 0 subjects.
โ Wrong
SELECT SUM(marks) / COUNT(*) AS average FROM marks WHERE student_id = 1;
Runs, but gives the wrong result: โโโโโโโโโโโ โ average โ โโโโโโโโโโโค โ 87 โ โโโโโโโโโโโ (1 row)
โ Right
SELECT ROUND(AVG(marks), 1) AS average FROM marks WHERE student_id = 1;
Both numbers are whole numbers, so SQLite does integer division and throws away the decimals (87 instead of 87.6). Use AVG, or multiply by 1.0 first.
โ Wrong
SELECT SUM(f.amount) AS total_fees FROM students st JOIN marks m ON m.student_id = st.id JOIN fees f ON f.student_id = st.id WHERE st.id = 1;
Runs, but gives the wrong result: โโโโโโโโโโโโโโ โ total_fees โ โโโโโโโโโโโโโโค โ 45000 โ โโโโโโโโโโโโโโ (1 row)
โ Right
SELECT SUM(amount) AS total_fees FROM fees WHERE student_id = 1;
Joining marks and fees together pairs every fee with every mark, so each fee is counted 5 times. Add up each table on its own (or in separate CTEs).
๐ Remember
- A school system is a set of related tables with keys and checks: plan them before you query.
- LEFT JOIN + GROUP BY + CASE makes a report card that keeps students without marks.
- Window functions rank inside each class; views keep daily dashboards short.
- Transactions and triggers keep new data complete and valid.
- Check data quality with LEFT JOIN โฆ IS NULL, and never join two unrelated child tables before SUM.
Finished the quiz and the challenge?
Questions students ask
How do I design a database for a school project?
List the things (students, subjects, teachers, fees, attendance) and make one table for each. Give every table a primary key, link them with foreign keys, and normalize to 3NF (Lesson 15). Then write the reports you need and add views and indexes for them.
Which database should I use for a school management system?
Any relational database works. SQLite is fine for learning and for one computer. A school website used by many people at once would usually use a server database like MySQL or PostgreSQL. The SQL in this course moves across with small changes.
How can I practise this capstone further?
Add a timetable table (class, day, period, subject, teacher), then write queries for a teacher's weekly load and clashes. Add an exam table with dates, and rank students per exam with window functions.
Why do my totals come out too big after a JOIN?
When one student has many marks and many fees, joining both gives every mark-fee pair, so sums repeat. Total each child table on its own, in a subquery or CTE, and join the totals.