Bird
0
0

How can you combine CASE in WHERE clause with AND to filter Orders where if priority is 'High', amount > 500 and status is 'Open', else amount <= 500 or status is 'Closed'?

hard📝 Application Q9 of 15
SQL - CASE Expressions
How can you combine CASE in WHERE clause with AND to filter Orders where if priority is 'High', amount > 500 and status is 'Open', else amount <= 500 or status is 'Closed'?
ASELECT * FROM Orders WHERE CASE WHEN priority = 'High' THEN amount > 500 ELSE amount <= 500 END AND CASE WHEN priority = 'High' THEN status = 'Open' ELSE status = 'Closed' END;
BSELECT * FROM Orders WHERE (CASE WHEN priority = 'High' THEN amount > 500 AND status = 'Open' ELSE amount <= 500 OR status = 'Closed' END);
CSELECT * FROM Orders WHERE CASE WHEN priority = 'High' THEN amount > 500 OR status = 'Open' ELSE amount <= 500 AND status = 'Closed' END;
DSELECT * FROM Orders WHERE CASE priority = 'High' THEN amount > 500 AND status = 'Open' ELSE amount <= 500 OR status = 'Closed' END;
Step-by-Step Solution
Solution:
  1. Step 1: Understand logical conditions

    For priority 'High', both amount > 500 AND status = 'Open' must be true.
  2. Step 2: Check SELECT * FROM Orders WHERE CASE WHEN priority = 'High' THEN amount > 500 ELSE amount <= 500 END AND CASE WHEN priority = 'High' THEN status = 'Open' ELSE status = 'Closed' END; structure

    SELECT * FROM Orders WHERE CASE WHEN priority = 'High' THEN amount > 500 ELSE amount <= 500 END AND CASE WHEN priority = 'High' THEN status = 'Open' ELSE status = 'Closed' END; uses two CASE expressions combined with AND, correctly applying conditions.
  3. Step 3: Identify errors in other options

    B and D misuse AND/OR inside single CASE; C swaps AND/OR incorrectly.
  4. Final Answer:

    SELECT * FROM Orders WHERE CASE WHEN priority = 'High' THEN amount > 500 ELSE amount <= 500 END AND CASE WHEN priority = 'High' THEN status = 'Open' ELSE status = 'Closed' END; -> Option A
  5. Quick Check:

    Use multiple CASE for complex AND/OR logic [OK]
Quick Trick: Use multiple CASE for combining AND/OR in WHERE [OK]
Common Mistakes:
  • Mixing AND and OR inside one CASE incorrectly
  • Omitting WHEN keyword
  • Using CASE without END

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes