Can someone explain this query

Go To StackoverFlow.com

1

here is the query

SELECT * FROM customers  
WHERE 
     NOT EXISTS 
     (     
          SELECT 1 FROM brochure_requests     
          WHERE  brochure_requests.first_name = customers.customer_first_name AND    
          brochure_requests.last_name = customers.customer_last_name
     )

This query works just fine but I am not sure why it works. In the NOT EXISTS part SELECT 1 what is the 1 for. When I ran this query

select 1 from test2

Here were the results:

1
-----
1
1
1
1
1
1
1
1
1
1
1
..

How does the not exists query work?

2012-04-05 16:11
by Luke101


3

The compiler is smart enough to ignore the actual SELECT in an EXISTS. So, basically, if it WOULD return rows because the filters match, that is all it cares about...the SELECT portion of the EXISTS never executes. It only uses the EXISTS clauses for evaluation purposes

I had this misconception for quite some time since you will see this SELECT 1 a lot. But, I have seen 42, *, etc....It never actually cares about the result, only that there would be one :). The key to keep in mind that SQL is a compiled language, so it will optimize this appropriately.

You could put a 1/0 and it will not throw a divide-by-zero exception...thus further proving that the result set is not evaluated. This is shown in this SQLFiddle

Code from Fiddle:

CREATE TABLE test (i int)
CREATE TABLE test2 (i int)

INSERT INTO test VALUES (1)
INSERT INTO test2 VALUES (1)

SELECT i
FROM test
WHERE EXISTS
(
  SELECT 1/0
  FROM test2
  WHERE test2.i = test.i
)

And finally, more to your point, the NOT simply negates an EXISTS, saying to IGNORE any rows that match

2012-04-05 16:14
by Justin Pihony
Wow..thanks for this explainatio - Luke101 2012-04-05 17:04


1

The subquery is a correlated subquery joining between the customers and brochure_requests tables on the selected fields.

The EXISTS clause is simply a predicate that will only return the matching rows (and the NOT negates that).

2012-04-05 16:15
by Oded


0

The query :

select 1 from test2

shows you the value 1 as the value for all the records in test2 table.

Every SELECT query must have at least one column. I think that's why an unnamed column, which has the value 1, is used here.

The sub-query gives you the rows of the related Customers from the table brochure_requests.

NOT EXISTS causes the main query to return all the rows from the Customers table, which are not in the table brochure_requests.

2012-04-05 16:25
by Mitesh Budhabhatti


0

The relational operator in question is known as 'antijoin' (alternatively 'not match' or 'semi difference'). In natural language: customers who do not match brochure_requests using the common attributes first_name and last_name.

A closely related operator is relational difference (alternatively 'minus' or 'except') e.g. in SQL

SELECT customer_last_name, customer_first_name
  FROM customers  
EXCEPT
SELECT last_name, first_name  
  FROM brochure_requests;
2012-04-10 10:37
by onedaywhen


-1

It's simple check, if row exists it will return 1, need only to check if it exists or not, nothing more.

2012-04-05 16:14
by Elkan
The exists does not actually return anything - Justin Pihony 2012-04-05 16:18
If it's not return anything it's only means that no query was matched, your query condition is common "if exists condition", when we need only to check if the row exists or not, don't need to return something, only check exists or not, thats why there is an "1" in subquery - Elkan 2012-04-05 16:21
No, what I am saying is that the EXISTS subquery does not actually return 1. It merely is used as a means of evaluation. See my answer's fiddle for proo - Justin Pihony 2012-04-05 16:23
oh... EXISTS returns true if the row exists in the resultset, in your condition. In your case NOT EXISTS returns true when subquery returns resultset of rows that matched subquery condtion (in subquery 1 needs only to insert "1" in the resultset that means that row exists, nothing special) - Elkan 2012-04-05 16:29
If the filters match, then EXISTS returns true...again, no resultset is really created within the subquer - Justin Pihony 2012-04-05 16:34
Yes, finally u get it : - Elkan 2012-04-05 16:48
Ummm, I am not the question asker...I was trying to explain why your description is technically incorrect...I am confused on whether you really understood my points or not.... - Justin Pihony 2012-04-05 16:51
Hm, let's get to the final stage of the discussion, so i mean: IF there is a row in subquery the NOT EXISTS part will return false, true in another way, and yes, the resultset not really created by the subquery(sorry not sure about this), so in this case am i right? Didn't noticed that u'r not the question asker, sorry about that - Elkan 2012-04-05 16:56
Yes! So, basically your answer should be more like: .. if the EXISTS evaluation matches all criteria, it returns true .. Or something like that...since it will never actually return - Justin Pihony 2012-04-05 16:59
Ok, u'r right, i will be more accurate in my future answers : - Elkan 2012-04-05 17:01


-1

if customer requested a brochure, subquery returns 1 for this customer. and this customer not be added to return resultset. bcouse of NOT EXISTS clause.

2012-04-05 16:16
by hkutluay
the subquery does not actually return anythin - Justin Pihony 2012-04-05 16:17


-1

Note: I don't know Oracle, and am not especially expert in SQL.

However, SELECT 1 from simply returns a 1 for every row matching the from clause. So the inner select can find a brochure_requests row whose name fields match those of the customer row currently being considered, it will produce a 1 result and fail the NOT EXISTS.

Hence the query selects all customers who do not have a brochure_request matching their name.

2012-04-05 16:16
by Chowlett
The thing to note is that the SELECT portion is never evaluated, so it will not actually produce a - Justin Pihony 2012-04-05 16:30


-2

For each row of the table Customers, the query returns the rows when the sub-query. NOT EXISTS returns no row.

If the sub-query in NOT EXISTS returns rows, then the rows of the table Customers are not returned.

2012-04-05 16:17
by BlueRat
The subquery does not actually return anything. It merely is used for evaluation purpose - Justin Pihony 2012-04-05 16:45
Ads