create or replace function is_sorted(str) as ( select not exists ( from (select (c > (lead(c) over ())) as outoforder from (select unnest(regexp_extract_all(str, '.')) as c)) where outoforder = true ) ); create or replace function longest_ordered_words(file) as table ( with words as ( from read_csv_auto( file, header=false, sep='', quote='', columns={'word': VARCHAR}) where is_sorted(word)), mx as (select max(length(word)) as mx from words) select word from words, mx where length(word) = mx order by word ); from longest_ordered_words('unixdict.txt');