Ok, just write because I had the same problem. If you know the range of prefix lengths that you have, you can do something similar to the following. The following example assumes a prefix length of 2-6
select t.num, coalesce(p6.PREFIX, p5.PREFIX, p4.PREFIX, p3.PREFIX, p2.PREFIX) PREFIX from NUMBERS t LEFT OUTER JOIN PREFIXES p2 ON substr(t.num,1,2)=p2.PREFIX LEFT OUTER JOIN PREFIXES p3 ON substr(t.num,1,3)=p3.PREFIX LEFT OUTER JOIN PREFIXES p4 ON substr(t.num,1,4)=p4.PREFIX LEFT OUTER JOIN PREFIXES p5 ON substr(t.num,1,5)=p5.PREFIX LEFT OUTER JOIN PREFIXES p6 ON substr(t.num,1,6)=p6.PREFIX
Equal joins are as good as you can get.
I believe it works much better than any other possible solution here, hope this helps anyone who stumbles on the same problem
Sqlfiddle link modified from sailaway answer, whose script still gives all matches, not just the longest
source share