In this tutorial you will learn about the MySQL Like Clause and its application with practical example.
The LIKE operator is used in a WHERE clause to search for a specified pattern of string in a column value, WHERE clause with equal sign (=) works fine where we want to do an exact match. Like if “emp_name = ‘deepmala'”. But there may be a requirement where we want to filter out all the results where emp_name should contain “mala”. This can be handled using SQL LIKE clause along with WHERE clause.
If LIKE clause is used along with % characters then sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
Without a % character LIKE clause works similar to equal sign along with WHERE clause.
Syntax:
1 2 3 |
SELECT <column_name> FROM <table_name> WHERE <column_name> LIKE <pattern> |
- LIKE clause can be used with WHERE clause.
- LIKE clause can be used in place of equal sign.
- When LIKE is used alongwith % sign then sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
- We can have more than one conditions with AND or OR operators
- LIKE clause can be used along in DELETE or UPDATE query too.
LIKE Operator Example
The “emp_info” table:
empID | empName |
---|---|
1 | smita |
2 | sneha |
3 | jyoti |
4 | pooja |
Now we want to select the employees whose name starts with character “s” from the table above then the query would be like below.
1 2 |
SELECT * FROM emp_info WHERE EMPNAME LIKE 's%'; |
OUTPUT:
empID | empName |
---|---|
1 | smita |
2 | sneha |