If you get rid of the IF
prefix, IN()
and EXISTS()
do work that way.
First, the version using IN()
is generally a Bad Idea™ - performance-wise, you are much better off using a JOIN or a derived table rather than a subquery in the WHERE clause.
-- instead of:
SELECT * FROM tbl1 WHERE tbl1.v_short IN (SELECT tbl2.sname FROM tbl2)
-- you would want something like:
SELECT tbl1.* FROM tbl1 JOIN tbl2 ON (tbl2.sname = tbl1.v_short)
EXISTS()
on the other hand is very useful. It's optimized to be identical to an INNER JOIN, while NOT EXISTS()
is identical to a LEFT JOIN with an IS NULL check. The [NOT] EXISTS()
version is just easier to read as though it were English. Note that there is no point in passing a column list to the SELECT within an EXISTS - it is not used, it is optimized away as a SELECT * every time.
-- easy to read:
SELECT * FROM tbl1 WHERE EXISTS (SELECT * FROM tbl2 WHERE tbl2.sname = tbl1.v_short)
-- same thing, just more SQL-like than English-like:
SELECT tbl1.* FROM tbl1 JOIN tbl2 ON (tbl2.sname = tbl1.v_short)