AI and SQL - Hands On Challenge - Solution
Master AI-Assisted SQL Query Development and Optimization
This hands-on solution assumes you have ChatGPT access, a database schema screenshot, and basic SQL knowledge for verification purposes.
AI-Assisted SQL Development Process
Upload Schema Documentation
Provide ChatGPT with your database structure screenshot or documentation to establish context
Write Specific Prompts
Be explicit about SQL dialect (PostgreSQL, SQL Server, Oracle) and exact requirements
Iterate and Refine
Engage in back-and-forth conversation to clarify requirements and fix issues
Validate Results
Always test the generated query and verify results make logical sense
Generic vs Specific AI Prompts
| Feature | Generic Prompt | Specific Prompt |
|---|---|---|
| Request Style | Calculate sales by state for 2021 | Write a PostgreSQL query that calculates sales by state for 2021 |
| AI Response | Attempts data analysis directly | Generates SQL query code |
| Outcome | May not match your needs | Provides exactly what you want |
ChatGPT may choose the easiest data relationship rather than the business-correct one. Always verify the logic matches your requirements.
Ship-To vs User Location Analysis
Database Query Results
Query Validation Checklist
Never assume AI-generated code works without testing
Check row counts, data ranges, and business logic
Ensure the query uses the right business relationships
Check that formulas like price times quantity are correct
Confirm the WHERE clauses match your requirements
When you need more complex analysis like year-over-year growth, you can iteratively build on existing queries by asking ChatGPT to add specific columns or calculations.
Common Query Patterns for Business Analysis
Revenue by Geography
Analyze sales performance across different states or regions. Requires joining user location data with sales transactions.
Year-over-Year Growth
Compare current period performance to previous periods. Often uses CTEs and window functions for complex calculations.
Customer Segmentation
Group customers by behavior or demographics. Involves aggregating transaction data with user characteristics.
Don't just assume it's going to get everything right. Be as specific as you can, and you'll get it closer to being in just kind of a one-shot query.
Complex calculations like percentage growth require NULLIF functions to prevent division by zero errors. Always understand why specific SQL functions are being used.
Learning SQL Through AI Assistance
Ask for Explanations
When you see unfamiliar functions or approaches, ask ChatGPT to explain them in detail
Understand the Logic
Don't just copy-paste queries; try to follow the reasoning behind each part of the code
Request Examples
Ask for specific examples of how functions like NULLIF work in different contexts
Build Incrementally
Start with simple queries and gradually add complexity as you understand each component
Using ChatGPT for SQL Development
ChatGPT can be a powerful ally for SQL development, but the responsibility for accuracy and validation always remains with you as the developer.
Key Takeaways