How to Use LIKE and ILIKE in SQL?
Discover how to effectively filter your SQL queries for partial text matches with LIKE and ILIKE for case-insensitivity.
Formula Genius can generate a draft from a plain-English prompt. Supported formulas can be checked with sample cases; review the coverage on the result.
The formula
"Filter rows using LIKE for partial text matching, and ILIKE for case-insensitive matching in PostgreSQL"
SELECT * FROM table_name WHERE column_name LIKE 'pattern' OR column_name ILIKE 'pattern';
This SQL query filters rows based on partial text matches, allowing for case-sensitive and case-insensitive searches.
Step-by-step breakdown
- SELECT * FROM table_name: Specifies the table to retrieve data from.
- WHERE: Introduces the condition for filtering the rows.
- column_name: The specific column in the table to apply the filter.
- LIKE 'pattern': Searches for rows that match the specified pattern, case-sensitive.
- ILIKE 'pattern': Searches for rows that match the specified pattern, case-insensitive.
Edge cases & warnings
- Using wildcards (%) incorrectly may lead to unexpected results.
- Empty strings in the pattern can return all rows.
- Case sensitivity may cause missed matches if ILIKE is not used when needed.
Examples
"SELECT * FROM users WHERE name LIKE 'Jo%';"
Returns all users whose names start with 'Jo'.
"SELECT * FROM products WHERE description ILIKE '%apple%';"
Returns all products with 'apple' in the description, regardless of case.
Frequently asked questions
What is the difference between LIKE and ILIKE?
LIKE is case-sensitive while ILIKE is case-insensitive.
Can I use wildcards with LIKE and ILIKE?
Yes, you can use '%' for any sequence of characters and '_' for a single character.
How do I match exact phrases?
Use LIKE with the exact phrase enclosed in single quotes.
Can't find what you need?
Describe a formula in plain English. Formula Genius drafts and explains it, and can check supported formulas on sample cases.