Search code examples
selectforeign-keysparent

Why can't I SELECT a parent field that doesn't have a child?


(rest_branches) is the table of restaurants.

(phone_numbers) is another table which contains the restaurants phone_numbers, and it has a field called (branch_id) which references the restaurant id.

When I try:

SELECT * 
FROM rest_branches
NATURAL JOIN phone_numbers

I only get the restaurants which have a phone_number. What should I do to get all restaurants even if they don't have a phone number?


Solution

  • Because that's how INNER/NATURAL joins work. If you wanted to get all restaurants, use a LEFT JOIN instead and specify the join condition.