LearnAI
Module 16 · AI for Data Science (Pro) · Lesson 1/5
← Back to catalog
Task

Text to SQL query

How to do text-to-SQL securely? Return JSON {"steps": [list of 2+ steps], "why": "short"}.

Unlock access to submit solutions for instant AI review.Start for free
Question in language → query to the database
💡 A bit of theory

The model can turn a question in natural language into an SQL query to the database. How to embed safely: give the model a SCHEMA of the database (tables, fields, relationships) so that it builds a correct query; generate a query, but DO NOT execute it blindly - check first (read-only, allowed tables, limits); show the user a query or result for control; restrict access rights to the database for this path (SELECT only, only the necessary tables). Risk: the model may construct an incorrect or dangerous query - therefore, there is always a check between generation and execution. Rule: text-to-SQL opens a database for questions in the language, but the generated query is untrusted: provide a schema, limit rights and check before execution.

How it's graded · passing score 70

  • 1Valid JSON without text around it30%
  • 2There is an array steps and why35%
  • 32+ steps: give the scheme, do not execute blindly, check/show, limit rights (SELECT only)35%
Your promptClaude ⌄