Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have read that the exists operator doesn't return the data instead it returns the value true and false.i.e if condition in exist query is true or false. Also exist doesn't check row by row data .As soon as it found that data exist it returns true and then
first query will run return data accordingly


however this was not the case .

when dealing with northwind database
when I was running following query it returned 91 rows

select customerid,companyname from customers

but when I used exist operator in following query it return only 89 rows

select customerid,companyname from customers cu where exists (select orderid from orders o where o.customerid=cu.customerid)


which means exist not only checks that particular set of data exist or not .it also returns all the cu.customer id which is equal to o.customerid


please Guide me regarding Exists operator
that what value it returns
Posted

You should avoid using this operator because the inner SQL is, by definition, run once for every outer row that is returned. It's expensive. I don't know what books you're reading, but the way you describe it, it would be utterly useless. It's being run once for every row returned, and it's only going to return customers who have an order.
 
Share this answer
 
You've got the correct result, this is an equivalent:

select distinct customers.customerid, companyname from customers inner join orders on orders.customerid=customers.customerid

You'll find the query executed without DISTINCT will return 830 rows

--- There are 91 customers.

So -

select customers.customerid, companyname from customers where customers.customerid NOT IN (select customerid from orders)

shows us there are two customers that have not placed an order. They don't EXIST in the order table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900