Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Databases SQL Basics Finding the Data You Want Finding Data that Matches a Pattern

Question about relation between LIKE and %

Hello, I'm just a little confused on what LIKE and the % does. I know that "%text%" will search through everything with text, so what's the need for the LIKE clause?

Thanks,

1 Answer

Leandro Botella Penalva
Leandro Botella Penalva
17,618 Points

Hi Max,

LIKE is an operator that tells to find something that matches the pattern between quotes you write after the LIKE. The "%" character is a wildcard that represents that where is placed should be zero, one or multiple characters. There is another wildcard character, the "_" which represents that where is placed should be only one character.

For example if you have: LIKE 'c%' you are finding something that starts with "c" and after that whatever (remember that % means 0, 1 or multiple characters), and with LIKE '_c%' you are finding something that starts with any character (remember that _ means just 1 character), the second character must be a "c" and then whatever.

And with the example of LIKE '%text%' you will find anything that starts, contains or ends with the word "text".

Hello,

Thanks for responding, I think I got confused with past experience. I recall seeing something like

SELECT * FROM table WHERE something = "%text%";

So I was wondering what does adding the LIKE do to it. But it seems like that you need the LIKE keyword.

Anyways, thanks.