0Pricing
Prompt Engineering & LLM Optimization for Developers · レッスン

自然言語からSQLクエリを生成する

スキーマのコンテキスト、例、安全策を与えて、LLMに平易な英語の質問を正しいSQLへ変換させる方法を学びます。

「自然言語からSQLクエリを生成する」はCoddyKit上の無料Prompt Engineering & LLM Optimization for Developersレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPrompt Engineering & LLM Optimization for Developers学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Prompt Engineering & LLM Optimization for Developersコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Text-to-SQL Overview

One of the most practical developer uses of LLMs is turning a plain question like How many orders shipped last week? into a runnable SQL query.

Done well, it lets non-experts query data and speeds up everyday analysis.

The Model Needs the Schema

An LLM cannot guess your table and column names. Always include the relevant schema in the prompt so it references real fields.

Schema:
orders(id, customer_id, status, total, created_at)
customers(id, name, country)

A Basic Text-to-SQL Prompt

Combine schema, the question, and an instruction to return only SQL.

Given the schema above, write a single
Postgres query. Return only SQL.
Question: total revenue per country last month.

Specify the Dialect

SQL dialects differ (Postgres, MySQL, SQLite, BigQuery). State which one you use, or the model may emit functions your database does not support.

Few-Shot Examples Help

Showing one or two question-to-SQL pairs teaches the model your conventions, like how you format dates or name aliases.

Q: customers from Japan
SQL: SELECT * FROM customers WHERE country = 'Japan';
Q: orders over 100
SQL: SELECT * FROM orders WHERE total > 100;

Handling Joins

For questions spanning tables, hint at the relationships in the schema (foreign keys) so the model joins correctly instead of guessing.

-- orders.customer_id references customers.id
SELECT c.country, SUM(o.total)
FROM orders o JOIN customers c
  ON o.customer_id = c.id
GROUP BY c.country;

Guardrail: Read-Only

Never run model-generated SQL with write access. Restrict the connection to SELECT only and reject any query containing DELETE, UPDATE, or DROP.

if (/\b(delete|update|drop|insert)\b/i.test(sql)) {
  throw new Error('Only read queries allowed');
}

Validate Before Executing

Parse or EXPLAIN the query before running it. This catches syntax errors and lets you reject queries touching tables outside an allowlist.

EXPLAIN SELECT ...; -- check plan, no data fetched

Self-Correction Loop

If the query errors, feed the database error message back to the model and ask it to fix the SQL. Two or three attempts usually resolve most mistakes.

The query failed with: column "revenue"
does not exist. Fix the SQL.

Explaining Results

After running the query, you can ask the model to summarize the rows in plain language, closing the loop from question to readable answer.

Limits to Remember

  • Ambiguous questions yield ambiguous SQL.
  • Large schemas may not fit in context, retrieve the relevant tables.
  • Always verify aggregates on critical reports.

Quick Check

Test your understanding of text-to-SQL.

Recap

Text-to-SQL works by giving the model the schema, dialect, and few-shot examples, then validating output. Always run generated SQL read-only, validate before executing, and use a self-correction loop on errors.

よくある質問

「自然言語からSQLクエリを生成する」レッスンは無料ですか?

はい。「自然言語からSQLクエリを生成する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Prompt Engineering & LLM Optimization for Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Prompt Engineering & LLM Optimization for Developersコースには全4レッスンが含まれています。

「自然言語からSQLクエリを生成する」で何を学びますか?

スキーマのコンテキスト、例、安全策を与えて、LLMに平易な英語の質問を正しいSQLへ変換させる方法を学びます。 ブラウザで直接実行するハンズオンコードでPrompt Engineering & LLM Optimization for Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Prompt Engineering & LLM Optimization for Developersを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPrompt Engineering & LLM Optimization for Developersは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「自然言語からSQLクエリを生成する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPrompt Engineering & LLM Optimization for Developersレッスンでコードを書いて実行できますか?

はい。すべてのPrompt Engineering & LLM Optimization for Developersレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. コード生成とリファクタリング
  2. デバッグとテストケース生成
  3. データ抽出と要約
  4. 自然言語からSQLクエリを生成する
← Prompt Engineering & LLM Optimization for Developersに戻る