Here is the rest of Rudy's response. (See part one.)
To summarize, if you want at least some number of rows, use an inequality in the HAVING clause of the IN?(subquery). If you want at least one row, the HAVING clause is redundant, and you should use an EXISTS?(subquery):
select id , loc_numb , loc_prec from item_loc xx where id in ( select id from item_loc where loc_prec = 'S' group by id having count(*) = 1 ) and exists ( select 1 from item_loc where id = xx.id and loc_prec = 'P' )
Note that the EXISTS subquery is correlated. The EXISTS syntax gives the database optimizer a chance to improve performance, since the EXISTS subquery will be true as soon as the first row is found. As soon as the row is found, grouping is not necessary.
In summary, use EXISTS if you are looking for one or more rows. If you want exactly some number, including one, or at least some number where the number is two or more, then grouping is necessary, and you can't use EXISTS.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.