Bird
0
0

You want to find all rows in messages where the body contains either 'error' or 'failure' but not both. Which to_tsquery expression with @@ operator will achieve this?

hard📝 Application Q15 of 15
PostgreSQL - Full-Text Search

You want to find all rows in messages where the body contains either 'error' or 'failure' but not both. Which to_tsquery expression with @@ operator will achieve this?

Abody @@ to_tsquery('(error | failure) & !(error & failure)')
Bbody @@ to_tsquery('error | failure & !error & !failure')
Cbody @@ to_tsquery('error <-> failure')
Dbody @@ to_tsquery('error | failure')
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want rows containing 'error' or 'failure' but NOT both. This is an exclusive OR (XOR) condition.
  2. Step 2: Analyze the options

    body @@ to_tsquery('(error | failure) & !(error & failure)') uses (error | failure) & !(error & failure), which means 'error or failure' AND NOT 'both error and failure'. This matches XOR logic.
  3. Step 3: Check other options

    body @@ to_tsquery('error | failure & !error & !failure') is invalid syntax. body @@ to_tsquery('error | failure') matches any with 'error' or 'failure' including both. body @@ to_tsquery('error <-> failure') looks for 'error' immediately followed by 'failure'.
  4. Final Answer:

    body @@ to_tsquery('(error | failure) & !(error & failure)') -> Option A
  5. Quick Check:

    XOR = (A OR B) AND NOT (A AND B) [OK]
Quick Trick: Use (A | B) & !(A & B) for XOR in to_tsquery [OK]
Common Mistakes:
  • Using simple OR instead of XOR
  • Incorrect syntax for NOT operator
  • Confusing proximity operator <-> with XOR

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes