Skip to main content

PROMPT_SQL

Generate SQL

You can ask MotherDuck AI in plain English to write SQL for you using the prompt_sql table function.

Syntax

CALL prompt_sql('<natural language question>', [include_tables=['<table_name>', '<table_name>']);

Include Tables parameter

You can specify the tables and columns that you want to include in the SQL generation process. This is useful when you want to generate SQL for a specific table or column.

CALL prompt_sql('<natural language question>', include_tables=['<table_name>', '<table_name>'])
CALL prompt_sql('<natural language question>', include_tables={'<table_name>': ['<column_name>', '<column_name>']})
CALL prompt_sql('<natural language question>', include_tables={'<table_name>': ['<column_regex>', '<column_name>']})

Example usage

We use MotherDuck's sample Hacker News dataset from MotherDuck's sample data database.

CALL prompt_sql('what are the top domains being shared on hacker_news?');

Output of this SQL statement is a single column table with the following content:

query
SELECT COUNT(*) as domain_count, SUBSTRING(SPLIT_PART(url, '//', 2), 1, POSITION('/' IN SPLIT_PART(url, '//', 2)) - 1) as domain FROM hn.hacker_news WHERE url IS NOT NULL GROUP BY domain ORDER BY domain_count DESC LIMIT 10

Notes

MotherDuck AI operates on your current database by evaluating the schemas and contents of the database. You can specify which tables and columns should be considered, using the optional include_tables parameter. By default, all tables in the current database are considered. To point MotherDuck AI at a specific database, execute the use database command (learn more about switching databases).

These capabilities are provided by MotherDuck's integration with OpenAI. For availability and pricing, see MotherDuck's Pricing Model