Course

Like - SQL

Sometimes we need to loosely match some filters in our Database. Think of a search. What if we wanted our website to contain a searchbar that let's users look for other users by 'name'?

Well we can use , but then the user needs to have the exact name for the results to work.

SQL instead let's us loosely match things using "LIKE".

"LIKE" gives us the wildcard operator so that we can match the search string anywhere in the results. The beginning, the end, or the middle.

To match just the beginning of a value- we append the symbol to the end of our search term.

ID (INT)name (VARCHAR)
1Oscar
2Marcus
3Mario
SELECT * FROM Users
WHERE name LIKE "Mar%";
ID (INT)name (VARCHAR)
2Marcus
3Mario

In this example we searched for meaning that we'll match any names that start with and have anything after. So we'll match values like Marcus, Mario, and Marshal.

To match the end of a value- we preprend the symbol to the beginning of our search term.

ID (INT)name (VARCHAR)
1Oscar
2Marcus
3Mario
SELECT * FROM Users
WHERE name LIKE "%ar";
ID (INT)name (VARCHAR)
1Oscar

In this example we searched for meaning we'll match any named that end in , no matter what is at the beginning of the name.

If we instead want to match anything in the middle of a value- we can surround our search term with symbols:

ID (INT)name (VARCHAR)
1Oscar
2Marcus
3Mario
SELECT * FROM Users
WHERE name LIKE "%ar%";
ID (INT)name (VARCHAR)
1Oscar
2Marcus
3Mario

This will match any rows with a name that contains anywhere in the name. Oscar, Mario, Lars, Marcus all will match.

You'll often use for searching loosely over values.

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Updating Rows With SQL