Premise: Suppose you have a table containing words, where some may be distinct and some "may overlap", meaning a longer word starts with a shorter one, eg:
---------------
| word |
---------------
| dog | *
| games | *
| stat |
| state |
| statement | *
| fulfill |
| fulfilled | *
| fulfillment | *
---------------
Question: How does one write a query that returns the list of non-overlapping + the longest overlapping words in such cases?
In the example above, the desired words are identified by a *, as per the following extended explanation:
dogandgamesdon't overlap with anything, so they're the longest in their "solo/distinct" categorystatementoverlaps withstateandstatand is the longestfulfilledoverlaps withfulfilland is longer (does NOT overlap withfulfillment)fulfillmentoverlaps withfulfilland is longer (does NOT overlap withfulfilled)
Note: Please be aware that for the sake of simplicity, the data sample is reduced. In reality there are a few million records to query and, there are no search terms known before hand, so it's not possible to directly use constructs such as WHERE word LIKE 'stat%'. Not sure if relevant or not, but the words have a relatively short max length, eg 20.