I had am maintaining an query that is as follows:
select field_1, field_2
from source_table
minus
select field_1, field_2
from source_table
where status_code in (3, 600);
When I looked at this query, I immediately thought, “That’s lame. Why not just use a ‘NOT IN’ and remove the MINUS business. So I re-wrote it as so:
select field_1, field_2
from source_table
where status_code not in (3, 600);
Just to double-check my sanity, I got counts of each query. To my surprise, the first query returned 789,089 records, and the second query returned 1,518,450 records!
I’ve looked at this from several angles but can’t figure out how these two queries are different. Can anyone explain what is going on, or why I am an idiot this morning?
These queries are indeed different.
field_1andfield2do not equate tostatus_code3 and 600.field_1could be ‘A’ andfield_2could be ‘B’, so you would be eliminating records from the first SELECT that look likeA, B. The original is probably the best way of achieving the correct result.Edit: To give you a better idea of what’s going on, you could get the same result, in a similar fashion to how you thought to write the query, by doing a subquery: