The ~ operator checks if a string matches a regular expression pattern. It returns true if the pattern matches, false otherwise.
Click to reveal answer
beginner
How do you write a query to find rows where the column name contains only digits using ~?
Use WHERE name ~ '^\d+$'. This means the entire name must be digits from start (^) to end ($).
Click to reveal answer
intermediate
What is the difference between ~ and ~* operators in PostgreSQL?
~ is case-sensitive regex match, while ~* is case-insensitive regex match.
Click to reveal answer
intermediate
Write a PostgreSQL query using ~ to find emails ending with @example.com.
SELECT * FROM table WHERE email ~ '@example\.com$'; The \. escapes the dot, and $ means end of string.
Click to reveal answer
intermediate
Can the ~ operator be used to check for multiple patterns at once?
No, ~ matches one regex pattern at a time. To check multiple patterns, combine conditions with OR or use regex alternation like pattern1|pattern2.
Click to reveal answer
What does the PostgreSQL operator ~ do?
AConcatenates two strings
BPerforms a case-insensitive string comparison
CChecks if a string contains only letters
DMatches a string against a regular expression pattern
✗ Incorrect
The ~ operator matches a string against a regex pattern and returns true if it matches.
Which operator would you use for case-insensitive regex matching in PostgreSQL?
A~*
B~
C!~
DILIKE
✗ Incorrect
~* is the case-insensitive regex match operator in PostgreSQL.
What does the regex pattern ^\d+$ mean when used with ~?
AString contains at least one digit
BString contains only digits from start to end
CString ends with a digit
DString starts with a digit
✗ Incorrect
The pattern means the entire string (from ^ start to $ end) consists only of digits (\d+).
How do you escape a dot (.) in a regex pattern for PostgreSQL?
A\.
B.
C\d
D\*
✗ Incorrect
A dot means any character in regex, so to match a literal dot, you escape it with a backslash: \.
Can the ~ operator match multiple patterns separated by commas?
AYes, it matches all patterns automatically
BYes, but only if patterns are in quotes
CNo, it matches only one regex pattern at a time
DNo, it only matches numeric patterns
✗ Incorrect
~ matches one regex pattern at a time. To match multiple patterns, use alternation or multiple conditions.
Explain how the ~ operator works in PostgreSQL and give an example query.
Think about how you check if a string fits a pattern using regex.
You got /4 concepts.
Describe the difference between ~ and ~* operators and when to use each.
Consider if letter case matters in your pattern matching.
You got /3 concepts.
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
Step 1: Understand the ~ operator
The ~ operator in PostgreSQL is used to check if a string matches a regular expression pattern.
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.
Final Answer:
Checks if the column value matches the regular expression pattern -> Option C
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
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.
Step 2: Check the syntax of the ~ operator usage
The correct syntax is email ~ '@example\.com$' to find emails ending with that domain.
Final Answer:
SELECT * FROM users WHERE email ~ '@example\.com$'; -> Option B
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
Step 1: Understand the regex pattern ^A
The caret ^ means the string must start with 'A'.
Step 2: Apply pattern to each code
Codes starting with 'A' are 'A123' and 'A345'.
Final Answer:
A123, A345 -> Option A
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
Step 1: Check regex pattern syntax
The pattern * is invalid because the * quantifier requires a preceding atom (e.g., a character or group).
Step 2: Identify common error with special characters
Regex special symbols like *, +, ? need escape characters (e.g., \* in SQL string) when used literally.
Final Answer:
The regex pattern is missing escape characters for special symbols -> Option A
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
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.
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.
Final Answer:
SELECT * FROM orders WHERE order_id ~ '^[A-Za-z][0-9]{3}$'; -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.