Bird
0
0

How can you modify this query to find rows where description contains either 'apple' or 'orange' but not 'banana' using the @@ operator?

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

How can you modify this query to find rows where description contains either 'apple' or 'orange' but not 'banana' using the @@ operator?


SELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('apple | orange');
ASELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('(apple | orange) & !banana');
BSELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('apple | orange | !banana');
CSELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('apple & orange & !banana');
DSELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('apple | orange & !banana');
Step-by-Step Solution
Solution:
  1. Step 1: Understand logical operators in tsquery

    Use parentheses to group OR conditions and AND with NOT for exclusion.
  2. Step 2: Apply correct grouping

    Expression (apple | orange) & !banana means apple or orange but not banana.
  3. Final Answer:

    SELECT * FROM products WHERE to_tsvector(description) @@ to_tsquery('(apple | orange) & !banana'); -> Option A
  4. Quick Check:

    Use parentheses for OR group with AND NOT = D [OK]
Quick Trick: Group OR terms with () before AND NOT in tsquery [OK]
Common Mistakes:
  • Missing parentheses causing wrong logic
  • Using & instead of | for OR terms
  • Placing !banana incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes