TalentJR

🗄️ SQL for beginners · Class 12 · Lesson 7 of 18 · Free

SQL Joins Deep Dive – INNER, LEFT, RIGHT, FULL, Self and CROSS JOIN

⏱ About 40 minutes · ▶ run real SQL here — nothing to install · no sign-in

In this lesson you will

  • Choose between INNER, LEFT, RIGHT and FULL OUTER JOIN
  • Find rows that have no match, like students with no marks
  • Join three or more tables, a table to itself, and every-pair CROSS JOINs

The report card needs a student's name from one table, subject names from another, marks from a third and the teacher from a fourth. Data is split on purpose (Lesson 5), so nothing is stored twice. Joins put it back together, and choosing the right join decides who appears on the report and who silently disappears.

What is an INNER JOIN in SQL?

In Lesson 2 you met JOIN … ON. Plain JOIN is short for INNER JOIN. It pairs rows from two tables wherever the ON condition is true, and it keeps ONLY the pairs it finds.

Look at what is missing below. Mrs. Nair has no subject_id, so she has no partner and is dropped. Chemistry has no teacher, so it is dropped too. INNER JOIN never warns you about this.

Try it: change JOIN to LEFT JOIN and run again. Who comes back?

📋 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);
🗄️ SQL — you can edit this query
Result
┌───────────────┬──────────────────┐
│ teacher       │ subject          │
├───────────────┼──────────────────┤
│ Mr. Gupta     │ English          │
│ Mr. Rao       │ Physics          │
│ Mrs. Kulkarni │ Maths            │
│ Ms. Fernandes │ Computer Science │
└───────────────┴──────────────────┘
(4 rows)

What is the difference between LEFT JOIN and INNER JOIN?

LEFT JOIN keeps every row of the left table (the one after FROM), matched or not. Where there is no match, the right table's columns are NULL.

The second query lists every student with their Maths mark. Rohan has no marks, so he gets NULL instead of vanishing. Notice the Maths filter sits inside ON, not in WHERE. In ON it only decides which marks may match; in WHERE it would throw away the NULL rows again.

Try it: move AND m.subject_id = 1 into a WHERE line and see Rohan disappear.

📋 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);
🗄️ SQL — you can edit this query
Result
┌───────────────┬──────────────────┐
│ teacher       │ subject          │
├───────────────┼──────────────────┤
│ Mr. Gupta     │ English          │
│ Mr. Rao       │ Physics          │
│ Mrs. Kulkarni │ Maths            │
│ Mrs. Nair     │ NULL             │
│ Ms. Fernandes │ Computer Science │
└───────────────┴──────────────────┘
(5 rows)

┌─────────────┬───────┐
│ name        │ maths │
├─────────────┼───────┤
│ Riya Sharma │ 92    │
│ Aman Verma  │ 75    │
│ Sara Khan   │ 81    │
│ Dev Patil   │ 64    │
│ Meera Iyer  │ 95    │
│ Kabir Singh │ 55    │
│ Anaya Joshi │ 73    │
│ Rohan Das   │ NULL  │
└─────────────┴───────┘
(8 rows)

How do you find rows with no match in SQL?

Questions like "which students have no marks yet?" or "which subject has no teacher?" are very common. The pattern: LEFT JOIN, then keep the rows where the right side IS NULL.

Test a column that can never be NULL in a real match, such as the right table's key. Then NULL can only mean "no match".

Try it: find teachers with no subject by starting FROM teachers and LEFT JOINing subjects.

📋 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);
🗄️ SQL — you can edit this query
Result
┌───────────┐
│ name      │
├───────────┤
│ Rohan Das │
└───────────┘
(1 row)

┌─────────────────────────┐
│ subject_without_teacher │
├─────────────────────────┤
│ Chemistry               │
└─────────────────────────┘
(1 row)

Does SQLite support RIGHT JOIN and FULL OUTER JOIN?

Yes, since SQLite 3.39 (2022). RIGHT JOIN is LEFT JOIN seen from the other side: it keeps every row of the right table. FULL OUTER JOIN keeps every row of both tables.

Below, FULL OUTER JOIN shows the whole picture in one result: matched pairs, Mrs. Nair with no subject, and Chemistry with no teacher.

PostgreSQL has both. MySQL has RIGHT JOIN but no FULL OUTER JOIN; there people combine a LEFT and a RIGHT JOIN with UNION. Try it: change FULL OUTER JOIN to RIGHT JOIN.

📋 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);
🗄️ SQL — you can edit this query
Result
┌───────────────┬──────────────────┐
│ teacher       │ subject          │
├───────────────┼──────────────────┤
│ Mrs. Nair     │ NULL             │
│ Mrs. Kulkarni │ Maths            │
│ Mr. Rao       │ Physics          │
│ NULL          │ Chemistry        │
│ Ms. Fernandes │ Computer Science │
│ Mr. Gupta     │ English          │
└───────────────┴──────────────────┘
(6 rows)

💡 ORDER BY sub.id puts Mrs. Nair's row (subject NULL) first, because SQLite sorts NULL first.

How do you join three or more tables in SQL?

Add one JOIN at a time. Each JOIN links a new table to one already in the query. Here: marks → students, marks → subjects, subjects → teachers.

Pick the join type for each link. Every mark has a student and a subject, so INNER JOIN is safe there. Chemistry has no teacher, so teachers needs a LEFT JOIN, or Riya's Chemistry mark would vanish from her report card.

Try it: change the LEFT JOIN to JOIN and count Riya's 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);
🗄️ SQL — you can edit this query
Result
┌─────────────┬──────────────────┬───────┬───────────────┐
│ student     │ subject          │ marks │ teacher       │
├─────────────┼──────────────────┼───────┼───────────────┤
│ Riya Sharma │ Maths            │ 92    │ Mrs. Kulkarni │
│ Riya Sharma │ Physics          │ 88    │ Mr. Rao       │
│ Riya Sharma │ Chemistry        │ 79    │ NULL          │
│ Riya Sharma │ Computer Science │ 95    │ Ms. Fernandes │
│ Riya Sharma │ English          │ 84    │ Mr. Gupta     │
└─────────────┴──────────────────┴───────┴───────────────┘
(5 rows)

What are a self join and a CROSS JOIN in SQL?

A self join joins a table to itself, using two aliases as if there were two copies. The first query pairs students who joined on the same day. s1.id < s2.id stops a student pairing with herself and stops each pair showing twice.

CROSS JOIN pairs every row with every row: 3 class 11 students × 5 subjects = 15 pairs. With a LEFT JOIN to marks, it finds every mark that is missing, a handy check before report cards go out.

Try it: change class = 11 to class = 12 in the second query.

📋 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);
🗄️ SQL — you can edit this query
Result
┌─────────────┬────────────┬────────────┐
│ student_1   │ student_2  │ joined     │
├─────────────┼────────────┼────────────┤
│ Riya Sharma │ Sara Khan  │ 2019-06-10 │
│ Dev Patil   │ Meera Iyer │ 2021-06-14 │
│ Anaya Joshi │ Rohan Das  │ 2023-06-12 │
└─────────────┴────────────┴────────────┘
(3 rows)

┌─────────────┬─────────────────┐
│ name        │ missing_subject │
├─────────────┼─────────────────┤
│ Anaya Joshi │ Physics         │
│ Anaya Joshi │ Chemistry       │
│ Dev Patil   │ 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. Teacher 4 is Mr. Gupta (subject 5), teacher 5 is Mrs. Nair (subject NULL). What does this return?

📋 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 t.name, sub.name
FROM teachers AS t
JOIN subjects AS sub ON sub.id = t.subject_id
WHERE t.id >= 4
ORDER BY t.id;

Question 2. In class 11, only Meera has a Chemistry (subject 3) mark: 89. What does this return?

📋 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 s.name, m.marks
FROM students AS s
LEFT JOIN marks AS m ON m.student_id = s.id
WHERE m.subject_id = 3 AND s.class = 11
ORDER BY s.name;

Question 3. What does this return? (SQLite sorts NULL first.)

📋 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 t.name, sub.name
FROM teachers AS t
FULL OUTER JOIN subjects AS sub ON sub.id = t.subject_id
WHERE t.name IS NULL OR sub.name IS NULL
ORDER BY sub.name;

🧩 Your challenge

The accounts office wants a July 2026 fee list for EVERY student. Show name and july_status: 'Paid' if their July fee is paid, 'Due' if it is not paid, and 'No bill' if there is no July row at all. Sort by name. You should get 8 rows, starting: Aman Verma | Due Anaya Joshi | No bill Dev Patil | Due Kabir Singh | No bill and later Riya Sharma | Paid.

Expected result :
┌─────────────┬─────────────┐
│ name        │ july_status │
├─────────────┼─────────────┤
│ Aman Verma  │ Due         │
│ Anaya Joshi │ No bill     │
│ Dev Patil   │ Due         │
│ Kabir Singh │ No bill     │
│ Meera Iyer  │ No bill     │
│ Riya Sharma │ Paid        │
│ Rohan Das   │ No bill     │
│ Sara Khan   │ No bill     │
└─────────────┴─────────────┘
(8 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);
🗄️ SQL — you can edit this query

⚠️ Common mistakes (and how to fix them)

❌ Wrong

SELECT name
FROM teachers
JOIN subjects ON subjects.id = teachers.subject_id;

Error: ambiguous column name: name

✅ Right

SELECT teachers.name AS teacher, subjects.name AS subject
FROM teachers
JOIN subjects ON subjects.id = teachers.subject_id
ORDER BY teachers.id;

Both tables have a column called name, so SQL cannot tell which one you mean. Put the table name or alias in front: teachers.name.

❌ Wrong

SELECT s.name, sub.name AS subject
FROM students AS s
JOIN subjects AS sub
WHERE s.id = 7
ORDER BY sub.name;

Runs, but gives the wrong result: ┌─────────────┬──────────────────┐ │ name │ subject │ ├─────────────┼──────────────────┤ │ Anaya Joshi │ Chemistry │ │ Anaya Joshi │ Computer Science │ │ Anaya Joshi │ English │ │ Anaya Joshi │ Maths │ │ Anaya Joshi │ Physics │

✅ Right

SELECT s.name, sub.name AS subject
FROM students AS s
JOIN marks AS m ON m.student_id = s.id
JOIN subjects AS sub ON sub.id = m.subject_id
WHERE s.id = 7
ORDER BY sub.name;

A JOIN with no ON pairs Anaya with all 5 subjects, though she takes only 3. The link between students and subjects is the marks table.

❌ Wrong

SELECT s.name, m.subject_id, m.marks
FROM students AS s
JOIN marks AS m ON m.subject_id = s.id
WHERE s.id = 2
ORDER BY m.marks DESC;

Runs, but gives the wrong result: ┌────────────┬────────────┬───────┐ │ name │ subject_id │ marks │ ├────────────┼────────────┼───────┤ │ Aman Verma │ 2 │ 91 │ │ Aman Verma │ 2 │ 88 │ │ Aman Verma │ 2 │ 77 │ │ Aman Verma │ 2 │ 68 │ │ Aman Verma │ 2 │ 62 │

✅ Right

SELECT s.name, m.subject_id, m.marks
FROM students AS s
JOIN marks AS m ON m.student_id = s.id
WHERE s.id = 2
ORDER BY m.marks DESC;

The ON links the wrong columns: a student's id is compared with a subject id. It runs, but shows everyone's Physics marks under Aman's name. Match a foreign key to the key it points to.

📌 Remember

  • JOIN (INNER JOIN) keeps only matched pairs; unmatched rows vanish without warning.
  • LEFT JOIN keeps every row of the left table and fills missing matches with NULL.
  • LEFT JOIN … WHERE right.key IS NULL finds rows with no match.
  • Filters on the right table go in ON, or a LEFT JOIN quietly becomes an INNER JOIN.
  • SQLite and PostgreSQL support RIGHT and FULL OUTER JOIN; MySQL has no FULL OUTER JOIN.
  • Self joins use two aliases of one table; CROSS JOIN makes every possible pair.

Finished the quiz and the challenge?

Questions students ask

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN shows only rows that have a match in both tables. LEFT JOIN shows every row from the left table, with NULLs where the right table has no match.

Is JOIN the same as INNER JOIN?

Yes. JOIN on its own means INNER JOIN in SQLite, PostgreSQL and MySQL. Writing INNER makes it clearer for readers.

Why does my LEFT JOIN behave like an INNER JOIN?

Usually a WHERE condition tests a column of the right table, like WHERE m.subject_id = 3. Rows with NULLs fail it and are removed. Move that condition into the ON clause.

How many tables can you join in one SQL query?

Many more than you will need: SQLite allows up to 64 tables in one join. Most real queries join 2 to 5. Each join should follow a key-to-foreign-key link.