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.

75
Duration
39s
Input Tokens
980
Output Tokens
2499
Cost
$0.03
Context
Input
I am in the class with teacher Amanda Svensson, I can't find the link to the video for the last lesson. Can you provide me with it?
Expected output
SELECT l.video_url FROM lessons l JOIN courses c ON l.course_id = c.course_id JOIN users u ON c.instructor_id = u.user_id WHERE u.name = 'Amanda Svensson' AND l.order_index = (SELECT MAX(order_index) FROM lessons l2 WHERE l2.course_id = c.course_id);
Model output
SELECT c.course_id,
       c.title AS course_title,
       l.lesson_id,
       l.title AS lesson_title,
       l.video_url
FROM courses c
JOIN users u ON u.user_id = c.instructor_id
JOIN LATERAL (
    SELECT lesson_id, title, video_url, order_index
    FROM lessons
    WHERE course_id = c.course_id
      AND is_draft = FALSE
      AND video_url IS NOT NULL
    ORDER BY order_index DESC NULLS LAST, lesson_id DESC
    LIMIT 1
) l ON TRUE
WHERE u.name = 'Amanda Svensson'
  AND c.is_published = TRUE;