0
0
PostgreSQLquery~20 mins

@@ match operator in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Full Text Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Basic usage of @@ match operator
Given a table documents with a content column of type tsvector, what is the result of this query?

SELECT id FROM documents WHERE content @@ to_tsquery('cat & dog');

Assume the table has these rows:
1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
A[1, 3]
B[3, 4]
C[2, 4]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
The @@ operator checks if the tsvector matches the tsquery condition.
query_result
intermediate
2:00remaining
Using @@ with OR condition in tsquery
What rows will this query return?

SELECT id FROM documents WHERE content @@ to_tsquery('cat | elephant');

Given the same documents table as before with rows:
1: 'cat dog mouse'
2: 'cat mouse'
3: 'dog cat'
4: 'dog elephant'
A[1, 2, 4]
B[1, 2, 3]
C[2, 4]
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint
The | operator means OR in tsquery.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in tsquery expression
Which option contains a syntax error in the tsquery passed to the @@ operator?
PostgreSQL
SELECT id FROM documents WHERE content @@ to_tsquery(?);
A'cat | (dog & mouse)'
B'cat & dog'
C'cat & | dog'
D'cat <-> dog'
Attempts:
2 left
💡 Hint
Look for invalid operator placement in the tsquery string.
optimization
advanced
2:00remaining
Improving performance of @@ queries
Which option best improves performance of queries using the @@ operator on a large documents table with a content tsvector column?
AUse LIKE '%cat%' instead of @@
BCreate a GIN index on the content column
CAdd a B-tree index on the content column
DConvert content to text and index it
Attempts:
2 left
💡 Hint
Think about which index type supports full text search efficiently.
🧠 Conceptual
expert
3:00remaining
Understanding @@ operator behavior with phrase search
Consider this query:

SELECT id FROM documents WHERE content @@ phraseto_tsquery('cat dog');

How does the @@ operator behave differently here compared to using to_tsquery('cat & dog')?
AIt matches rows where 'cat' and 'dog' appear next to each other in order
BIt matches rows where 'cat' or 'dog' appear anywhere in any order
CIt matches rows where 'cat' and 'dog' appear anywhere but not necessarily adjacent
DIt matches rows only if 'cat' appears before 'dog' anywhere in the text
Attempts:
2 left
💡 Hint
phraseto_tsquery creates a phrase search condition.