MiniGunnR MiniGunnR - 1 month ago 8
SQL Question

How do I do an SQL query based on a foreign key field?

I have the following tables:

people:

id, name


parent:

id, people_id, name


I have tried the following:

SELECT * FROM people
LEFT JOIN parent ON people.id = parent.people_id
WHERE parent.name != 'Carol';


How do I find all the people whose parent's name is not Carol?

Answer

You can try below code

select people.name from people
inner join parent on people.id=parent.people_id
where parent.name not in ('Carol')