Oracle Not exists inside not exists
-
23-12-2019 - |
Question
Guys when I run this query:
SELECT * FROM big_table big
WHERE sum_number = 1
AND NOT EXISTS ( SELECT 1
FROM smal_table smal
WHERE other_number = big.other_number
)
I get 4 resuslts, but when I run this one
SELECT * FROM big_table big
WHERE sum_number = 1
AND NOT EXISTS ( SELECT 1
FROM smal_table smal
WHERE other_number = big.other_number
AND NOT EXISTS ( SELECT 1
FROM and_another_table another
WHERE and_another_number = big.and_another_number
)
)
I get all lines from big_table. Does anyone have the slightest idea why this happens? I want to do a query that shows me all lines from big_table that does not exist in the smal_table but if it exists in the small table I only want the lines that are not in the and_another_table Can anyone help me please?
EDIT: I want the lines from the big_table that are not in small_table plus the lines from big_table that are on the small_table but not in the and_another_table
Solution
I think the logic is that you want the rows from the big table that are not in either the small table or the another table. If so, this should do what you want:
SELECT *
FROM big_table big
WHERE sum_number = 1 AND
(NOT EXISTS (SELECT 1
FROM small_table smal
WHERE other_number = big.other_number
) OR
NOT EXISTS (SELECT 1
FROM and_another_table another
WHERE and_another_number = big.and_another_number
)
);
EDIT:
Based on the comment below, the where
clause would be:
WHERE sum_number = 1 AND
(NOT EXISTS (SELECT 1
FROM small_table smal
WHERE other_number = big.other_number
) OR
EXISTS (SELECT 1
FROM and_another_table another
WHERE and_another_number = big.and_another_number
)
);