Can GPT‑3 Write Complex SQL Queries? A Hands‑On Exploration

This article demonstrates how GPT‑3 can translate plain‑English questions into SQL statements, starting with simple counts and progressing to multi‑table joins, while highlighting prompting techniques, limitations, and the impact of model variants such as GPT‑3 Instruct.

ITPUB
ITPUB
ITPUB
Can GPT‑3 Write Complex SQL Queries? A Hands‑On Exploration

SQL Generation with GPT‑3

GPT‑3 can translate a natural‑language request into a PostgreSQL query. For example, the prompt “How many users signed up last month?” yields:

SELECT COUNT(*) FROM users WHERE signup_time > now() - interval '1 month'

Handling a Multi‑Table Schema

The sandbox defines two tables: users – columns: id (int), signup_dt (timestamp), email (varchar), plan_type (varchar) charges – columns: amount (bigint), user_id (int), charge_dt (timestamp)

With this schema GPT‑3 correctly answers questions such as:

SELECT SUM(amount) FROM charges WHERE charge_dt > now() - interval '7 days'

It also produces a conditional aggregation for a fixed date range:

SELECT SUM(CASE WHEN charge_dt >= '2020-10-01'::date AND charge_dt < '2020-11-15'::date THEN amount ELSE 0 END) AS revenue FROM charges;

Teaching GPT‑3 to Join Tables

Initial join‑related prompts failed because the model did not know which table contained email. By supplying example queries that expose the schema, GPT‑3 learns to generate proper INNER JOIN statements. Example:

SELECT MIN(charge_dt) AS last_payment_dt
FROM users
INNER JOIN charges ON users.id = charges.user_id
WHERE users.email = '[email protected]';

Further prompts demonstrate counting payments per user, summarising recent sign‑ups, and calculating monthly sign‑up totals, all using correct joins and aggregation.

Prompt Engineering and Model Choice

Providing concrete examples ("example‑driven" prompting) markedly improves output quality. The GPT‑3 Instruct variant consistently outperforms the generic GPT‑3 engine for SQL generation tasks.

Open‑Source Sandbox

The full experiment code is available at:

https://github.com/bkane1/gpt3-instruct-sandbox

SQLprompt engineeringdatabase automationGPT-3AI-generated code
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.