Bird
Raised Fist0
PostgreSQLquery~20 mins

Pattern matching with LIKE and ILIKE in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pattern Matching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Case-sensitive pattern matching with LIKE
Given a table employees with a column name, what will be the output of this query?

SELECT name FROM employees WHERE name LIKE 'Jo%';

Assume the table contains names: 'John', 'john', 'Joanna', 'joey', 'Joseph'.
PostgreSQL
SELECT name FROM employees WHERE name LIKE 'Jo%';
AJohn, Joanna, Joseph
Bjohn, joey
CJohn, john, Joanna, joey, Joseph
DNo rows returned
Attempts:
2 left
💡 Hint
LIKE is case-sensitive in PostgreSQL.
❓ query_result
intermediate
2:00remaining
Case-insensitive pattern matching with ILIKE
Using the same employees table, what will this query return?

SELECT name FROM employees WHERE name ILIKE 'jo%';

Assume the same names as before.
PostgreSQL
SELECT name FROM employees WHERE name ILIKE 'jo%';
AJohn, Joanna, Joseph
Bjohn, joey
CNo rows returned
DJohn, john, Joanna, joey, Joseph
Attempts:
2 left
💡 Hint
ILIKE ignores case differences.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in pattern matching
Which option contains a syntax error in the pattern matching query?
ASELECT * FROM products WHERE code LIKE 'B%_2';
BSELECT * FROM products WHERE code LIKE 'C%_';
CSELECT * FROM products WHERE code LIKE 'D%_2' ESCAPE '\';
DSELECT * FROM products WHERE code LIKE 'A_1%';
Attempts:
2 left
💡 Hint
Look for unbalanced quotes in the SQL statements.
❓ optimization
advanced
2:00remaining
Optimizing pattern matching queries
Which query is the most efficient to find names starting with 'Ann' in a large users table?
ASELECT * FROM users WHERE name LIKE 'Ann%';
BSELECT * FROM users WHERE name ILIKE 'ann%';
CSELECT * FROM users WHERE LOWER(name) LIKE 'ann%';
DSELECT * FROM users WHERE name LIKE '%Ann%';
Attempts:
2 left
💡 Hint
Consider how indexes work with pattern matching.
🧠 Conceptual
expert
2:00remaining
Understanding ESCAPE clause in LIKE patterns
Which query correctly searches for names containing the literal underscore character '_' in a customers table?
ASELECT * FROM customers WHERE name LIKE '%[_]%';
BSELECT * FROM customers WHERE name LIKE '%\_%';
CSELECT * FROM customers WHERE name LIKE '%\_%' ESCAPE '\';
DSELECT * FROM customers WHERE name LIKE '%_%';
Attempts:
2 left
💡 Hint
Underscore is a wildcard for one character unless escaped.

Practice

(1/5)
1. Which of the following statements about LIKE and ILIKE in PostgreSQL is true?
easy
A. LIKE is case-sensitive, while ILIKE is case-insensitive.
B. LIKE ignores case, but ILIKE is case-sensitive.
C. LIKE and ILIKE both ignore case.
D. ILIKE is used only for numeric pattern matching.

Solution

  1. Step 1: Understand the difference between LIKE and ILIKE

    LIKE matches patterns considering case, so 'A' and 'a' are different. ILIKE ignores case differences.
  2. Step 2: Evaluate the options

    LIKE is case-sensitive, while ILIKE is case-insensitive correctly states the case sensitivity difference. The other options are incorrect based on PostgreSQL behavior.
  3. Final Answer:

    LIKE is case-sensitive, while ILIKE is case-insensitive. -> Option A
  4. Quick Check:

    LIKE case-sensitive = A [OK]
Hint: Remember: LIKE cares about case, ILIKE ignores it [OK]
Common Mistakes:
  • Confusing LIKE and ILIKE case sensitivity
  • Thinking ILIKE works in all SQL databases
  • Assuming LIKE ignores case by default
2. Which of the following is the correct syntax to find rows where the column name starts with 'Jo' using case-insensitive matching?
easy
A. SELECT * FROM users WHERE name LIKE 'Jo%';
B. SELECT * FROM users WHERE name LIKE '%Jo';
C. SELECT * FROM users WHERE name ILIKE 'Jo%';
D. SELECT * FROM users WHERE name ILIKE '%Jo';

Solution

  1. Step 1: Identify the pattern for names starting with 'Jo'

    The pattern should be 'Jo%' to match strings starting with 'Jo'.
  2. Step 2: Choose case-insensitive matching syntax

    Use ILIKE for case-insensitive matching in PostgreSQL. SELECT * FROM users WHERE name ILIKE 'Jo%'; uses ILIKE 'Jo%', which is correct.
  3. Final Answer:

    SELECT * FROM users WHERE name ILIKE 'Jo%'; -> Option C
  4. Quick Check:

    ILIKE + 'Jo%' = C [OK]
Hint: Use ILIKE with 'pattern%' to match start ignoring case [OK]
Common Mistakes:
  • Using LIKE instead of ILIKE for case-insensitive search
  • Using '%Jo' which matches end, not start
  • Forgetting the % wildcard
3. Given a table products with a column code, what will the following query return?

SELECT code FROM products WHERE code LIKE '_A%';

Assuming the code values are: '1A23', 'BA45', 'CA67', 'AA89', 'AB12'.
medium
A. AA89, AB12
B. 1A23, AA89
C. BA45, CA67, AB12
D. 1A23, BA45, CA67, AA89

Solution

  1. Step 1: Understand the pattern '_A%'

    The underscore (_) matches exactly one character, 'A' is literal, and % matches any sequence after. So the second character must be 'A'.
  2. Step 2: Check each code value

    '1A23' (second char 'A') matches. 'BA45' (second 'A') matches. 'CA67' (second 'A') matches. 'AA89' (second 'A') matches. 'AB12' (second 'B') does not match.
  3. Final Answer:

    1A23, BA45, CA67, AA89 -> Option D
  4. Quick Check:

    Second char 'A' = D [OK]
Hint: Underscore matches one char; check second char carefully [OK]
Common Mistakes:
  • Confusing _ with %
  • Ignoring position of characters in pattern
  • Missing some matching rows
4. You wrote this query to find all users whose email ends with '@example.com' ignoring case:

SELECT * FROM users WHERE email LIKE '%@example.com';

But it misses emails like 'User@Example.com'. What is the fix?
medium
A. Change LIKE to ILIKE: email ILIKE '%@example.com'
B. All of the above
C. Use UPPER function: UPPER(email) LIKE '%@EXAMPLE.COM'
D. Add LOWER function: LOWER(email) LIKE '%@example.com'

Solution

  1. Step 1: Identify why LIKE misses case differences

    LIKE is case-sensitive, so 'User@Example.com' does not match '%@example.com'.
  2. Step 2: Explore fixes for case-insensitive matching

    Change LIKE to ILIKE: email ILIKE '%@example.com' uses ILIKE which ignores case. Add LOWER function: LOWER(email) LIKE '%@example.com' converts email to lowercase before matching. Use UPPER function: UPPER(email) LIKE '%@EXAMPLE.COM' converts email to uppercase and matches uppercase pattern. All achieve case-insensitive matching.
  3. Step 3: Confirm all options work

    All three approaches fix the problem, so the correct answer is all of the above.
  4. Final Answer:

    All of the above -> Option B
  5. Quick Check:

    Case-insensitive fix = B [OK]
Hint: Use ILIKE or LOWER/UPPER for case-insensitive matching [OK]
Common Mistakes:
  • Using LIKE without case conversion
  • Forgetting to match case in pattern
  • Assuming LIKE is case-insensitive
5. You want to find all product codes in a table inventory where the code contains exactly three letters followed by any digits, ignoring case. Which query correctly uses pattern matching?
hard
A. SELECT * FROM inventory WHERE code ~* '^[a-zA-Z]{3}[0-9]+$';
B. SELECT * FROM inventory WHERE code ILIKE '___%';
C. SELECT * FROM inventory WHERE code LIKE '___%';
D. SELECT * FROM inventory WHERE code ILIKE '???%';

Solution

  1. Step 1: Understand the pattern requirements

    We need exactly three letters (any case) followed by digits, and the match should be case-insensitive.
  2. Step 2: Evaluate pattern matching options

    LIKE '___%' and ILIKE '___%' match any three characters followed by anything, but do not restrict to letters followed by digits. ILIKE '???%' uses invalid wildcard syntax (? is not supported in SQL LIKE/ILIKE). The ~* '^[a-zA-Z]{3}[0-9]+$' uses regex with case-insensitive flag to match exactly three letters then one or more digits to end of string, which fits perfectly.
  3. Step 3: Confirm correct syntax and behavior

    ~* is PostgreSQL's case-insensitive regex match operator with correct pattern.
  4. Final Answer:

    SELECT * FROM inventory WHERE code ~* '^[a-zA-Z]{3}[0-9]+$'; -> Option A
  5. Quick Check:

    Regex with ~* matches exact pattern = A [OK]
Hint: Use regex (~*) for exact letter-digit patterns ignoring case [OK]
Common Mistakes:
  • Using LIKE with wildcards but no character restrictions
  • Using invalid wildcard characters like '?'
  • Not using case-insensitive matching properly
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