id | name
1 | alice
2 | bob
3 | charlie
student_id | subject | grade
1 | math | a
2 | math | b
4 | science | c
SELECT students.name, grades.subject, grades.grade
FROM students
INNER JOIN grades ON students.id = grades.student_id;
This query returns only the rows where there's a match in both tables.
Result:
name | subject | grade
--------|---------|------
alice | math | a
bob | math | b
Explanation: Only Alice and Bob have matching records in both tables.
SELECT students.name, grades.subject, grades.grade
FROM students
LEFT JOIN grades ON students.id = grades.student_id;
This query returns all rows from the left table (students) and matching rows from the right table (grades). If there's no match, NULL values are returned for the right table columns.
Result:
name | subject | grade
--------|---------|------
alice | math | a
bob | math | b
charlie | NULL | NULL
Explanation: Charlie is included even though there's no matching grade.
SELECT students.name, grades.subject, grades.grade
FROM students
RIGHT JOIN grades ON students.id = grades.student_id;
This query returns all rows from the right table (grades) and matching rows from the left table (students). If there's no match, NULL values are returned for the left table columns.
Result:
name | subject | grade
--------|---------|------
alice | math | a
bob | math | b
NULL | science | c
Explanation: The science grade for student_id 4 is included, even though there's no matching student.
SELECT students.name, grades.subject, grades.grade
FROM students
FULL JOIN grades ON students.id = grades.student_id;
This query returns all rows when there's a match in either table. If there's no match, NULL values are returned for the columns from the table without a match.
Result:
name | subject | grade
--------|---------|------
alice | math | a
bob | math | b
charlie | NULL | NULL
NULL | science | c
Explanation: This includes all students and all grades, even when there's no match between the tables.