Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do SQL EXISTS statements work?

Tags:

sql

I'm trying to learn SQL and am having a hard time understanding EXISTS statements. I came across this quote about "exists" and don't understand something:

Using the exists operator, your subquery can return zero, one, or many rows, and the condition simply checks whether the subquery returned any rows. If you look at the select clause of the subquery, you will see that it consists of a single literal (1); since the condition in the containing query only needs to know how many rows have been returned, the actual data the subquery returned is irrelevant.

What I don't understand is how does the outer query know which row the subquery is checking? For example:

SELECT *   FROM suppliers  WHERE EXISTS (select *                  from orders                 where suppliers.supplier_id = orders.supplier_id); 

I understand that if the id from the supplier and orders table match, the subquery will return true and all the columns from the matching row in the suppliers' table will be outputted. What I don't get is how the subquery communicates which specific row (lets say the row with supplier id 25) should be printed if only a true or false is being returned.

It appears to me that there is no relationship between the outer query and the subquery.

like image 269
Dan Avatar asked May 01 '11 06:05

Dan


People also ask

How does exists clause work in SQL?

The EXISTS condition in SQL is used to check whether the result of a correlated nested query is empty (contains no tuples) or not. The result of EXISTS is a boolean value True or False. It can be used in a SELECT, UPDATE, INSERT or DELETE statement.

How does a subquery with exists work?

The EXISTS checks the existence of a result of a Subquery. The EXISTS subquery tests whether a subquery fetches at least one row. When no data is returned then this operator returns 'FALSE'. A valid EXISTS subquery must contain an outer reference and it must be a correlated Subquery.

Why do we need exists in SQL?

The advantage of exists is that the actual count from the subquery is not needed: It only takes a single match to qualify. Unless the optimizer is really smart, materializing those counts is thing to slow everything down.

How does SQL not exists work?

SQL NOT EXISTS in a subquery In simple words, the subquery with NOT EXISTS checks every row from the outer query, returns TRUE or FALSE, and then sends the value to the outer query to use. In even simpler words, when you use SQL NOT EXISTS, the query returns all the rows that don't satisfy the EXISTS condition.


2 Answers

Think of it this way:

For 'each' row from Suppliers, check if there 'exists' a row in the Order table that meets the condition Suppliers.supplier_id (this comes from Outer query current 'row') = Orders.supplier_id. When you find the first matching row, stop right there - the WHERE EXISTS has been satisfied.

The magic link between the outer query and the subquery lies in the fact that Supplier_id gets passed from the outer query to the subquery for each row evaluated.

Or, to put it another way, the subquery is executed for each table row of the outer query.

It is NOT like the subquery is executed on the whole and gets the 'true/false' and then tries to match this 'true/false' condition with outer query.

like image 52
sojin Avatar answered Nov 11 '22 19:11

sojin


It appears to me that there is no relationship between the outer query and the subquery.

What do you think the WHERE clause inside the EXISTS example is doing? How do you come to that conclusion when the SUPPLIERS reference isn't in the FROM or JOIN clauses within the EXISTS clause?

EXISTS valuates for TRUE/FALSE, and exits as TRUE on the first match of the criteria -- this is why it can be faster than IN. Also be aware that the SELECT clause in an EXISTS is ignored - IE:

SELECT s.*   FROM SUPPLIERS s  WHERE EXISTS (SELECT 1/0                  FROM ORDERS o                 WHERE o.supplier_id = s.supplier_id) 

...should hit a division by zero error, but it won't. The WHERE clause is the most important piece of an EXISTS clause.

Also be aware that a JOIN is not a direct replacement for EXISTS, because there will be duplicate parent records if there's more than one child record associated to the parent.

like image 24
OMG Ponies Avatar answered Nov 11 '22 19:11

OMG Ponies