# Chapter 7: Writing Queries ## The Problem You need to answer a complex question: "What's the average order value for customers who signed up in 2024, segmented by their first purchase category?" This goes beyond simple exploration. You need a real SQL query—and you don't know SQL. ## The Key Idea Core concept AI writes SQL queries from plain English descriptions. You review the results, not the syntax. SQL is the language of data. It's powerful but technical. AI handles the technical part. You focus on asking the right questions and interpreting the answers. ## Your First Query Ask AI to answer a business question: ``` > "What was our total revenue last month?" ``` AI writes and runs the query: ```sql select sum(amount) as total_revenue from events.purchases where purchased_at >= date '2024-12-01' and purchased_at < date '2025-01-01' ``` And returns the result: ``` Total revenue for December 2024: $1,247,893.45 ``` You didn't write the SQL. You asked a question and got an answer. ## More Complex Questions AI handles joins, aggregations, and filters: ``` > "Show me the top 10 customers by total spend this year" ``` ```sql select c.customer_id, c.email, sum(p.amount) as total_spend from marketing.customers c join events.purchases p on c.customer_id = p.customer_id where p.purchased_at >= date '2024-01-01' group by c.customer_id, c.email order by total_spend desc limit 10 ``` ``` | customer_id | email | total_spend | |-------------|----------------------|-------------| | C-44821 | sarah@example.com | $12,847.00 | | C-91023 | mike@example.com | $11,234.50 | ... ``` ## Understanding What AI Wrote You don't need to write SQL, but understanding the basics helps you verify results: | SQL Keyword | What It Does | Marketing Translation | | --- | --- | --- | | `select` | Choose columns | "Show me these fields" | | `from` | Choose table | "Look in this data" | | `where` | Filter rows | "Only include if..." | | `join` | Combine tables | "Match with related data" | | `group by` | Aggregate | "Summarize by category" | | `order by` | Sort | "Rank by this value" | | `limit` | Cap results | "Show top N" | When AI shows you a query, scan for these keywords to understand the logic. ## Iterating on Queries Results don't look right? Refine your request: ``` > "That includes returns. Exclude orders with negative amounts." ``` AI modifies the query: ```sql select sum(amount) as total_revenue from events.purchases where purchased_at >= date '2024-12-01' and purchased_at < date '2025-01-01' and amount > 0 -- Exclude returns ``` ## Saving Queries Useful queries can be saved for reuse: ``` > "Save this as 'monthly_revenue.sql'" ``` Later: ``` > "Run the monthly_revenue query for November" ``` AI adapts the saved query with new parameters. ## Query Patterns for Marketers Common questions you can ask: **Cohort Analysis:** ``` > "Compare revenue from customers who signed up in Q1 vs Q2" ``` **Funnel Analysis:** ``` > "Of customers who viewed the pricing page, how many signed up within 7 days?" ``` **Trend Analysis:** ``` > "Show me weekly purchase counts for the last 3 months" ``` **Segmentation:** ``` > "What's the average order value by customer country?" ``` ## When Queries Inform Segments Often, queries help you design segments: ``` > "What's the distribution of lifetime value?" ``` ``` | LTV Range | Customer Count | |---------------|----------------| | $0-100 | 45,230 | | $100-500 | 32,102 | | $500-1000 | 12,891 | | $1000+ | 8,443 | ``` Now you know: "high-value" might mean $500+ (top 22%) or $1000+ (top 9%). The query informed your segment definition. ## Mental Model: AI as Query Translator Think of AI as a translator between business language and database language: ``` You: "Who are my best customers?" AI thinks: "Best" probably means highest value. Let me check lifetime_value. AI writes: select ... order by lifetime_value desc ... AI returns: "Here are your top customers by lifetime value" ``` The translation happens automatically. Your job is to be clear about what "best" means to you. ## Pitfalls **"The numbers don't match the dashboard."** Dashboards often have specific filters or definitions. Ask AI: ``` > "Why might this number differ from the dashboard?" ``` **"The query is taking forever."** Large time ranges or unfiltered scans are slow. Narrow your criteria: ``` > "Just look at last week instead of last year" ``` **"I don't understand the results."** Ask AI to explain: ``` > "What does this result mean in plain English?" ``` ## What You've Learned - AI writes SQL from plain English questions - You review results, not syntax - Queries can be saved and reused - Query results inform segment design ## Next Step You can explore and query data. [Chapter 8](/treasure-code/book/08-first-segment) puts this knowledge to work—building your first audience segment with AI. *You've learned to ask questions. Next, you'll learn to build audiences.*