3

I am looking for a dataset containing natural language sentences (English preferred) and their corresponding SQL queries.

E.g. "What are the names of the employees who live in Los Angeles" could be associated with the SQL query "SELECT name FROM employees WHERE location = 'LA'"

  • 4
    Could also be Louisiana or Laos ;) – philshem Feb 25 '15 at 20:06
  • 1
    @philshem I got lazy when writing the query :) – Franck Dernoncourt Feb 25 '15 at 20:18
  • 3
    For what it's worth, that's going to be really difficult. Queries only make sense in the presence of underlying schema. For example, what if instead of a Location column on the table, there was a LocationId that was a foreign key back to a Locations table? The query changes to select e.name from employees as e join locations as l on e.locationid = l.locationid where l.name = 'LA' – Ben Thul Feb 26 '15 at 21:41
  • @BenThul Definitely, the schema would have to be specified. – Franck Dernoncourt Feb 26 '15 at 21:46
4

Not exactly SQL but SPARQL: NL-SPARQL: A Dialog-System Challenge Set for Converting Natural Language to Structured Queries

NL-SPARQL is a data set of natural language (NL) utterances to a conversational system in the movies domain and corresponding queries to Freebase in SPARQL. This dataset was collected via Crowdsourcing as described below.

The data set is split into two sets: training (3,338 examples) and test (1,084 examples) set NL utterance and SPARQL query pairs.

Two examples:

NL utterance:

How many movies has Alfred Hitchcock directed?

SPARQL Query:

 SELECT (COUNT(?movie) AS ?count) WHERE {?movie http://rdf.freebase.com/ns/type.object.type http://rdf.freebase.com/ns/film.film. ?movie http://rdf.freebase.com/ns/film.film.directed_by ?director. ?director http://rdf.freebase.com/ns/type.object.name "Alfred Hitchcock"@en.}

NL utterance:

Show me movies in Chinese.

SPARQL Query:

SELECT ?movie WHERE { ?movie http://rdf.freebase.com/ns/type.object.type http://rdf.freebase.com/ns/film.film. ?movie http://rdf.freebase.com/ns/film.film.language ?lang. ?lang http://rdf.freebase.com/ns/type.object.name "Chinese language"@en.}

2

You can find homework assignments that are submitted as forks for a MOOC Github repo. In this case, you'll find multiple SQL statements for the same question. The SQL style will be a little biased because all the students are following the same course.

An example:

Then find forks of the main repo.

The files contain code like this:

-- TASK 09
-- *Write a T-SQL script that shows for each
-- town a list of all employees that live in it.

GO

USE TelerikAcademy
GO

DECLARE townCursor CURSOR READ_ONLY FOR
SELECT t.Name, t.TownID
FROM Towns t

OPEN townCursor
DECLARE @townName nvarchar(50), @townID int
FETCH NEXT FROM townCursor INTO @townName, @townID

WHILE @@FETCH_STATUS = 0
BEGIN

    DECLARE empCursor CURSOR READ_ONLY FOR
    SELECT e.FirstName, e.LastName, a.TownID
    FROM Employees e, Addresses a
    WHERE e.AddressID = a.AddressID

    OPEN empCursor
    DECLARE @empFirstName nvarchar(50), @empLastName nvarchar(50), @empTownID nvarchar(50), @toPrint nvarchar(1000) = @townName + ' ->'

    FETCH NEXT FROM empCursor INTO @empFirstName, @empLastName, @empTownID

    WHILE @@FETCH_STATUS = 0
    BEGIN
        IF(@empTownID = @townID)
        BEGIN
            SET @toPrint = @toPrint + ' ' + @empFirstName + ' ' + @empLastName + ','
        END

        FETCH NEXT FROM empCursor INTO @empFirstName, @empLastName, @empTownID
    END

    DECLARE @length int = LEN(@toPrint)
    SET @toPrint = SUBSTRING(@toPrint, 1, @length - 1)
    PRINT @toPrint;

    CLOSE empCursor
    DEALLOCATE empCursor

    FETCH NEXT FROM townCursor INTO @townName, @townID
END

CLOSE townCursor
DEALLOCATE townCursor

This MOOC is just one example...

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.