Hone

Lessons · SQL · LIKE

Pattern matching on text

LIKE 'A%' matches text starting with A; % is any run of characters, _ is exactly one.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Search boxes, prefix filters, 'contains'.

How to think about it

Where can anything appear in the text? Put % where anything may appear. Case sensitivity depends on the database; SQLite's LIKE ignores case for ASCII.

Worked example

WHERE title LIKE 'The %'
Starts with 'The '.
WHERE title LIKE '%night%'
Contains night.
WHERE code LIKE 'A__'
A followed by exactly two characters.

Your turn

Emails ending in .org.

WHERE email LIKE '.org'

The trap

A leading % cannot use an index: '%night%' scans the whole table. Fine for thousands, slow for millions.

Practise LIKE on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.