Let Claude Write SQL Queries Using Your Actual Database Schema
Writing complex SQL is easier when someone already knows your schema. Claude Code reads your migration files, model definitions, and schema dumps, so the queries it writes use your actual table and column names — not guesses.
I need a query that finds all users who placed an order in the last 30 days
but haven't logged in since. Check my migrations for the correct column names.
Claude reads your database migrations or ORM models, identifies the right tables and columns, and writes a query that works on the first try:
SELECT u.id, u.email, u.last_login_at
FROM users u
INNER JOIN orders o ON o.user_id = u.id
WHERE o.created_at >= NOW() - INTERVAL 30 DAY
AND (u.last_login_at IS NULL OR u.last_login_at < o.created_at)
GROUP BY u.id, u.email, u.last_login_at;
This works for all levels of query complexity:
# Aggregations
Show me monthly revenue by product category for the last year,
with a running total column
# Performance investigation
Write a query to find the slowest N+1 pattern —
which users trigger the most order lookups?
# Data migration
Write an UPDATE query to backfill the new 'full_name' column
from the existing 'first_name' and 'last_name' columns
# Reporting
Generate a query for a cohort retention table —
group users by signup month and show what percentage are still active each subsequent month
If you have a database MCP server connected, Claude can go further — running the query, checking the results, and iterating if something looks off. But even without one, reading your schema files is enough to get the structure right.
Describe the data, not the SQL — Claude knows your schema and writes queries that match it.
via Claude Code
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.