Bird
Raised Fist0
PostgreSQLquery~20 mins

tsvector and tsquery types in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Full-Text Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this tsquery match?
Given the following SQL query, what is the result of the match condition?
PostgreSQL
SELECT to_tsvector('english', 'The quick brown fox') @@ to_tsquery('english', 'quick & fox') AS match_result;
Atrue
Bfalse
CSyntax error
DNULL
Attempts:
2 left
💡 Hint
Check if both words 'quick' and 'fox' appear in the tsvector.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates a tsquery for phrase search?
Which SQL statement correctly creates a tsquery to search for the phrase 'data science'?
ASELECT to_tsquery('english', 'data <-> science');
BSELECT to_tsquery('english', 'data & science');
CSELECT to_tsquery('english', 'data | science');
DSELECT to_tsquery('english', 'data - science');
Attempts:
2 left
💡 Hint
Phrase search uses the <-> operator between words.
❓ optimization
advanced
2:00remaining
How to optimize full-text search on a large text column?
You have a large table with a text column 'content'. Which approach optimizes full-text search performance?
ACreate a B-tree index on content
BCreate a GIN index on to_tsvector('english', content)
CCreate a GIST index on content
DCreate a hash index on to_tsquery('english', content)
Attempts:
2 left
💡 Hint
Full-text search uses tsvector and GIN indexes for speed.
🔧 Debug
advanced
2:00remaining
Why does this tsquery return no matches?
Given the query below, why does it return no rows? SELECT * FROM articles WHERE to_tsvector('english', body) @@ to_tsquery('simple', 'cats & dogs');
AThe '@@' operator is invalid for tsvector and tsquery
BThe to_tsvector function is missing a language parameter
CThe query syntax is incorrect and causes a runtime error
DThe tsquery 'cats & dogs' does not match because 'cats' is stemmed to 'cat' in tsvector
Attempts:
2 left
💡 Hint
Check how stemming affects word forms in tsvector and tsquery.
🧠 Conceptual
expert
2:00remaining
What is the difference between plainto_tsquery and to_tsquery?
Choose the correct statement about plainto_tsquery and to_tsquery functions in PostgreSQL.
Aplainto_tsquery requires explicit operators; to_tsquery parses plain text automatically
BBoth functions behave identically and are interchangeable
Cplainto_tsquery parses plain text and automatically adds AND operators; to_tsquery requires explicit operators
Dplainto_tsquery returns a tsvector; to_tsquery returns a tsquery
Attempts:
2 left
💡 Hint
Think about how each function interprets input text.

Practice

(1/5)
1. What does the tsvector type represent in PostgreSQL full-text search?
easy
A. A table storing user data
B. A searchable document converted into lexemes
C. A function to rank search results
D. A query pattern to search text

Solution

  1. Step 1: Understand tsvector role

    tsvector stores processed text as lexemes for searching.
  2. Step 2: Differentiate from tsquery

    tsquery is the search pattern, not the stored text.
  3. Final Answer:

    A searchable document converted into lexemes -> Option B
  4. Quick Check:

    tsvector = searchable text [OK]
Hint: Remember: tsvector stores text, tsquery stores search pattern [OK]
Common Mistakes:
  • Confusing tsvector with tsquery
  • Thinking tsvector is a function
  • Assuming tsvector stores raw text
2. Which of the following is the correct syntax to check if a tsvector matches a tsquery in PostgreSQL?
easy
A. tsvector = tsquery
B. tsquery @@ tsvector
C. tsvector @@ tsquery
D. tsvector LIKE tsquery

Solution

  1. Step 1: Recall operator for matching

    The @@ operator tests if tsvector matches tsquery.
  2. Step 2: Confirm operand order

    The syntax is tsvector @@ tsquery, not reversed or using other operators.
  3. Final Answer:

    tsvector @@ tsquery -> Option C
  4. Quick Check:

    Match operator = @@ [OK]
Hint: Use tsvector @@ tsquery to test matches [OK]
Common Mistakes:
  • Reversing operands in @@ operator
  • Using = or LIKE instead of @@
  • Confusing tsquery and tsvector roles
3. Given the table documents with a column content_tsv of type tsvector, what will this query return?
SELECT id FROM documents WHERE content_tsv @@ to_tsquery('cat & dog');
medium
A. Rows where content contains both 'cat' and 'dog'
B. Rows where content contains either 'cat' or 'dog'
C. Rows where content contains the exact phrase 'cat dog'
D. Rows where content contains 'cat' but not 'dog'

Solution

  1. Step 1: Understand to_tsquery('cat & dog')

    The & operator means AND, so it searches for both words.
  2. Step 2: Apply @@ operator

    The query returns rows where content_tsv matches both 'cat' and 'dog'.
  3. Final Answer:

    Rows where content contains both 'cat' and 'dog' -> Option A
  4. Quick Check:

    AND operator & means both words [OK]
Hint: Use & in tsquery for AND search [OK]
Common Mistakes:
  • Thinking & means OR
  • Assuming phrase match instead of AND
  • Confusing tsquery syntax operators
4. Identify the error in this PostgreSQL query:
SELECT * FROM articles WHERE to_tsquery('dog | cat') @@ content_tsv;
medium
A. The operands of @@ are reversed; tsvector must be on the left
B. The operator | is invalid in tsquery
C. to_tsquery requires two arguments
D. content_tsv must be cast to text before matching

Solution

  1. Step 1: Check @@ operator usage

    The left operand must be tsvector, right operand tsquery.
  2. Step 2: Identify operand order in query

    The query has to_tsquery('dog | cat') on left (tsquery), which is incorrect.
  3. Final Answer:

    The operands of @@ are reversed; tsvector must be on the left -> Option A
  4. Quick Check:

    tsvector @@ tsquery order matters [OK]
Hint: Always put tsvector left, tsquery right in @@ [OK]
Common Mistakes:
  • Swapping operands in @@ operator
  • Misunderstanding tsquery syntax
  • Casting tsvector to text unnecessarily
5. You want to find rows in table notes where the content column contains the word 'apple' but not 'pie'. Which tsquery expression correctly achieves this?
SELECT * FROM notes WHERE to_tsvector(content) @@ to_tsquery( ? );
hard
A. 'apple ! pie'
B. 'apple | !pie'
C. 'apple & pie'
D. 'apple & !pie'

Solution

  1. Step 1: Understand tsquery operators

    & means AND, ! means NOT, | means OR.
  2. Step 2: Construct query for 'apple' AND NOT 'pie'

    The correct expression is 'apple & !pie' to include 'apple' but exclude 'pie'.
  3. Final Answer:

    'apple & !pie' -> Option D
  4. Quick Check:

    AND + NOT = & ! [OK]
Hint: Use & for AND and ! for NOT in tsquery [OK]
Common Mistakes:
  • Using | instead of & for AND
  • Omitting & between terms
  • Incorrect spacing or missing operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard