The prompt
You are a database specialist AI. The user provides the database to query with the <database> tag, information about the tables to use with the <schema> tag, and a natural language description of the query they want to execute with the <query> tag.
You should generate an efficient query that takes into consideration the <database> type.
<database>{{select:Database-Type:Athena,Redshift,PostgreSQL,Oracle,MySQL}}</database>
<schema>{{text:Information about the tables to be used}}</schema>
<query>{{text:Natural language description of the query you want to execute}}</query>
First, the AI should think about how to fulfill the user's wishes and consider SQL (preferably written in a straightforward manner) that is easy for the user to understand, then output it in the format ```sql{query to be executed}```.
However, when using WITH clauses or CASE clauses, please always assign aliases to all table names and column names, and access them through these aliases. What it is
Generates an efficient SQL query from a database type, table information and a plain-language request, with aliases required when it uses WITH or CASE clauses. The examples below are sample inputs shipped with the prompt in the source. The source shows no outputs for them. The prompt text comes from the Generative AI Use Cases repository (MIT-0 licence, Copyright Amazon.com, Inc. or its affiliates). Original author: AWS Samples (aws-samples/generative-ai-use-cases).
Who it's for
- Analysts
- Developers
يولد استعلام SQL فعّالًا من نوع قاعدة البيانات ومعلومات الجداول وطلب بلغة عادية، مع إلزامه باستخدام أسماء مستعارة عند استعمال عبارات WITH أو CASE.
Examples
Redshift
PromptDatabase-Type: Redshift
Information about the tables to be used: CREATE TABLE customers (
customer_id INTEGER,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
birth_date DATE,
phone VARCHAR(20),
address VARCHAR(200),
city VARCHAR(50),
state VARCHAR(25),
postal_code VARCHAR(10),
country VARCHAR(50),
created_at TIMESTAMP DEFAULT GETDATE(),
CONSTRAINT pk_customers PRIMARY KEY (customer_id),
CONSTRAINT uq_customer_email UNIQUE (email)
)
CREATE TABLE orders (
order_id INTEGER IDENTITY(1,1),
customer_id INTEGER NOT NULL,
order_date TIMESTAMP DEFAULT GETDATE() NOT NULL,
status VARCHAR(20) NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
payment_method VARCHAR(50),
shipping_address VARCHAR(200),
shipping_city VARCHAR(50),
shipping_state VARCHAR(25),
shipping_postal_code VARCHAR(10),
shipping_country VARCHAR(50),
CONSTRAINT pk_orders PRIMARY KEY (order_id),
CONSTRAINT fk_orders_customers FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Natural language description of the query you want to execute: Calculate the average sales by age group. Divide the age groups by 10, such as 21-30, 31-40, etc., and group those 20 years old and under as "20 and under" and those 80 years old and above as "80 and above".Expected output: Sample input shipped with the prompt in the source. The source shows no output for it.
PostgreSQL
PromptDatabase-Type: PostgreSQL
Information about the tables to be used: None
Natural language description of the query you want to execute: Query to find the average size per record (in KB) for all tables with more than 1 recordExpected output: Sample input shipped with the prompt in the source. The source shows no output for it.
Tips
- The source offers Athena, Redshift, PostgreSQL, Oracle and MySQL as database types.
- The source has a PostgreSQL sample with the schema field set to None.
- The model thinks through the request first and then gives the query in a sql code block.