SQL LIKE

It searchs for a specified pattern in a column

SQL LIKE Syntax

SELECT * FROM table_name WHERE column_name LIKE pattern

The “%” sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

‘%ab ‘ – It will return data that end with the letter ‘ab’
‘ab%’ – It returns the data that starts with ‘ab’
‘ab’ – its similar to =
‘%ab%’ – it returns the data that has the value ‘ab’ in it

MySQL Table – users

+—-+——+
| id | name |
+—-+——+
|  1 | PHP  |
|  2 | JS   |
|  3 | HTML |
|  5 | ASP  |
+—-+——+

Example

mysql> select * from users WHERE name LIKE ‘%HP’;
+—-+——+
| id | name |
+—-+——+
|  1 | PHP  |
+—-+——+
1 row in set (0.00 sec)

mysql> select * from users WHERE name LIKE ‘HT%’;
+—-+——+
| id | name |
+—-+——+
|  3 | HTML |
+—-+——+
1 row in set (0.00 sec)

mysql> select * from users WHERE name LIKE ‘HT%’;
+—-+——+
| id | name |
+—-+——+
|  3 | HTML |
+—-+——+
1 row in set (0.00 sec)

mysql> select * from users WHERE name LIKE ‘%p%’;
+—-+——+
| id | name |
+—-+——+
|  1 | PHP  |
|  5 | ASP  |
+—-+——+
2 rows in set (0.00 sec)

Share