← SQL and databases

Matching text patterns with LIKE

Find text that starts with, ends with or contains something.

Start this lesson

Sometimes you do not know the exact text, only part of it. LIKE compares text with a pattern. In a pattern, % stands for "any number of characters (even none)" and _ stands for "exactly one character". So 'The%' means "starts with The", '%Road' means "ends with Road" and '%garden%' means "contains garden".

SELECT title FROM books WHERE title LIKE '%Market%';

Finds The Midnight Market: there is some text before "Market" and nothing after, and % allows both.

In SQLite, LIKE ignores the difference between capital and small letters (for English letters), so '%market%' finds Market too. Other databases may be stricter.

Without % or _, LIKE works like =. The power is in the wildcards.