Bird
0
0

How would you write a to_tsquery to find documents containing 'apple' and either 'banana' or 'cherry'?

hard📝 Application Q9 of 15
PostgreSQL - Full-Text Search
How would you write a to_tsquery to find documents containing 'apple' and either 'banana' or 'cherry'?
A'apple & banana | cherry'
B'apple & (banana | cherry)'
C'(apple & banana) | cherry'
D'apple | banana & cherry'
Step-by-Step Solution
Solution:
  1. Step 1: Understand the search logic

    We want documents with apple AND (banana OR cherry).
  2. Step 2: Use parentheses to group OR terms

    Parentheses around (banana | cherry) ensure correct evaluation order.
  3. Step 3: Evaluate options

    'apple & (banana | cherry)' correctly expresses apple & (banana | cherry). Others misplace operators or grouping.
  4. Final Answer:

    'apple & (banana | cherry)' -> Option B
  5. Quick Check:

    Group OR terms with parentheses for correct logic [OK]
Quick Trick: Group OR terms with parentheses when combined with AND [OK]
Common Mistakes:
  • Ignoring parentheses causing wrong logic
  • Misplacing AND and OR operators
  • Assuming left-to-right evaluation without grouping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes