The SQL Query to power Google Suggest

Lucas Jellema 6

You probably know Google Suggest or one of its derivatives. An input item on a web-page or another user interface where you can enter a value that all of a sudden becomes active and helpful by displaying a number of suggestions. For example a Country field that we can enter the country into within the previously selected Region (Europe in this case): 

After entering Bel, the suggestions are displayed as shown in the screenshot. This article describes the SQL we could use for producing the list of suggestions.....

The data returned by the query should satisfy these conditions:

  • only countries can be shown that are equal to what was entered, start with the value that was entered or come alfabetically after the value that was entered
  • if there is no value at all that starts with the string entered, then no values may be displayed 
  • no more than five values can be shown

When we type an extra g, Belarus disappears as suggestion since it does not start with Belg. Finland is added as fifth (and not very logical) suggestion.

When we enter Belgr, we see no suggestions at all, since no single value starts with Belgr.

 

The data displayed in this example is retrieved from two tables: REGIONS and COUNTRIES that are linked through a REGION_ID column. The join query looks like this:

 select region_name
, country_name
from regions
join
countries
using (region_id)
order
by region_name
, country_name

If we only want to see countries in Region=Europe, we have the following query:

select country_name
from regions
join
countries
using (region_id)
where region_name = 'Europe'

 

Now we want to create the Google Suggest Query, based on the requirements indicated above. The following query does exactly what we need:

select country_name
from ( select country_name
, first_value( country_name)
over (order by country_name) first_country_value
from ( select country_name
from regions
join
countries
using (region_id)
where region_name = 'Europe'
)
where country_name >= 'VALUE_ENTERED_BY_USER'
order
by country_name
)
where first_country_value like 'VALUE_ENTERED_BY_USER%'
and rownum < 6
/
 

Some results of using this query:

 

6 thoughts on “The SQL Query to power Google Suggest

  1. I’d thought of something like this at first:

    select omschrijving from bestemmingen
    where soundex(omschrijving) = soundex( ‘Santa’ )
    /

    OMSCHRIJVING
    —————————————————–
    Sand
    Sand
    Sannat
    Schwendau
    Sunny Day

    5 rijen zijn geselecteerd.

    Verstreken: 00:00:00.06

    Advantage: Finland would not appear in this list.
    Disadvantage: Soundex only works fine for English words.

    But, I’ve once written a Dutch algorithm for it so there is hope 😉

Comments are closed.

Next Post

Invitation for AMIS Query (14th December) on High Availability: RAC vs. Data Guard with Oak Table Network members Carel Jan Engel and Jeroen Evers

AMIS is proud to announce our next AMIS Query (free technical seminar), targeted at Oracle Database architects en DBAs: Oracle Database High Availability – RAC and Data Guard go head-to-head – presented by Oak Table Network members Carel Jan Engel (Dr. Data Guard for friends) and Jeroen Evers. In addition […]