The LIKE operator is used to search for a specified pattern in a column. It matches text based on wildcard characters like % for any sequence of characters and _ for a single character.
Click to reveal answer
beginner
What is the difference between LIKE and ILIKE in PostgreSQL?
LIKE is case-sensitive, meaning it distinguishes uppercase and lowercase letters. ILIKE is case-insensitive, so it matches letters regardless of case.
Click to reveal answer
beginner
What does the wildcard % represent in a LIKE pattern?
The % wildcard matches zero or more characters of any kind. For example, 'a%' matches any string starting with 'a'.
Click to reveal answer
beginner
How do you match exactly one character in a pattern using LIKE?
Use the underscore _ wildcard to match exactly one character. For example, 'b_t' matches 'bat', 'bit', or 'but'.
Click to reveal answer
intermediate
Write a query to find all names starting with 'Jo' ignoring case using ILIKE.
Example query: SELECT * FROM users WHERE name ILIKE 'Jo%'; This finds names like 'John', 'joanna', or 'JOSEPH'.
Click to reveal answer
Which wildcard matches any number of characters in a LIKE pattern?
A%
B_
C*
D?
✗ Incorrect
The percent sign (%) matches zero or more characters in SQL pattern matching.
What does ILIKE do differently than LIKE in PostgreSQL?
AMatches patterns case-insensitively
BMatches patterns only at the start of the string
CMatches patterns only at the end of the string
DMatches patterns with regular expressions
✗ Incorrect
ILIKE performs case-insensitive pattern matching.
Which pattern matches any single character in LIKE?
A%
B?
C_
D#
✗ Incorrect
The underscore (_) matches exactly one character.
What will WHERE name LIKE 'A%' return?
ANames containing 'A' anywhere
BNames ending with 'A'
CNames exactly equal to 'A'
DNames starting with 'A'
✗ Incorrect
The pattern 'A%' matches any string starting with 'A'.
Which query finds names like 'Sam', 'sam', or 'SAM'?
ASELECT * FROM users WHERE name LIKE 'Sam';
BSELECT * FROM users WHERE name ILIKE 'Sam';
CSELECT * FROM users WHERE name LIKE '%Sam%';
DSELECT * FROM users WHERE name = 'Sam';
✗ Incorrect
ILIKE matches 'Sam' ignoring case.
Explain how to use LIKE and ILIKE for pattern matching in PostgreSQL.
Think about how you find words that start with or contain certain letters.
You got /4 concepts.
Describe the difference between the wildcards % and _ in SQL pattern matching.
One wildcard is for many characters, the other for just one.
You got /2 concepts.
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
Step 1: Understand the difference between LIKE and ILIKE
LIKE matches patterns considering case, so 'A' and 'a' are different. ILIKE ignores case differences.
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.
Final Answer:
LIKE is case-sensitive, while ILIKE is case-insensitive. -> Option A
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
Step 1: Identify the pattern for names starting with 'Jo'
The pattern should be 'Jo%' to match strings starting with 'Jo'.
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.
Final Answer:
SELECT * FROM users WHERE name ILIKE 'Jo%'; -> Option C
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
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'.
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
Step 1: Identify why LIKE misses case differences
LIKE is case-sensitive, so 'User@Example.com' does not match '%@example.com'.
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.
Step 3: Confirm all options work
All three approaches fix the problem, so the correct answer is all of the above.
Final Answer:
All of the above -> Option B
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
Step 1: Understand the pattern requirements
We need exactly three letters (any case) followed by digits, and the match should be case-insensitive.
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.
Step 3: Confirm correct syntax and behavior
~* is PostgreSQL's case-insensitive regex match operator with correct pattern.
Final Answer:
SELECT * FROM inventory WHERE code ~* '^[a-zA-Z]{3}[0-9]+$'; -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.