We use cookies to enhance your experience on the site
CodeWorlds

Searching patterns: LIKE

Sometimes we don't know the exact value - we only remember that a title starts with the letter O, or that a surname contains the syllable "kow". For such fuzzy searching we use the LIKE operator together with wildcard characters.

The percent sign %

The most important wildcard is the percent

%
, which replaces any sequence of characters (including empty):

1SELECT * FROM ksiazki
2WHERE tytul LIKE 'O%';

This means: "Titles starting with O" - after the letter O there can be anything (e.g. Odyssey, Old scroll).

Three popular patterns

  • 'O%'
    - starts with O (percent at the end),
  • '%a'
    - ends with the letter a (percent at the beginning),
  • '%war%'
    - contains the word "war" anywhere in the middle (percent on both sides).
1SELECT * FROM ksiazki
2WHERE tytul LIKE '%war%';

This query will find any title in which the word "war" appears - at the beginning, in the middle or at the end.

Remember the quotes

A pattern in

LIKE
is text, so we always wrap it in single quotes:

1SELECT * FROM czytelnicy
2WHERE nazwisko LIKE 'Kow%';

This will find all readers whose surname starts with "Kow" - for example Kowalski or Kowalczyk.

LIKE
is the scribe's eye, @name, able to recognize a scroll even from a fragment of its title!

Go to CodeWorlds