0
0
PostgreSQLquery~5 mins

SIMILAR TO for regex-lite matching in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SIMILAR TO operator do in PostgreSQL?
It checks if a string matches a pattern using a simplified form of regular expressions, combining SQL LIKE and regex features.
Click to reveal answer
intermediate
How is SIMILAR TO different from LIKE in PostgreSQL?
LIKE uses simple wildcard patterns (% and _), while SIMILAR TO supports more complex patterns like alternation (|) and grouping with parentheses.
Click to reveal answer
beginner
Write a SIMILAR TO pattern to match strings that start with 'cat' or 'dog'.
Use '(cat|dog)%' inside SIMILAR TO. For example: WHERE animal SIMILAR TO '(cat|dog)%' matches strings starting with 'cat' or 'dog'.
Click to reveal answer
beginner
What symbols are used in SIMILAR TO for alternation and grouping?
Alternation uses the pipe symbol |, and grouping uses parentheses ( ).
Click to reveal answer
intermediate
Can SIMILAR TO patterns use quantifiers like + or *?
No, SIMILAR TO supports only a limited set of regex features. Quantifiers like + or * are not supported; use % and _ for repetition instead.
Click to reveal answer
Which operator in PostgreSQL allows pattern matching with a simplified regex syntax?
ABETWEEN
BLIKE
CILIKE
DSIMILAR TO
What symbol does SIMILAR TO use for alternation (choice between patterns)?
A%
B*
C|
D_
Which of these patterns matches strings starting with 'cat' or 'dog' using SIMILAR TO?
A'(cat|dog)%'
B'cat|dog%'
C'cat%|dog%'
D'cat|dog'
Can you use + quantifier in SIMILAR TO patterns?
AYes, always
BNo, never
COnly with escape characters
DOnly in PostgreSQL 14+
Which wildcard in SIMILAR TO matches any sequence of characters?
A%
B+
C*
D_
Explain how the SIMILAR TO operator works in PostgreSQL and how it differs from LIKE.
Think about how <code>SIMILAR TO</code> combines SQL and regex features.
You got /4 concepts.
    Write an example SQL query using SIMILAR TO to find rows where a column starts with 'red' or 'blue'.
    Use <code>WHERE column SIMILAR TO '(red|blue)%'</code>.
    You got /3 concepts.