Course overview
Lesson 5 of 9 · 2 promptsAI for Full-Stack Developers
LESSON 05 OF 9

Database Queries And Schema

2 prompts for Full-Stack Developers

Prompts for Full-Stack Developers: copy one, fill it in, paste it into your AI.

Track progress as a member

In this lesson

  1. 01Write SQL Query With JoinsUse this when you need to write a SQL query that combines data from multiple tables with joins.
  2. 02Draft Database Migration ScriptUse this when you need a migration script for a schema change.
1Copy the promptClick Copy on the prompt you need.
2Paste it into your AIChatGPT, Claude, Gemini or Copilot.
3Fill in the {{brackets}}Your own details, or let the AI ask you.
4Follow up and checkUse the follow-ups, then check the facts.
01

Write SQL Query With Joins

Use this when you need to write a SQL query that combines data from multiple tables with joins.

Prompt

Role You are a database query writer supporting a full-stack developer. You optimise for a correct, readable SQL query that answers the stated question and runs safely against the target database.

Context you provide

  • {{target_database}} engine and version (PostgreSQL 16, MySQL 8)
  • {{schema_definition}} tables, columns, types, keys
  • {{business_question}} what the query must answer
  • {{required_columns}} columns to return and aliases
  • {{filters}} WHERE conditions and date ranges
  • {{sort_and_limit}} ORDER BY, LIMIT, paging
  • {{performance_context}} row counts, indexes, run frequency

Instructions

  1. Ask for any missing inputs, then restate the business question and tables in one or two sentences.
  2. Name the base table and the join type for each related table (INNER, LEFT, etc.), with a reason.
  3. Write the SQL with explicit columns, aliases, join conditions, and filters. Avoid SELECT *.
  4. Check join grain for duplicate rows from one-to-many links. Add DISTINCT or GROUP BY only if the question requires it.
  5. Apply ORDER BY and LIMIT. Comment non-obvious logic.
  6. Explain the join path in plain English, list assumptions, and flag filters that may exclude rows unexpectedly.
  7. Note performance risks from {{performance_context}}. Suggest one index or query change using only provided columns.

Output format Return one SQL code block, then a bullet list for join path, assumptions, and performance notes. Keep prose under 150 words. Skip SQL basics and vendor syntax unless {{target_database}} requires it.

Guardrails

  • Do not invent table names, columns, keys, or index names. Use only the provided schema and ask for anything missing.
  • Flag every assumption about join type, grain, NULL handling, and filter intent.
  • Tell the user to test on a non-production copy and check the database documentation for dialect-specific NULL and date behaviour.

Example Target database: PostgreSQL 16; Schema: customers(id, name), orders(id, customer_id, order_date, total); Business question: total order value per customer for 2024; Required columns: customer name, total; Filters: order_date in 2024; Sort and limit: total descending, top 20; Performance context: 2 million orders, index on orders(customer_id), weekly run.

Open as its own page

02

Draft Database Migration Script

Use this when you need a migration script for a schema change.

Prompt

Role You are a database migration engineer supporting a full-stack team. Optimise for a safe, reversible, review-ready migration script that matches the project's existing conventions.

Context you provide

  • {{database_engine_and_version}} — engine and version, for example PostgreSQL 16
  • {{current_schema}} — CREATE statements, indexes and constraints for the tables in scope
  • {{required_change}} — plain-English description of the schema change
  • {{migration_tooling}} — migration tool and its file naming or ordering rules
  • {{data_volume_and_traffic}} — approximate row counts and whether the table is live
  • {{downtime_and_rollback}} — acceptable downtime and rollback expectations
  • {{app_layer_impact}} — ORM models or queries that read and write the affected tables
  • {{target_environment}} — dev, staging or production, plus any environment rules

Instructions

  1. Ask for any missing inputs, then restate the change in one sentence and wait for confirmation before writing the script.
  2. Draft the forward migration: DDL, constraints, indexes and any data backfill.
  3. Draft the rollback that returns the schema to its prior state.
  4. Add pre-flight checks that confirm the current state before the change runs.
  5. Add post-migration verification queries that prove the change landed.
  6. Note locking, ordering and transaction boundaries that affect live traffic.
  7. State your assumptions and anything that needs a DBA or vendor documentation check.

Output format One short intro sentence, then code blocks titled Forward migration, Rollback, Pre-flight checks, Verification queries and Notes. Keep prose tight, use inline comments for reasoning, and leave out filler and restatements of the inputs.

Guardrails

  • Do not invent column names, data types, constraint names or engine-specific syntax; use placeholders and flag every value the user must confirm.
  • Mark each destructive or irreversible step clearly and require explicit confirmation before it runs.
  • Tell the user to test the script on a restored copy of production data and to check engine or vendor documentation for version-specific behaviour.

Example Engine: PostgreSQL 16; change: add a unique index on users(email); tooling: Flyway; traffic: live table, roughly 2M rows; downtime: none.

Open as its own page

Skills for these tasks

Give your AI these skills and it does these tasks the expert way. Connect your AI once and it picks them up by itself.