Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Real-World Join Scenarios | Handling NULLs, Performance, and Real-World Join Scenarios
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

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how to interpret the results of the second query?

What are some ways to address students who appear without classes or grades?

How can I modify the queries to include additional student information, like contact details?

bookReal-World Join Scenarios

Scorri per mostrare il menu

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

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
some-alt