Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Real-World Join Scenarios | Handling NULLs, Performance, and Real-World Join Scenarios
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL Joins in Depth

bookReal-World Join Scenarios

Imagine you are tasked with generating a comprehensive report for a school's administration. The goal is to list all students, the classes they belong to, their assigned teachers, and grades for each subject. This type of report helps identify which students are excelling, which classes might need additional support, and where there are gaps in student assignments or grading.

1234567891011121314
SELECT s.student_id, s.name AS student_name, c.class_name, t.name AS teacher_name, g.subject, g.score FROM students s LEFT JOIN classes c ON s.class_id = c.class_id LEFT JOIN teachers t ON c.teacher_id = t.teacher_id LEFT JOIN grades g ON s.student_id = g.student_id ORDER BY s.student_id, g.subject;
copy

This query uses multiple LEFT JOINs to bring together information from the students, classes, teachers, and grades tables. By starting with the students table and joining to classes, you ensure every student appears in the report, even if they are not assigned to a class (class_id is NULL). The subsequent join to teachers connects each class to its teacher, but if a class has no teacher, the teacher_name will be NULL. Finally, joining to grades brings in each student's subject and score, but if a student has no grades, those columns will also be NULL. This approach provides a full picture: you can spot students without classes or grades, classes without teachers, and see detailed grade distributions across all subjects. Such a report enables actionable insights, like identifying unassigned students or tracking academic performance by class and teacher.

1234567891011121314
SELECT s.student_id, s.name AS student_name, c.class_name, g.subject, g.score FROM students s LEFT JOIN classes c ON s.class_id = c.class_id LEFT JOIN grades g ON s.student_id = g.student_id WHERE c.class_id IS NULL OR g.grade_id IS NULL ORDER BY s.student_id;
copy

1. Which join type is best for finding students without grades?

2. How can you combine multiple join types in a single query?

3. Fill in the blanks to complete the query that joins students, classes, and teachers to show each student's name, class, and teacher:

question mark

Which join type is best for finding students without grades?

Select the correct answer

question mark

How can you combine multiple join types in a single query?

Select the correct answer

question-icon

Fill in the blanks to complete the query that joins students, classes, and teachers to show each student's name, class, and teacher:

SELECT s.name AS student_name, c.class_name, t.name AS teacher_name FROM students s JOIN classes c ON s.class_id = c.class_id JOIN teachers t ON c.teacher_id = t.teacher_id;
student_name | class_name | teacher_name
-------------+--------------+----------------
Emily Brown | Mathematics | Alice Johnson
David Green | Mathematics | Alice Johnson
Sophia White | Science | Bob Smith
Michael Black| NULL | NULL
Olivia Blue | History | NULL
James Red | NULL | NULL
Lily Yellow | Art | Carol Lee

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookReal-World Join Scenarios

Свайпніть щоб показати меню

Imagine you are tasked with generating a comprehensive report for a school's administration. The goal is to list all students, the classes they belong to, their assigned teachers, and grades for each subject. This type of report helps identify which students are excelling, which classes might need additional support, and where there are gaps in student assignments or grading.

1234567891011121314
SELECT s.student_id, s.name AS student_name, c.class_name, t.name AS teacher_name, g.subject, g.score FROM students s LEFT JOIN classes c ON s.class_id = c.class_id LEFT JOIN teachers t ON c.teacher_id = t.teacher_id LEFT JOIN grades g ON s.student_id = g.student_id ORDER BY s.student_id, g.subject;
copy

This query uses multiple LEFT JOINs to bring together information from the students, classes, teachers, and grades tables. By starting with the students table and joining to classes, you ensure every student appears in the report, even if they are not assigned to a class (class_id is NULL). The subsequent join to teachers connects each class to its teacher, but if a class has no teacher, the teacher_name will be NULL. Finally, joining to grades brings in each student's subject and score, but if a student has no grades, those columns will also be NULL. This approach provides a full picture: you can spot students without classes or grades, classes without teachers, and see detailed grade distributions across all subjects. Such a report enables actionable insights, like identifying unassigned students or tracking academic performance by class and teacher.

1234567891011121314
SELECT s.student_id, s.name AS student_name, c.class_name, g.subject, g.score FROM students s LEFT JOIN classes c ON s.class_id = c.class_id LEFT JOIN grades g ON s.student_id = g.student_id WHERE c.class_id IS NULL OR g.grade_id IS NULL ORDER BY s.student_id;
copy

1. Which join type is best for finding students without grades?

2. How can you combine multiple join types in a single query?

3. Fill in the blanks to complete the query that joins students, classes, and teachers to show each student's name, class, and teacher:

question mark

Which join type is best for finding students without grades?

Select the correct answer

question mark

How can you combine multiple join types in a single query?

Select the correct answer

question-icon

Fill in the blanks to complete the query that joins students, classes, and teachers to show each student's name, class, and teacher:

SELECT s.name AS student_name, c.class_name, t.name AS teacher_name FROM students s JOIN classes c ON s.class_id = c.class_id JOIN teachers t ON c.teacher_id = t.teacher_id;
student_name | class_name | teacher_name
-------------+--------------+----------------
Emily Brown | Mathematics | Alice Johnson
David Green | Mathematics | Alice Johnson
Sophia White | Science | Bob Smith
Michael Black| NULL | NULL
Olivia Blue | History | NULL
James Red | NULL | NULL
Lily Yellow | Art | Carol Lee

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 5
some-alt