MySQL Tutorial
MySQL LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
MySQL LIKE uses % and _..
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara |
Now we will fetch records from the Students table with a NAME starting with "s".
SELECT * FROM Students
WHERE NAME LIKE '%s';
The above SELECT statement selects NAME starts with s alphabet Students table.
ROLL_NO | NAME | SUBJECT |
---|---|---|
2 | SAM | Python |
3 | Sara | HTML |
Examples showing different LIKE operators with '%' and '_' wildcards:
Operator | Description |
---|---|
WHERE NAME LIKE 's%' | Finds any values that start with "s" |
WHERE NAME LIKE '%s' | Finds any values that end with "s" |
WHERE NAME LIKE '%or%' | Finds any values that have "or" in any position |
WHERE NAME LIKE '_s%' | Finds any values that have "s" in the second position |
WHERE NAME LIKE 's_%' | Finds any values that start with "s" and are at least 2 characters in length |
WHERE NAME LIKE 's__%' | Finds any values that start with "s" and are at least 3 characters in length |
WHERE NAME LIKE 's%o' | Finds any values that start with "s" and ends with "o" |