Pull rows out, then filter them

INPUT · Slides

Search by a word inside

01 / 04

You do not know the exact name

= can only hit names that are completely the same. Unless you get Bluesky Bread right down to the last letter, you will not find it.

But looking for something is usually vaguer than that — "it had the word bread in it somewhere". LIKE is how you write that.

02 / 04

LIKE and %

Write LIKE instead of =, and put a % inside the value. % means "anything at all can sit here".

'%Bread%' says "something, then Bread, then something else" — in other words, contains Bread.

SELECT * FROM shops  WHERE name LIKE '%Bread%';

Result

1 | Bluesky Bread | bread | 1 | 4
3 | Sunset Bread Workshop | bread | 1 | 6

03 / 04

% is happy with nothing

What % covers is "nought or more characters". So nothing at all is fine.

That is why '%Bread%' also hits Bluesky Bread, where there are nought characters after it. That property is what makes "contains" come out so neatly.

SELECT name FROM shops  WHERE name LIKE '%Bookshop%';

Result

Lane Bookshop
Sunny Bookshop

04 / 04

It works on any column

LIKE works on any column that holds text. Names, kinds, it makes no difference.

The search box that finds things by what you typed is built out of queries like this. Let us write some.

SELECT name, kind FROM shops  WHERE kind LIKE '%b%';

Result

Bluesky Bread | bread
Lane Bookshop | books
Sunset Bread Workshop | bread
Sunny Bookshop | books