Levenshtein distance examples
Now let's take a closer look at how we can use the levenshtein
function to match strings against text data. If you recall, the levenshtein
distance represents the number of edits required to convert one string to another string being compared.
In a search application or when performing data analysis on any data that contains manual user input, you will always want to account for typos or incorrect spellings. The levenshtein
function provides a great method for performing this task. In this exercise, we will perform a query against the film
table using a search string with a misspelling and use the results from levenshtein
to determine a match. Let's check it out.
This exercise is part of the course
Functions for Manipulating Data in PostgreSQL
Exercise instructions
- Select the film title and film description.
- Calculate the levenshtein distance for the film title with the string
JET NEIGHBOR
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Select the title and description columns
SELECT
___,
___,
-- Calculate the levenshtein distance
___(___, ___) AS distance
FROM
film
ORDER BY 3