0
0
PostgreSQLquery~20 mins

Regular expression matching (~ operator) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find names starting with 'A' or 'a'
Given a table users with a column name, which query returns all users whose names start with the letter 'A' or 'a' using the regular expression match operator?
PostgreSQL
SELECT name FROM users WHERE name ~ '^[Aa]';
ASELECT name FROM users WHERE name ~* 'a';
BSELECT name FROM users WHERE name ~ '^a';
CSELECT name FROM users WHERE name ~ '^[Aa]';
DSELECT name FROM users WHERE name LIKE 'A%';
Attempts:
2 left
💡 Hint
Remember that ~ is case-sensitive and ^ means start of string.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in regex query
Which of the following queries will cause a syntax error due to incorrect regular expression syntax?
ASELECT * FROM logs WHERE message ~ '[0-9]{3}-[0-9]{2}-[0-9]{4}';
BSELECT * FROM logs WHERE message ~ '^[A-Za-z]+$';
CSELECT * FROM logs WHERE message ~ '^Error.*$';
DSELECT * FROM logs WHERE message ~ '[0-9]{3-[0-9]{2}-[0-9]{4}';
Attempts:
2 left
💡 Hint
Check for balanced brackets in the regex pattern.
optimization
advanced
2:00remaining
Optimize regex for case-insensitive matching
You want to find all rows where the column title contains the word 'data' regardless of case. Which query is the most efficient and correct way to do this using PostgreSQL regex operators?
ASELECT * FROM books WHERE title ~* '\mdata\M';
BSELECT * FROM books WHERE title ~ '\mdata\M';
CSELECT * FROM books WHERE title ILIKE '%data%';
DSELECT * FROM books WHERE LOWER(title) ~ 'data';
Attempts:
2 left
💡 Hint
~* is the case-insensitive regex match operator.
🔧 Debug
advanced
2:00remaining
Debug why regex query returns no rows
A user runs this query but gets no results: SELECT * FROM employees WHERE email ~ '^\w+@company\.com$'; The emails are stored in mixed case and look like 'John@Company.com'. Why might this query return no rows?
AThe regex is case-sensitive and emails might have uppercase letters.
BThe ~ operator does not support regex matching.
CThe regex pattern is missing anchors to match the whole string.
DThe backslash before the dot is not escaped properly in SQL string.
Attempts:
2 left
💡 Hint
The ~ operator is case-sensitive.
🧠 Conceptual
expert
2:00remaining
Understanding difference between ~ and ~* operators
Which statement best describes the difference between the PostgreSQL operators ~ and ~*?
A<code>~</code> matches regex anywhere in string; <code>~*</code> matches only at start.
B<code>~</code> performs case-sensitive regex matching; <code>~*</code> performs case-insensitive regex matching.
C<code>~</code> uses POSIX regex; <code>~*</code> uses Perl regex.
D<code>~</code> matches whole words only; <code>~*</code> matches partial words.
Attempts:
2 left
💡 Hint
Think about case sensitivity in regex matching.