SQL Generation

Turn plain-English questions into correct SQL against a schema. The skill behind analytics chat, natural-language dashboards, and 'just ask the database' interfaces.

100
Duration
30s
Input Tokens
1205
Output Tokens
257
Cost
$0.04
Context
Input
Add a foreign key from completions to enrollments, linking through user_id and course_id to ensure data consistency.
Expected output
ALTER TABLE completions ADD COLUMN course_id INT, ADD CONSTRAINT fk_completions_user_course FOREIGN KEY (user_id, course_id) REFERENCES enrollments(user_id, course_id);
Model output
ALTER TABLE completions ADD COLUMN course_id INT;

UPDATE completions c
SET course_id = l.course_id
FROM lessons l
WHERE c.lesson_id = l.lesson_id;

ALTER TABLE completions ALTER COLUMN course_id SET NOT NULL;

ALTER TABLE completions ADD CONSTRAINT fk_completions_enrollments 
FOREIGN KEY (user_id, course_id) 
REFERENCES enrollments(user_id, course_id);