CPCODELAB
SQL

LIKE — Pattern Matching

7 min read

Pattern Matching with LIKE

LIKE matches text against a pattern using two wildcard characters: % matches any sequence of characters (including zero characters) and _ matches exactly one character.

sql
-- Students whose name starts with 'A'
SELECT name FROM students WHERE name LIKE 'A%';

-- Students whose name ends with 'i'
SELECT name FROM students WHERE name LIKE '%i';

-- Students whose name contains 'ita'
SELECT name FROM students WHERE name LIKE '%ita%';

-- Exactly 5-character names
SELECT name FROM students WHERE name LIKE '_____';

Most databases also support ILIKE (PostgreSQL) for case-insensitive matching. In MySQL you can use LIKE with a binary column collation to force case-sensitivity.

Leading wildcards (LIKE '%abc') force a full table scan because the database cannot use a standard index to speed up the lookup. Use full-text search features for serious text-search workloads.

Ready to test yourself?
Practice SQL— quiz & coding exercises