I have a table and sample data as below.
create table MyTable
(
Col1 NUMBER,
Col2 VARCHAR2(30)
)
MyTable
Col1 Col2
1 | Val1
2 | Val2
3 | Val3
4 | Val4
Below is the query which is already written and deployed to the application by some one else.
SELECT Col2
FROM MyTable A WHERE Col1 IN (2,3,4)
AND NOT EXISTS
(SELECT 1
FROM MyTable B
WHERE B.Col1 <> A.Col1)
How does <> compare multiple values in this case? Does it just compare with value 2? Or randomly compares with any value amoung 2,3 or 4?
The values are compare one by one.
If you have the sample data:
Then:
Will return 3 rows:
For your full query:
Then for each of the rows it will check that a row does
NOT EXISTS
in theMyTable
table whereB.Col1 <> A.Col1
. In your case, there are 3 rows that exist in the sub-query for each of the matched rows in the main query. You can see this with the query:Which outputs:
Given that there is always (more than) one row that exists then the
NOT EXISTS
condition will exclude every row and your result set will be empty.db<>fiddle here