Open In App

SOUNDEX() Function in MySQL

Last Updated : 07 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

SOUNDEX() function in MySQL is used to return a phonetic representation of a string. The phonetic represents the way the string will sound. The SOUNDEX function helps to compare words that are spelled differently, but sound alike in English.

Syntax :

SOUNDEX(str)

Parameter :
SOUNDEX() function accepts one parameter as mentioned above and described below.

  • str : The string whose phonetic representation we want to know.

Returns :
It returns phonetic representation of given string.

Note :

  • This function, as currently implemented, is intended to work well with strings that are in the English language only. Strings in other languages may not produce reliable results.
  • This function is not guaranteed to provide consistent results with strings that use multibyte character sets, including utf-8.

Example-1 :
Finding the SOUNDEX string of ‘geeksforgeeks’ using SOUNDEX Function.

SELECT SOUNDEX('geeksforgeeks') AS SoundexString; 

Output :

SoundexString
G162

Example-2 :
Finding the SOUNDEX string of ‘Hello’ using SOUNDEX Function.

SELECT SOUNDEX('Hello') AS SoundexString; 

Output :

SoundexString
H400

Example-3 :
SOUNDEX Function can also be used to find the SOUNDEX string for column data. To demonstrate create a table named Student.

CREATE TABLE Student
(
   Student_id INT AUTO_INCREMENT,  
   Student_name VARCHAR(100) NOT NULL,
   Student_Class VARCHAR(20) NOT NULL,
   PRIMARY KEY(Student_id )

);

Now inserting some data to the Student table :

INSERT INTO Student
(Student_name, Student_Class )
VALUES
   ('Ananya Majumdar', 'IX'),
   ('Anushka Samanta', 'X' ),
   ('Aniket Sharma', 'XI' ),
   ('Anik Das', 'X'  ),
   ('Riya Jain', 'IX' ),
   ('Tapan Samanta', 'X' ),
   ('Deepak Sharma', 'X'  ),
   ('Ankana Jana', 'XII'),
   ('Shreya Ghosh', 'X') ;

So, the Student Table is as follows.

mysql> select * from Student;
+------------+-----------------+---------------+
| Student_id | Student_name    | Student_Class |
+------------+-----------------+---------------+
|          1 | Ananya Majumdar | IX            |
|          2 | Anushka Samanta | X             |
|          3 | Aniket Sharma   | XI            |
|          4 | Anik Das        | X             |
|          5 | Riya Jain       | IX            |
|          6 | Tapan Samanta   | X             |
|          7 | Deepak Sharma   | X             |
|          8 | Ankana Jana     | XII           |
|          9 | Shreya Ghosh    | X             |
+------------+-----------------+---------------+
9 rows in set (0.00 sec)

Now, we are going to find SOUNDEX string for column Student_name.

SELECT  
   Student_id,  Student_name,
   SOUNDEX( Student_name) AS  SoundexSname,
   Student_Class FROM Student ;    

Output :

+------------+-----------------+--------------+---------------+
| Student_id | Student_name    | SoundexSname | Student_Class |
+------------+-----------------+--------------+---------------+
|          1 | Ananya Majumdar | A52536       | IX            |
|          2 | Anushka Samanta | A5253        | X             |
|          3 | Aniket Sharma   | A523265      | XI            |
|          4 | Anik Das        | A5232        | X             |
|          5 | Riya Jain       | R250         | IX            |
|          6 | Tapan Samanta   | T15253       | X             |
|          7 | Deepak Sharma   | D1265        | X             |
|          8 | Ankana Jana     | A52525       | XII           |
|          9 | Shreya Ghosh    | S620         | X             |
+------------+-----------------+--------------+---------------+

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads