Collaborative data workspace combining SQL, Python, notebooks and dashboards, with AI that writes queries and code from natural language.
Tag
#SQL
SQL on The Latent Daily: 1 tool and 3 prompts. News, explainers and guides on SQL, each linked to its sources.
Tools tagged #SQL
Prompts tagged #SQL
Cohort retention analysis plan with SQL or pandas
You are a product analyst. Help me plan and build a cohort retention analysis. Product and business model: [e.g. "subscription meal-kit app", "B2B SaaS with monthly plans", "free mobile game"] Question I want to answer: [e.g. "did users who signed up after the new onboarding retain better?"] Data available (tables and key columns): """ [e.g. users(user_id, signup_at, country, plan), events(user_id, event_name, event_at)] """ Tool: [SQL DIALECT, OR "Python pandas"] Date range: [START AND END] Steps: 1. Propose the cohort definition (what event and date puts a user in a cohort) and explain alternatives. 2. Propose the "retained" definition: which activity counts, and whether retention is classic (active in period N) or rolling (active in period N or later). Recommend one for my question. 3. Choose the time grain (day, week, month) and justify it from how often a healthy user should return. 4. Explain how to handle the most recent cohorts that have not had time to reach later periods, time zones, and users with no activity. 5. Write the query or code that outputs a cohort table: cohort, cohort size, and retention % for periods 0..N. 6. Explain how to read the table, what patterns to look for, and which comparisons would be misleading. 7. List checks to validate the result (e.g. cohort sizes match total signups).
Works with: ChatGPT, Claude, Gemini, Any LLM
Database schema design review before you migrate
You are a senior database engineer. Review the schema below before it is created in production. Database: [e.g. "PostgreSQL 16", "MySQL 8", "SQLite", "SQL Server"] What the application does: [ONE-PARAGRAPH DESCRIPTION] Expected scale: [ROWS PER TABLE, WRITES PER SECOND, GROWTH] Most important queries: [LIST 3-8 QUERIES IN SQL OR PLAIN ENGLISH] Existing data to migrate: [YES/NO, AND FROM WHERE] Schema: """ [PASTE CREATE TABLE STATEMENTS OR AN ORM MODEL FILE] """ Review in this order and label each finding HIGH, MEDIUM or LOW: 1. Correctness: primary keys, foreign keys, NOT NULL, UNIQUE and CHECK constraints that the business rules require. 2. Modeling: normalization problems, repeated data, many-to-many relationships, soft deletes, history and auditing needs. 3. Data types: money, time zones, text length, enums, IDs. 4. Indexes: which of my queries each index serves; missing and redundant indexes. 5. Naming and consistency. 6. Migration and operations: locking risks, backfills, defaults on large tables. For each finding, give the reason and the corrected DDL. Finish with a revised full schema and a list of questions for me where the right answer depends on business rules you cannot see.
Works with: ChatGPT, Claude, Gemini, Any LLM
Write a SQL query from your schema and a plain-English question
You are a senior data analyst who writes correct, readable SQL. Database: [ENGINE AND VERSION, e.g. PostgreSQL 16, BigQuery, SQLite] Schema (only the tables that matter): ``` [PASTE CREATE TABLE STATEMENTS OR TABLE: COLUMN (TYPE) LISTS] ``` Notes about the data: [e.g. "orders.status can be 'paid', 'refunded', 'cancelled'"; "timestamps are UTC"] Question: [YOUR BUSINESS QUESTION IN PLAIN ENGLISH] Before writing SQL: 1. Restate the question as a precise definition (what counts, what time range, what grouping). 2. List any assumptions and ambiguities. If an ambiguity would change the answer a lot, ask me before continuing. Then: 3. Write one query using only the tables and columns in the schema. Use CTEs for readability and comment each step. 4. Explain how the query handles NULLs, duplicates and time zones. 5. Give 2 sanity checks I can run (e.g. a total that should match a known number, a row count). Never invent table or column names. If something needed is missing from the schema, say what is missing.
Works with: ChatGPT, Claude, Gemini, Any LLM