🐘 PostgreSQL
Beginner
How do you use LIKE and ILIKE in PostgreSQL?
Answer
LIKE performs case-sensitive pattern matching. ILIKE is PostgreSQL's case-insensitive variant. Pattern characters: % matches any sequence of characters (including empty), _ matches exactly one character. Examples: WHERE name LIKE 'A%' (starts with A), WHERE email LIKE '%@gmail.com' (ends with), WHERE code LIKE 'B_3' (B, any char, 3). For literal % or _, escape them: LIKE '50\%' ESCAPE '\' . Performance: LIKE with a leading wildcard (LIKE '%text') cannot use a B-tree index and requires a full scan. For prefix searches (LIKE 'text%'), a standard index works. For arbitrary pattern matching, use pg_trgm extension with a GIN/GiST index.