- #1
whitehorsey
- 192
- 0
I'm having a hard time understanding how nested loops work in SQL.
Find all the people who likes all the hobbies.
My logic:
In the first not exists it gets all the columns in hobby. The second not exists it gets all the hobbies but this time checks to see if the hobby are equal and the ids. But how does this all come together?
Find all the people who likes all the hobbies.
Code:
select p.name
from person p
where not exists (select *
from (select distinct h.hobby
from hobby h) as h1
where not exists (select *
from hobby as h2
where h2.hobby = h1.hobby and h2.id = p.id))
My logic:
In the first not exists it gets all the columns in hobby. The second not exists it gets all the hobbies but this time checks to see if the hobby are equal and the ids. But how does this all come together?