Q:

How to write SQL - check for A5 Or if not exist then A6 AND A7

I have a SQL code which prints message like particular attachment is missing.
For example, pseudo code is below :-
SELECT 'A1' AS MSG WHERE NOT EXISTS (SELECT ...)
UNION    
SELECT 'A2' AS MSG WHERE NOT EXISTS (SELECT ...)
UNION
SELECT 'A3' AS MSG WHERE NOT EXISTS (SELECT ...)
UNION
SELECT 'A4' AS MSG WHERE NOT EXISTS (SELECT ...)

All these messages I am storing it in a variable in "," appended format which I will be using further for printing. 
Now I want to union with one conditional message, check for A5 Or if not exist then A6 AND A7.
I tried coding the scenario but with no luck. How can I simply achieve this ?
Please help.
Thanks,

A:

So it's okay when either A5 or A6+A7 exist:
Okay = EXISTS(A5) OR ( EXISTS(A6) AND EXISTS(A7) )

This translates to:
Not okay = NOT EXISTS(A5) AND ( NOT EXISTS(A6) OR NOT EXISTS(A7) )

In the query:
UNION
SELECT 'A5+A6/A7' AS MSG WHERE NOT EXISTS (<A5 query>)
                         AND ( NOT EXISTS(<A6 query>) OR NOT EXISTS(<A7 query>) )

So you tell the DBMS what to check, but you cannot really tell it in what order to do it. The DBMS may decide to do the A6 test first, if it thinks this performs better than to start with the A5 check. But this is nothing we should worry about. Important to us is only to get the correct result. To find the best execution path is not our business.

