The MySQL soundex function; Selecting names

Soundex is the algorithm for indexing names by sound or as pronounced. The MySQL SOUNDEX() function returns a Soundex string from a string supplied.

It can also be used to find same-sounding strings from a table.

The example people table:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  3 |       Ryan |
|  4 |        Tim |
|  5 |      Ayden |
|  6 |       Matt |
|  7 |      Aydin |

You can see there are several “aidens” but they are spelt different however essentially pronounced the same.

This is where SOUNDEX() can help because using a LIKE query will not.

An example SOUNDEX() query to get all names that sound like “aiden”

SELECT `id`, `first_name` FROM `people` WHERE soundex("aiden") = soundex(first_name);

The result:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  5 |      Ayden |
|  7 |      Aydin |

SOUNDEX() does its job in finding the values that sound the same as “aiden”.

SELECT `id`, `first_name` FROM `people` WHERE soundex("aidn") = soundex(first_name);

Returns the same results, however using soundex("aid") returns nothing, because it doesn’t sound like Aiden.

Mysql Docs for SOUNDEX()

SQL Fiddle example