SQL "Where not exists" (nested loops)

  • Thread starter whitehorsey
  • Start date
  • Tags
    Loops Sql
In summary: where h2.hobby = h1.hobby and h2.id = p.id))
  • #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.

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?
 
Technology news on Phys.org
  • #2
whitehorsey said:
I'm having a hard time understanding how nested loops work in SQL.

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?

You want to start your evaluation with the inner most subquery and work your way back. Kind of like mathematics where you have a situation like a(b(c*d)).Start with:
(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))

So here you have a derived table before the where not exists and a sub query afterwards.

Look at the sub query first.

exists is true when a subquery returns results. Not exists means its false when it returns results.

Now it's just a matter of walking this backwards till we evaluate the outermost statement.
 
Last edited:
  • #3
An unsolicited piece of advice: write Sql keywords in caps, variable and table names in lower case. Like this:

SELECT p.name
FROM person p
WHERE NOT EXISTS (...
 
  • #4
whitehorsey said:
I'm having a hard time understanding how nested loops work in SQL.

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?

Just to be clear. Are you trying to list the names of people who like every type of hobby in the Hobby table?

What are the column names in those two tables? Your Hobby table has a column named hobby. It's not a good idea to name columns in your table with the same exact name as your table. You should either rename the table HOBBIES or change the name of the column to HOBBY_NAME.

Also, I wouldn't expect the Hobby table IDs have anything to do with the Person table IDs (i.e., h2.id = p.id). Based on the way that you're trying to construct the query, I would expect the Hobby table to have a column named something like PERSON_ID (i.e., h2.person_id = p.id).
 
  • #5
whitehorsey said:
I'm having a hard time understanding how nested loops work in SQL.

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?

DO NOT THINK in the normal "NOT IN" way, I consider it as an "attribute" way

Think of it in this way.

1. For each person you try to find, his/her hobbies are listed in the innermost connected table, and this person needs to have another attribute which is he/she don't have all the hobbies in the hobbies table. That is the first NOT EXIST.

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)
means that pick hobbies out from the hobby table that does not belong to this person.

2. But we don't want this person.

3. So we used second NOT exist.

So, in short, Start with the innermost combined table in your mind. and You don't want those person whose hobbies (think as his/her attribute) does not match all the hobbies from the derived hobbies table..
 
Last edited:
  • #6
harborsparrow said:
An unsolicited piece of advice: write Sql keywords in caps, variable and table names in lower case. Like this:

SELECT p.name
FROM person p
WHERE NOT EXISTS (...
Thank you!

My previous company had the complete opposite standard:
select P.NAME
from PERSON P
where not exists(...
That irked me so badly.
 

FAQ: SQL "Where not exists" (nested loops)

1. What is "SQL "Where not exists" (nested loops)?

"SQL "Where not exists" (nested loops) is a SQL statement used to query data from a database table. It is used to retrieve data that does not exist in a specified table or subquery. This statement uses a nested loop algorithm to compare data between two tables and returns rows from the first table that do not have a match in the second table."

2. How does "SQL "Where not exists" (nested loops) work?

The SQL "Where not exists" (nested loops) statement works by first selecting data from the first table in the statement. It then iterates through each row and compares it to the data in the second table. If there is no match, the row from the first table is returned in the results. This process continues until all rows from the first table have been checked.

3. When should I use "SQL "Where not exists" (nested loops)?

SQL "Where not exists" (nested loops) should be used when you need to query data from a table that does not have a direct relationship with another table. It is useful when you want to exclude data that already exists in another table or when you need to find missing data in a specific table.

4. What are the advantages of using "SQL "Where not exists" (nested loops)?

One advantage of using "SQL "Where not exists" (nested loops) is that it allows for efficient querying of data from large tables. It also provides flexibility in querying data from multiple tables and can be used to find missing or duplicate data. Additionally, this statement is easy to understand and can be used in various database management systems.

5. Are there any limitations to using "SQL "Where not exists" (nested loops)?

One limitation of using "SQL "Where not exists" (nested loops) is that it can be slower compared to other join methods when dealing with large datasets. Additionally, it may not be suitable for complex queries that involve multiple subqueries or tables. It is also important to ensure that proper indexing is in place for optimal performance.

Similar threads

Replies
35
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
5
Views
818
Replies
3
Views
2K
Replies
11
Views
1K
Replies
12
Views
2K
Back
Top