The SQL Query to power Google Suggest

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): 

The SQL Query to power Google Suggest googleSuggest1

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.

The SQL Query to power Google Suggest googleSuggest2

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

The SQL Query to power Google Suggest googleSuggest3 

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:

The SQL Query to power Google Suggest googleSuggest4 

6 Comments

  1. Patrick Sinke November 29, 2006
  2. Marco Gralike November 27, 2006
  3. Lucas Jellema November 13, 2006
  4. Jeff Kemp November 11, 2006
  5. Karl r. November 10, 2006
  6. Alex Nuijten November 10, 2006