Developing strong SQL skills is one of the essential parts of the training as a Data School Consultant. Our SQL training combined classic lectures and demonstrations with hands-on exercises like the SQL Murder Mystery. To further deepen our skills, we're encouraged to work through LeetCode's SQL50 challenges in our own time. Besides tracking my solutions on on GitHub with LeetSync, I've found that using AI as a learning partner has been particularly valuable - especially when tackling tricky problems or exploring alternative solutions.
Niklas recently shared how he used ChatGPT as a Python tutor, and I've been applying similar principles to deepen my SQL learning. While I personally prefer Claude, the principles work with any AI assistant - the key is treating them as mentors who guide your learning rather than just answer machines.
Setting Up Your AI Mentor
I start with a carefully structured prompt that sets the stage for meaningful learning:
I am working through the SQL50 challenges on LeetCode to improve my SQL skills. Please act as my mentor with the following approach:
For each challenge I share, I'll provide:
1. The full problem description with table structures
- My current SQL query (whether it's a work in progress or a working solution)
- What I'm specifically looking to understand better (e.g., where I'm stuck, concepts I want to explore, potential optimizations)
2. Please help me by:
- Asking questions to identify gaps in my understanding
- Breaking down complex problems into smaller steps
- Providing hints that guide me toward solutions
- Suggesting alternative approaches and their trade-offs
- Highlighting relevant SQL concepts, patterns, and best practices
- Discussing performance considerations when relevant
Please avoid giving direct solutions until I've had the chance to discover them through our discussion. Focus on helping me understand the underlying concepts and patterns that will help me solve similar problems in the future.
To make these interactions even more effective, I use custom instructions (available in both ChatGPT and Claude Pro with projects) to let the AI know about my background in statistical analysis, Python programming, and research methods. Alternatively, you can add a section to the prompt to customize it further. For example, in my case I might use:
3. Draw parallels between SQL concepts and my existing knowledge of:
- Statistical analysis and research methodology
- Python data manipulation (e.g., comparing SQL operations to pandas)
- Data structures from psychological research
This helps ensure explanations build on existing knowledge rather than starting from scratch. For example, my AI mentor explained the difference between SQL Window Functions and Correlated Subqueries by comparing them to running an ANOVA vs. multiple t-tests. In another discussion, it compared SQL operations to analogue pandas functions in Python. These are concepts I am already familiar with, making it easier for me to grasp new SQL concepts.
The AI Mentor in Action
Let me show you how this works in practice with a SQL challenge I completed recently. In this case I had no problem getting to a working solution, but I was interested whether there might alternative approaches to the problem that I had not thought of or that required concepts I was unfamiliar with:
Alright so here is a challenge I would like to talk about
---
Table: `Customer`
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicates rows.
customer_id is not NULL.
product_key is a foreign key (reference column) to Product table.
Table: `Product`
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key (column with unique values) for this table.
Write a solution to report the customer ids from the `Customer` table that bought all the products in the `Product` table.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Customer table:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
+-------------+-------------+
Product table:
+-------------+
| product_key |
+-------------+
| 5 |
| 6 |
+-------------+
Output:
+-------------+
| customer_id |
+-------------+
| 1 |
| 3 |
+-------------+
Explanation:
The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.
---
My solution (which works) is this one:
---
-- PostgreSQL
SELECT
customer_id
FROM customer
GROUP BY customer_id
HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(*) FROM product)
---
But I am wondering if this is the most elegant solution or if there are alternative approaches and their advantages and trade-offs.
This interaction led me to discover how to solve the problem using correlated subqueries, with the AI mentor guiding my thought process through questions and hints. The discussion then broadened into a deeper examination of how correlated subqueries compare to other SQL operations in terms of their advantages and trade-offs.
Making the Most of AI Mentoring
I've found this approach particularly valuable for:
- Getting unstuck when documentation alone isn't enough to solve a challenging problem
- Deepening my understanding of solutions I found but don't fully grasp yet
- Exploring alternative approaches to working solutions and their trade-offs
- Connecting new SQL concepts to concepts I am already familiar with
Whether you're working on SQL, Python, or any other technical skill, the key is to treat AI not as a solution provider but as a mentor who guides you toward deeper understanding. With the right approach, AI can become more than just a tool; it becomes a personalized mentor adapting to your unique learning journey.