table - Use of IN and EXISTS in SQL -
assuming 1 has 3 tables in relational database :
customer(id, name, city), product(id, name, price), orders(cust_id, prod_id, date)
my first question best way excecute query: "get customers ordered product". people propose query exists
as:
select * customer c exists (select cust_id orders o c.id=o.cust_id)
is above query equivalent (can written?) as:
select * customer exists (select cust_id orders o join customer c on c.id=o.cust_id)
what problem when use in
instead of exists
apart performance as:
select * customer customer.id in (select o.cust_id order o )
do 3 above queries return same records?
update: how exists evaluation works in second query (or first), considering checks if subquery returns true or false? "interpretation" of query i.e.?
select * customer c exists (true)
the first 2 queries different.
the first has correlated subquery , return want -- information customers have order.
the second has uncorrelated subquery. return either customers or no customers, depending on whether or not customers have placed order.
the third query alternative way of expressing want.
the possible issue can think of arise when cust_id
might have null values. in such case, first , third queries may not return same results.
Comments
Post a Comment