Bird
0
0

You want to retrieve rows from the messages table where the body column contains the exact phrase 'quick brown fox'. Which query correctly uses the @@ operator to perform this search?

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

You want to retrieve rows from the messages table where the body column contains the exact phrase 'quick brown fox'. Which query correctly uses the @@ operator to perform this search?

ASELECT * FROM messages WHERE body @@ to_tsquery('quick | brown | fox');
BSELECT * FROM messages WHERE body @@ to_tsquery('quick & brown & fox');
CSELECT * FROM messages WHERE body @@ to_tsquery('"quick brown fox"');
DSELECT * FROM messages WHERE body @@ to_tsquery('quick <-> brown <-> fox');
Step-by-Step Solution
Solution:
  1. Step 1: Understand phrase search

    To search for an exact phrase, use the <-> operator between words in to_tsquery.
  2. Step 2: Analyze options

    SELECT * FROM messages WHERE body @@ to_tsquery('quick <-> brown <-> fox'); uses quick <-> brown <-> fox, which matches the phrase in order.
  3. Step 3: Other options

    SELECT * FROM messages WHERE body @@ to_tsquery('quick & brown & fox'); uses AND (&) but does not enforce word order or adjacency. SELECT * FROM messages WHERE body @@ to_tsquery('"quick brown fox"'); uses quotes incorrectly (not supported in to_tsquery). SELECT * FROM messages WHERE body @@ to_tsquery('quick | brown | fox'); uses OR (|), which matches any word.
  4. Final Answer:

    SELECT * FROM messages WHERE body @@ to_tsquery('quick <-> brown <-> fox'); -> Option D
  5. Quick Check:

    Use <-> for phrase search in to_tsquery [OK]
Quick Trick: Use <-> operator for phrase search [OK]
Common Mistakes:
  • Using & instead of <-> for phrase matching
  • Trying to use quotes inside to_tsquery
  • Using | which matches any word, not phrase

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes