Skip to content
Advertisement

SQL query with multiple OR condition inside AND returns null or empty

I’m trying to get those members who are going to match my filter criteria, but I am passing multiple OR condition inside the round brackets, and every condition which is inside the round brackets is, AND with another round bracket; however, the query does not work, and it returns an empty table, but whensoever I run the query with INTERSECT statement it returns the members. Still, it has been taking 3 seconds on 1 million records, and it is a very cost-paying operation on the server-side, and it will increase if the records increase. Could someone help me why my first query is not working or if I use the second query, is there any way to attenuate the time?

My actual table enter image description here

First Query, which returns Empty table

enter image description here

Second Query, which returns the result in, which I am interested but taking too much time

enter image description here

Advertisement

Answer

You have conflicting conditions on each row. I’m pretty sure you want aggregation:

The WHERE checks that either condition match. The HAVING validates that both match for a given member.

Note that you should avoid double quotes for identifiers. They just make queries harder to write and to read.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement