I am working on an SQL problem for a class - "Provide a student/parent list. Formatted child name, parent name 1, parent name 2. If a child doesn’t have a parent, just display null."
The expected result should look like this
However, I have been unable to get the result to return "NULL" values, I am stuck only getting rows returned where there is no null
Utilizing this code
SELECT child.name AS Child, parent1.name AS Parent1, parent2.name AS Parent2
FROM Person child
JOIN Person parent1 ON child.parentID1 = parent1.personID
JOIN Person parent2 ON child.parentID2 = parent2.personID
ORDER BY child.name;
Any help in how to modify the SQL to include null values would be greatly appreciated


You can use a
LEFT JOINfor each parent, which will ensure that even if a child doesn't have a parent, they will still be included in the result with "NULL" values.enter image description here
The modified query should be something like this:
A similar template for the SQL Query:
You can read more about
LEFT JOIN