MiniGunnR MiniGunnR - 1 month ago 6
SQL Question

How to do SQL query based on 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 to 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')