Bird
0
0

Given the table products(name TEXT) and the pg_trgm extension enabled, what does the query below return?

medium📝 query result Q4 of 15
PostgreSQL - Advanced Features
Given the table products(name TEXT) and the pg_trgm extension enabled, what does the query below return?
SELECT name FROM products WHERE name % 'apple';
ARows where 'name' starts with 'apple'
BRows where 'name' is similar to 'apple' based on trigram similarity
CRows where 'name' contains the substring 'apple'
DRows where 'name' exactly equals 'apple'
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operator % in pg_trgm

    The % operator checks trigram similarity between strings, returning rows with similar text.
  2. Step 2: Differentiate from other string matches

    Exact match uses =, substring uses LIKE '%apple%', and starts with uses LIKE 'apple%'.
  3. Final Answer:

    Rows where 'name' is similar to 'apple' based on trigram similarity -> Option B
  4. Quick Check:

    pg_trgm % operator = trigram similarity [OK]
Quick Trick: % operator in pg_trgm means similarity match [OK]
Common Mistakes:
  • Confusing % with exact equality
  • Thinking % means substring search
  • Assuming % means prefix match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes