I have 2 sql queries that return different results.
Both queries use the same join operation but with different binding to the join parameter inside the IN clause.
on the first one,I reffer directly to the join parameter.
on the second I use free context query.
I’m getting different results and I’d like to understand why.
-- this one returns 13 tuples
select c.companyname
from companies as c
join stocklist as s
using (companyid)
where s.price in((select MAX(s.price)),( select MIN(s.price)));
--this one returns two tuples. as it should
select companyname
from companies join stockslist
using (companyid)
where price in(
(select max(price) from stockslist),(select min(price) from stockslist)
);
You can think of
sas a named instance of thestockslisttable. It isn’t exactly true, but it helps to think of it that way.When you use a named table from the outer query inside of an inner query, it performs the inner query for each row of the outer table using the values from that particular row.
Your top query is essentially equivalent to: