MySQL - SOUNDS LIKE Operator
The MySQL SOUNDS LIKE operator is used to perform a fuzzy search on strings that sound similar but may be spelled differently. Fuzzy search searches for text that matches a term closely instead of exactly.
SOUNDS LIKE operator is based on the Soundex algorithm, which is a phonetic algorithm that converts words into a code based on their sound. This operator returns 1 if the Soundex codes of two strings are same, and 0 if they are different.
Syntax
Following is the syntax of SOUNDS LIKE operator −
expr1 SOUNDS LIKE expr2
Example
In the following example, we are comparing two equal strings −
SELECT 'tutorialspoint' SOUNDS LIKE 'tutorialspoint';
Following is the output of the above code −
| 'tutorialspoint' SOUNDS LIKE 'tutorialspoint' |
|---|
| 1 |
Example
If the soundex values of both arguments doesn't match, this function returns 0 −
SELECT 'banana' SOUNDS LIKE 'apple';
Output of the above code is as shown below −
| 'banana' SOUNDS LIKE 'apple' |
|---|
| 0 |
Example
In both arguments of this function are empty, it returns 1 −
SELECT '' SOUNDS LIKE '';
We get the output as follows −
| '' SOUNDS LIKE '' |
|---|
| 1 |
Example
If the either of the arguments of this function is NULL, it returns NULL −
SELECT NULL SOUNDS LIKE 'test';
Following is the output of the above code −
| NULL SOUNDS LIKE 'test' |
|---|
| NULL |
Example
Let us create a table named "STUDENTS_TABLE" and insert records into it using CREATE and INSERT statements as shown below −
CREATE TABLE STUDENTS_TABLE ( name VARCHAR(15), marks INT, grade CHAR );
Now, let us insert records into it using the INSERT statement −
INSERT INTO STUDENTS_TABLE VALUES
('Raju', 80, 'A'),
('Rahman', 60, 'B'),
('Robert', 45, 'C');
The STUDENTS_TABLE obtained is as follows −
| name | marks | grade |
|---|---|---|
| Raju | 80 | A |
| Rahman | 60 | B |
| Robert | 45 | C |
Following query compares the entities of the column "name" with the string 'Raju' and retrieves the result −
SELECT name, marks, grade, name SOUNDS LIKE 'robert' FROM STUDENTS_TABLE;
After executing the above code, we get the following output −
| name | marks | grade | name SOUNDS LIKE 'robert' |
|---|---|---|---|
| Raju | 80 | A | 0 |
| Rahman | 60 | B | 0 |
| Robert | 45 | C | 1 |