Query & Retrieval Lab
Every example below is a real question a school office would ask, the exact SQL that answers
it, and the live result — run straight from database/learning_queries.sql.
نیچے دی گئی ہر مثال اسکول کے دفتری نظام میں اٹھائے جانے والے ایک حقیقی سوال، اس کا
جواب دینے والی درست ایس کیو ایل (SQL) کیوری، اور اس کے لائیو نتائج پر مشتمل ہے —
جو براہِ راست database/learning_queries.sql سے چلائی گئی ہے۔
Query 1 — Show all students
Question: “Who are all our students?”
Tables: students
Keys used in the JOIN: None — no JOIN needed.
SELECT student_id, admission_no, first_name, last_name, gender, status FROM students ORDER BY last_name, first_name;
ORDER BY only changes display order, not which rows come back.
| student_id | admission_no | first_name | last_name | gender | status |
|---|---|---|---|---|---|
| 11 | ADM-2025-011 | Hassan | Abbas | Male | Active |
| 2 | ADM-2025-002 | Sara | Ahmed | Female | Active |
| 17 | ADM-2025-017 | Yusuf | Anwar | Male | Active |
| 14 | ADM-2025-014 | Khadija | Aslam | Female | Active |
| 20 | ADM-2025-020 | Hira | Basit | Female | Active |
| 15 | ADM-2025-015 | Bilawal | Chaudhry | Male | Active |
| 7 | ADM-2025-007 | Usman | Farooq | Male | Active |
| 8 | ADM-2025-008 | Mahnoor | Iqbal | Female | Active |
20 row(s) total, showing first 8.
Query 2 — Find student by admission number
Question: “Who is ADM-2025-001?”
Tables: students
Keys used in the JOIN: admission_no — a UNIQUE key, so at most one row can match.
SELECT student_id, admission_no, first_name, last_name, date_of_birth FROM students WHERE admission_no = 'ADM-2025-001';
Because admission_no is UNIQUE, this WHERE clause can never return more than one row.
| student_id | admission_no | first_name | last_name | date_of_birth |
|---|---|---|---|---|
| 1 | ADM-2025-001 | Ali | Khan | 2014-03-12 |
1 row(s) total.
Query 3 — Students in Grade 5, Section A
Question: “Which students are in Grade 5-A this year?”
Tables: students, student_enrollments, classes
Keys used in the JOIN: students.student_id = student_enrollments.student_id
student_enrollments.class_id = classes.class_id
SELECT s.student_id, s.first_name, s.last_name, c.class_name, c.section FROM students s INNER JOIN student_enrollments e ON e.student_id = s.student_id INNER JOIN classes c ON c.class_id = e.class_id WHERE c.class_name = 'Grade 5' AND c.section = 'A' AND c.academic_year = '2025-2026' ORDER BY s.last_name;
INNER JOIN only returns rows with a match on both sides — a student with no enrollment row would disappear here.
| student_id | first_name | last_name | class_name | section |
|---|---|---|---|---|
| 2 | Sara | Ahmed | Grade 5 | A |
| 1 | Ali | Khan | Grade 5 | A |
| 3 | Bilal | Raza | Grade 5 | A |
| 5 | Hamza | Sheikh | Grade 5 | A |
| 4 | Ayesha | Tariq | Grade 5 | A |
5 row(s) total.
Query 4 — Every student's parent(s)
Question: “Who are each student's parents/guardians?”
Tables: students, student_parents, parents
Keys used in the JOIN: students.student_id = student_parents.student_id
student_parents.parent_id = parents.parent_id
SELECT s.first_name AS student_first, s.last_name AS student_last, p.first_name AS parent_first, p.last_name AS parent_last, sp.relationship_type, sp.is_primary_contact FROM students s INNER JOIN student_parents sp ON sp.student_id = s.student_id INNER JOIN parents p ON p.parent_id = sp.parent_id ORDER BY s.last_name, s.first_name;
This walks through the junction table — there is no parent_id column directly on students, on purpose (it is a Many-to-Many relationship).
| student_first | student_last | parent_first | parent_last | relationship_type | is_primary_contact |
|---|---|---|---|---|---|
| Hassan | Abbas | Zafar | Abbas | Father | 1 |
| Hassan | Abbas | Shazia | Zafar | Mother | 0 |
| Sara | Ahmed | Imran | Khan | Father | 0 |
| Sara | Ahmed | Nasreen | Khan | Mother | 1 |
| Yusuf | Anwar | Naveed | Aslam | Guardian | 1 |
| Khadija | Aslam | Tariq | Mehmood | Father | 1 |
| Hira | Basit | Nasreen | Khan | Mother | 1 |
| Bilawal | Chaudhry | Farah | Yousaf | Guardian | 1 |
30 row(s) total, showing first 8.
Query 5 — Teacher + class + subject assignments
Question: “Which teacher teaches which subject to which class?”
Tables: class_subject_teachers, classes, subjects, teachers
Keys used in the JOIN: class_subject_teachers.class_id = classes.class_id
class_subject_teachers.subject_id = subjects.subject_id
class_subject_teachers.teacher_id = teachers.teacher_id
SELECT c.class_name, c.section, sub.subject_name, t.first_name AS teacher_first, t.last_name AS teacher_last FROM class_subject_teachers cst INNER JOIN classes c ON c.class_id = cst.class_id INNER JOIN subjects sub ON sub.subject_id = cst.subject_id INNER JOIN teachers t ON t.teacher_id = cst.teacher_id ORDER BY c.class_name, c.section, sub.subject_name;
One assignment row, three JOINs — this is the whole reason the junction table carries three Foreign Keys at once.
| class_name | section | subject_name | teacher_first | teacher_last |
|---|---|---|---|---|
| Grade 5 | A | Art | Nadia | Sheikh |
| Grade 5 | A | English | Bilal | Hussain |
| Grade 5 | A | Mathematics | Sarah | Ahmed |
| Grade 5 | A | Science | Ayesha | Malik |
| Grade 5 | A | Social Studies | Omar | Farooq |
| Grade 5 | A | Urdu | Omar | Farooq |
| Grade 5 | B | Art | Nadia | Sheikh |
| Grade 5 | B | English | Bilal | Hussain |
24 row(s) total, showing first 8.
Query 6 — Daily attendance for one date
Question: “Who was present/absent on 2025-09-03?”
Tables: attendance, students, classes
Keys used in the JOIN: attendance.student_id = students.student_id
attendance.class_id = classes.class_id
SELECT a.attendance_date, s.first_name, s.last_name, c.class_name, c.section, a.status FROM attendance a INNER JOIN students s ON s.student_id = a.student_id INNER JOIN classes c ON c.class_id = a.class_id WHERE a.attendance_date = '2025-09-03' ORDER BY c.class_name, s.last_name;
A straightforward two-way JOIN filtered by a specific date.
| attendance_date | first_name | last_name | class_name | section | status |
|---|---|---|---|---|---|
| 2025-09-03 | Sara | Ahmed | Grade 5 | A | Present |
| 2025-09-03 | Ali | Khan | Grade 5 | A | Present |
| 2025-09-03 | Bilal | Raza | Grade 5 | A | Absent |
| 2025-09-03 | Hamza | Sheikh | Grade 5 | A | Present |
| 2025-09-03 | Ayesha | Tariq | Grade 5 | A | Present |
| 2025-09-03 | Hassan | Abbas | Grade 6 | A | Present |
| 2025-09-03 | Khadija | Aslam | Grade 6 | A | Present |
| 2025-09-03 | Bilawal | Chaudhry | Grade 6 | A | Present |
10 row(s) total, showing first 8.
Query 7 — Absent students
Question: “Who has been marked Absent, and when?”
Tables: attendance, students, classes
Keys used in the JOIN: Same JOIN shape as Query 6.
SELECT a.attendance_date, s.first_name, s.last_name, c.class_name, c.section, a.remarks FROM attendance a INNER JOIN students s ON s.student_id = a.student_id INNER JOIN classes c ON c.class_id = a.class_id WHERE a.status = 'Absent' ORDER BY a.attendance_date;
Same JOIN as Query 6, filtered by status instead of by date — proof that a JOIN's shape and a query's WHERE clause are independent choices.
| attendance_date | first_name | last_name | class_name | section | remarks |
|---|---|---|---|---|---|
| 2025-09-02 | Mariam | Yousaf | Grade 6 | A | No reason given |
| 2025-09-03 | Bilal | Raza | Grade 5 | A | Sick leave informed by parent |
| 2025-09-04 | Khadija | Aslam | Grade 6 | A | Family emergency |
3 row(s) total.
Query 8 — Exam results with subject names
Question: “How did each student do, per subject, per exam?”
Tables: exam_results, exams, students, subjects
Keys used in the JOIN: exam_results.exam_id = exams.exam_id
exam_results.student_id = students.student_id
exam_results.subject_id = subjects.subject_id
SELECT e.exam_name, s.first_name, s.last_name, sub.subject_name, r.marks_obtained, r.grade FROM exam_results r INNER JOIN exams e ON e.exam_id = r.exam_id INNER JOIN students s ON s.student_id = r.student_id INNER JOIN subjects sub ON sub.subject_id = r.subject_id ORDER BY e.exam_name, s.last_name, sub.subject_name;
Three JOINs turn three raw id columns into readable names.
| exam_name | first_name | last_name | subject_name | marks_obtained | grade |
|---|---|---|---|---|---|
| Mid-Term Exam | Hassan | Abbas | English | 68.00 | C |
| Mid-Term Exam | Hassan | Abbas | Mathematics | 60.00 | C |
| Mid-Term Exam | Hassan | Abbas | Science | 69.00 | C |
| Mid-Term Exam | Sara | Ahmed | English | 70.00 | B |
| Mid-Term Exam | Sara | Ahmed | Mathematics | 72.00 | B |
| Mid-Term Exam | Sara | Ahmed | Science | 69.00 | C |
| Mid-Term Exam | Khadija | Aslam | English | 81.00 | A |
| Mid-Term Exam | Khadija | Aslam | Mathematics | 89.00 | A |
30 row(s) total, showing first 8.
Query 9 — Unpaid fee invoices
Question: “Which invoices still need to be collected?”
Tables: fee_invoices, students
Keys used in the JOIN: fee_invoices.student_id = students.student_id
SELECT s.first_name, s.last_name, i.invoice_month, i.amount, i.due_date, i.status FROM fee_invoices i INNER JOIN students s ON s.student_id = i.student_id WHERE i.status IN ('Unpaid', 'Overdue') ORDER BY i.due_date;
A filter on status narrows a simple JOIN down to just the invoices that still need action.
| first_name | last_name | invoice_month | amount | due_date | status |
|---|---|---|---|---|---|
| Amna | Riaz | 2025-09 | 5000.00 | 2025-09-10 | Unpaid |
| Talha | Nadeem | 2025-09 | 5000.00 | 2025-09-10 | Unpaid |
| Hira | Basit | 2025-09 | 5000.00 | 2025-09-10 | Unpaid |
| Omar | Saeed | 2025-10 | 5000.00 | 2025-10-10 | Unpaid |
| Khadija | Aslam | 2025-10 | 5000.00 | 2025-10-10 | Unpaid |
| Bilawal | Chaudhry | 2025-10 | 5000.00 | 2025-10-10 | Unpaid |
| Sana | Rafiq | 2025-10 | 5000.00 | 2025-10-10 | Unpaid |
| Yusuf | Anwar | 2025-10 | 5000.00 | 2025-10-10 | Unpaid |
20 row(s) total, showing first 8.
Query 10 — Total paid by each student
Question: “How much has each student's family paid in total?”
Tables: students, fee_invoices, fee_payments
Keys used in the JOIN: students.student_id = fee_invoices.student_id
fee_invoices.invoice_id = fee_payments.invoice_id
SELECT s.student_id, s.first_name, s.last_name, COALESCE(SUM(p.amount_paid), 0) AS total_paid FROM students s LEFT JOIN fee_invoices i ON i.student_id = s.student_id LEFT JOIN fee_payments p ON p.invoice_id = i.invoice_id GROUP BY s.student_id, s.first_name, s.last_name ORDER BY total_paid DESC;
LEFT JOIN is essential here — a student with an unpaid invoice and no payments yet must still appear, with a total of 0, instead of disappearing from the report.
| student_id | first_name | last_name | total_paid |
|---|---|---|---|
| 2 | Sara | Ahmed | 10000.00 |
| 1 | Ali | Khan | 10000.00 |
| 3 | Bilal | Raza | 10000.00 |
| 11 | Hassan | Abbas | 5000.00 |
| 7 | Usman | Farooq | 5000.00 |
| 8 | Mahnoor | Iqbal | 5000.00 |
| 6 | Zainab | Malik | 5000.00 |
| 10 | Fatima | Noor | 5000.00 |
20 row(s) total, showing first 8.
Query 11 — Count students per class
Question: “How many students are in each class right now?”
Tables: classes, student_enrollments
Keys used in the JOIN: classes.class_id = student_enrollments.class_id
SELECT c.class_name, c.section, COUNT(e.student_id) AS student_count FROM classes c LEFT JOIN student_enrollments e ON e.class_id = c.class_id AND e.status = 'Active' GROUP BY c.class_id, c.class_name, c.section ORDER BY c.class_name, c.section;
LEFT JOIN keeps a class in the results even with 0 enrolled students; COUNT(e.student_id) only counts non-NULL matches, so an empty class correctly shows 0, not 1.
| class_name | section | student_count |
|---|---|---|
| Grade 5 | A | 5 |
| Grade 5 | B | 5 |
| Grade 6 | A | 5 |
| Grade 6 | B | 3 |
4 row(s) total.
Query 12 — Average marks per subject
Question: “Which subject has the highest average score school-wide?”
Tables: exam_results, subjects
Keys used in the JOIN: exam_results.subject_id = subjects.subject_id
SELECT sub.subject_name, ROUND(AVG(r.marks_obtained), 2) AS average_marks, COUNT(*) AS results_counted FROM exam_results r INNER JOIN subjects sub ON sub.subject_id = r.subject_id GROUP BY sub.subject_id, sub.subject_name ORDER BY average_marks DESC;
AVG() and COUNT() are aggregate functions — GROUP BY subject collapses many result rows into one summary row per subject.
| subject_name | average_marks | results_counted |
|---|---|---|
| Mathematics | 81.00 | 10 |
| Science | 73.30 | 10 |
| English | 71.10 | 10 |
3 row(s) total.
Explain this page
- How are these queries organised?
- They start simple (a single SELECT on one table) and build up to multi-table JOINs and aggregation with GROUP BY. Each example shows the SQL, a plain-English explanation of the key technique, and the actual live result rows from the database.
- Why twelve examples?
- They cover every major retrieval pattern used in real school reporting: finding a record by a unique key, filtering, sorting, joining across junction tables, calculating aggregates, and counting with LEFT JOIN to include empty groups.
- یہ کیوریز کس ترتیب سے ہیں؟
- یہ سادہ سے شروع ہوتی ہیں (ایک ٹیبل پر سادہ SELECT) اور بتدریج ملٹی-ٹیبل جوائنز (Multi-table JOINs) اور GROUP BY کے ساتھ اگریگیشن (Aggregation) تک پہنچتی ہیں۔ ہر مثال میں ایس کیو ایل (SQL)، تکنیک کی آسان وضاحت، اور ڈیٹا بیس سے حاصل شدہ لائیو نتائج شامل ہیں۔
- بارہ مثالیں کیوں؟
- یہ اسکول کی رپورٹنگ میں استعمال ہونے والے تمام اہم ڈیٹا بازیابی (Retrieval) طریقوں کو احاطہ کرتی ہیں: یونیک کی (Unique Key) سے ریکارڈ تلاش کرنا، فلٹر (Filter) کرنا، ترتیب دینا، جنکشن ٹیبلز (Junction Tables) کے پار جوائن (JOIN) کرنا، اگریگیٹ (Aggregate) حساب لگانا، اور خالی گروپوں کو شامل رکھنے کے لیے LEFT JOIN کے ساتھ گنتی کرنا۔