How to use MySQL SOUNDEX() function with LIKE operator to retrieve the records from table?


As we know that SOUNDEX() function is used to return the soundex, a phonetic algorithm for indexing names after English pronunciation of sound, a string of a string. In the following example, we are taking the data from ‘student_info’ table and applying SOUNDEX() function with LIKE operator to retrieve a particular record from a table −

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101 | YashPal  | Amritsar   | History    |
| 105 | Gaurav   | Chandigarh | Literature |
| 125 | Raman    | Shimla     | Computers  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)

mysql> Select * from student_info where SOUNDEX(Name) LIKE '%G%';
+------+--------+------------+------------+
| id   | Name   | Address    | Subject    |
+------+--------+------------+------------+
| 105  | Gaurav | Chandigarh | Literature |
+------+--------+------------+------------+
1 row in set (0.00 sec)

We have used the name of the column as an argument of SOUNDEX() function and it returns the row that is having SOUNDEX values LIKE %G%.

Remember the output of SOUNDEX() function would always contain the first letter of a string passed in it as an argument. For example, if we will pass ‘Ram’ as an argument of SOUNDEX() function then see the output, it contains ‘R’ as the first character −

mysql> Select SOUNDEX('Ram');
+----------------+
| SOUNDEX('Ram') |
+----------------+
| R500           |
+----------------+
1 row in set (0.00 sec)

mysql> Select SOUNDEX('ram');
+----------------+
| SOUNDEX('ram') |
+----------------+
| R500           |
+----------------+
1 row in set (0.00 sec)

Updated on: 22-Jun-2020

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements