AdvancedString Functions

LEVENSHTEIN: fuzzy matching distance

The query

SQL
-- Requires: CREATE EXTENSION fuzzystrmatch;
SELECT
  a.name AS name1,
  b.name AS name2,
  LEVENSHTEIN(LOWER(a.name), LOWER(b.name)) AS edit_distance,
  SIMILARITY(a.name, b.name) AS similarity_score
FROM customers a
CROSS JOIN customers b
WHERE a.cust_id < b.cust_id
  AND LEVENSHTEIN(LOWER(a.name), LOWER(b.name)) <= 3
ORDER BY edit_distance;
Tested against PostgreSQL 16

Note

Levenshtein distance = minimum edits to transform one string to another. Fuzzy name matching.

Tables referenced