0Pricing
Prompt Engineering & LLM Optimization for Developers · บทเรียน

การสร้างคำค้น SQL จากภาษาธรรมชาติ

เรียนรู้การเขียนพรอมต์ให้ LLM แปลคำถามภาษาทั่วไปเป็น SQL ที่ถูกต้อง ด้วยการให้บริบทโครงสร้างฐานข้อมูล ตัวอย่าง และเกราะป้องกันด้านความปลอดภัย

การสร้างคำค้น SQL จากภาษาธรรมชาติ เป็นบทเรียน Prompt Engineering & LLM Optimization for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 จากภาษาธรรมชาติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Prompt Engineering & LLM Optimization for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Prompt Engineering & LLM Optimization for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสร้างคำค้น SQL จากภาษาธรรมชาติ”

เรียนรู้การเขียนพรอมต์ให้ LLM แปลคำถามภาษาทั่วไปเป็น SQL ที่ถูกต้อง ด้วยการให้บริบทโครงสร้างฐานข้อมูล ตัวอย่าง และเกราะป้องกันด้านความปลอดภัย คุณปฏิบัติ Prompt Engineering & LLM Optimization for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Prompt Engineering & LLM Optimization for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Prompt Engineering & LLM Optimization for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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