MySQL SOUNDEX() function
In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples.
Introduction of MySQL SOUNDEX() function
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 compare words spelled differently but sound alike in English.
Syntax of the MySQL SOUNDEX() function
The syntax of the MySQL SOUNDEX() function is given as follows:
SOUNDEX(str)
Parameters or arguments used in MySQL SOUNDEX() function:
Only one parameter is accepted by the SOUNDEX() function in MySQL, which is given as mentioned above and described below.
Str: The string whose phonetic representation we want to know.
Returns:
It will return a phonetic representation of the given string.
Some important points of MySQL SOUNDEX function:
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.
Application used for SOUNDEX() function:
The SOUNDEX() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL SOUNDEX() function:
Now we will look into some MySQL SOUNDEX() function examples and will explore how we can use the SOUNDEX function in MySQL.
Example 1:
In this example, we will Find the SOUNDEX string of 'JAVATPOINT' using the SOUNDEX Function in MySQL.
SELECT SOUNDEX(' JAVATPOINT ') AS SoundexString;
Output:
SoundexString
J131
1 row in set (0.00 sec)
Example 2:
In this example, we will Find the SOUNDEX string of 'WORLD' using the SOUNDEX Function in MySQL.
SELECT SOUNDEX('WORLD') AS SoundexString;
Output:
SoundexString
W643
1 row in set (0.00 sec)
Example 3:
When we require to find the SOUNDEX string for column data, then we can take the help of the SOUNDEX function in MySQL. For demonstration, we have created a table named Pupil.
CREATE TABLE Pupil(
Pupil_id INT AUTO_INCREMENT,
Pupil_name VARCHAR(100) NOT NULL,
Pupil_Class VARCHAR(20) NOT NULL,
PRIMARY KEY(Pupil_id )
);
Now we will insert some data into the Pupil table :
INSERT INTO Pupil
(Pupil_name, Pupil_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 Pupil Table is as follows.
mysql> select * from Pupil;
Pupil_id | Pupil_name | Pupil_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 |
Now, we are going to find the SOUNDEX string for column Pupil_name.
SELECT
Pupil_id, Pupil_name,
SOUNDEX( Pupil_name) AS SoundexSname,
Pupil_Class FROM Pupil ;
Output:
Pupil_id | Pupil_name | SoundexSname | Pupil_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 |
9 rows in set (0.00 sec)
Application of MySQL SOUNDEX() function:
This function is used to return a phonetic representation of a string.
Summary:
In the above context, we have learned how we can use the SOUNDEX() function in MySQL used to return a phonetic representation of a string.