0
0
PostgreSQLquery~20 mins

to_tsquery for search terms in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
to_tsquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Basic to_tsquery search output
Given a table documents with a text column content, what rows will match the query to_tsquery('cat & dog')?
PostgreSQL
SELECT id, content FROM documents WHERE to_tsvector(content) @@ to_tsquery('cat & dog');
ARows where content contains 'cat' followed immediately by 'dog' with no words between
BRows where content contains either 'cat' or 'dog', but not necessarily both
CRows where content contains the exact phrase 'cat dog' in sequence
DRows where content contains both 'cat' and 'dog' anywhere in the text
Attempts:
2 left
💡 Hint
The & operator means AND in to_tsquery syntax.
query_result
intermediate
2:00remaining
Using OR operator in to_tsquery
What is the result of this query?
SELECT id FROM documents WHERE to_tsvector(content) @@ to_tsquery('cat | dog');
ARows containing either 'cat' or 'dog' or both
BRows containing both 'cat' and 'dog' only
CRows containing the exact phrase 'cat dog'
DRows containing neither 'cat' nor 'dog'
Attempts:
2 left
💡 Hint
The | operator means OR in to_tsquery syntax.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in to_tsquery
Which option contains a syntax error when used inside to_tsquery()?
A'cat & | dog'
B'cat | dog'
C'cat & dog'
D'cat <-> dog'
Attempts:
2 left
💡 Hint
Check operator placement and usage rules.
query_result
advanced
2:00remaining
Phrase search with <-> operator
What does this query return?
SELECT id FROM documents WHERE to_tsvector(content) @@ to_tsquery('cat <-> dog');
ARows where 'cat' and 'dog' appear separated by exactly one word
BRows where 'cat' is immediately followed by 'dog' in the text
CRows where 'cat' or 'dog' appear anywhere in the text
DRows where 'cat' and 'dog' appear anywhere in the text, order doesn't matter
Attempts:
2 left
💡 Hint
The <-> operator means words are adjacent in order.
🧠 Conceptual
expert
3:00remaining
Understanding lexeme normalization in to_tsquery
Which statement about to_tsquery lexeme normalization is true?
AIt ignores stop words but does not stem words
BIt searches for exact word matches without any normalization
CIt converts words to their root form (stemming) before searching
DIt only matches words at the start of the text
Attempts:
2 left
💡 Hint
Think about how PostgreSQL handles word variations in full text search.