Bird
Raised Fist0
PostgreSQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the PostgreSQL operator ~ do when used in a query like column_name ~ 'pattern'?
easy
A. Returns the length of the column value
B. Replaces the column value with the pattern
C. Checks if the column value matches the regular expression pattern
D. Counts the number of times the pattern appears in the column

Solution

  1. Step 1: Understand the ~ operator

    The ~ operator in PostgreSQL is used to check if a string matches a regular expression pattern.
  2. Step 2: Apply to the query context

    When used as column_name ~ 'pattern', it returns true if the column's value matches the pattern, otherwise false.
  3. Final Answer:

    Checks if the column value matches the regular expression pattern -> Option C
  4. Quick Check:

    ~ means match pattern [OK]
Hint: Remember: ~ means 'matches pattern' in PostgreSQL [OK]
Common Mistakes:
  • Thinking ~ replaces or modifies data
  • Confusing ~ with counting occurrences
  • Assuming ~ returns string length
2. Which of the following is the correct syntax to find rows where the column email contains the domain example.com using the ~ operator?
easy
A. SELECT * FROM users WHERE email ~ 'example.com';
B. SELECT * FROM users WHERE email ~ '@example\.com$';
C. SELECT * FROM users WHERE email LIKE '%example.com%';
D. SELECT * FROM users WHERE email = '~example.com';

Solution

  1. Step 1: Understand the pattern for matching domain

    To match emails ending with '@example.com', the regex should be @example\.com$ where \. escapes the literal dot and $ means end of string.
  2. Step 2: Check the syntax of the ~ operator usage

    The correct syntax is email ~ '@example\.com$' to find emails ending with that domain.
  3. Final Answer:

    SELECT * FROM users WHERE email ~ '@example\.com$'; -> Option B
  4. Quick Check:

    Regex match with ~ and end anchor [OK]
Hint: Use $ to anchor regex at string end with ~ operator [OK]
Common Mistakes:
  • Using LIKE instead of ~ for regex matching
  • Missing anchors like $ for exact domain match
  • Using = operator with regex pattern
3. Given the table products with a column code containing values: 'A123', 'B234', 'A345', 'C456', what will the query SELECT code FROM products WHERE code ~ '^A'; return?
medium
A. A123, A345
B. B234, C456
C. All codes
D. No rows

Solution

  1. Step 1: Understand the regex pattern ^A

    The caret ^ means the string must start with 'A'.
  2. Step 2: Apply pattern to each code

    Codes starting with 'A' are 'A123' and 'A345'.
  3. Final Answer:

    A123, A345 -> Option A
  4. Quick Check:

    ^ means start with 'A' [OK]
Hint: ^ anchors regex to start of string [OK]
Common Mistakes:
  • Confusing ^ with end anchor $
  • Expecting all rows to match
  • Ignoring regex anchors
4. You wrote the query SELECT * FROM users WHERE username ~ '*'; but it returns an error. What is the most likely cause?
medium
A. The regex pattern is missing escape characters for special symbols
B. The ~ operator requires the pattern to be enclosed in double quotes
C. The regex pattern is invalid because it lacks anchors
D. The pattern is correct but the column contains NULL values causing error

Solution

  1. Step 1: Check regex pattern syntax

    The pattern * is invalid because the * quantifier requires a preceding atom (e.g., a character or group).
  2. Step 2: Identify common error with special characters

    Regex special symbols like *, +, ? need escape characters (e.g., \* in SQL string) when used literally.
  3. Final Answer:

    The regex pattern is missing escape characters for special symbols -> Option A
  4. Quick Check:

    Escape special regex chars in SQL strings [OK]
Hint: Escape special regex characters properly in SQL strings [OK]
Common Mistakes:
  • Using single quotes incorrectly for regex
  • Assuming ~ needs double quotes
  • Ignoring NULL values causing errors
5. You want to select all rows from orders where the order_id starts with a letter followed by exactly three digits. Which query using the ~ operator is correct?
hard
A. SELECT * FROM orders WHERE order_id ~ '^\w{4}$';
B. SELECT * FROM orders WHERE order_id ~ '^[0-9]{3}[A-Za-z]$';
C. SELECT * FROM orders WHERE order_id ~ '[A-Za-z][0-9]{3}';
D. SELECT * FROM orders WHERE order_id ~ '^[A-Za-z][0-9]{3}$';

Solution

  1. Step 1: Understand the pattern requirements

    The order_id must start with a letter [A-Za-z] followed by exactly three digits [0-9]{3}, and nothing else.
  2. Step 2: Check regex anchors and pattern

    Using ^ and $ anchors ensures the entire string matches. '^[A-Za-z][0-9]{3}$' matches a letter then three digits exactly.
  3. Final Answer:

    SELECT * FROM orders WHERE order_id ~ '^[A-Za-z][0-9]{3}$'; -> Option D
  4. Quick Check:

    ^ and $ anchor full string match [OK]
Hint: Use ^ and $ to match entire string pattern [OK]
Common Mistakes:
  • Swapping letter and digit order
  • Missing anchors causing partial matches
  • Using \w which includes digits and letters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard