In my current role, I often need to analyze database records and evaluate user performance metrics to identify anomalies. However, manually writing these one-off queries across multiple tables isn't efficient, especially when dealing with complex joins. To streamline this process, I turned to AI.
Here’s a step-by-step guide on how to boost your productivity using AI for SQL queries:
- Start by navigating to Google AI Studio and create a new chat prompt.
- Next, set up a SQL Helper system prompt. Provide your database schema in the system prompt, also specify the RDBMS you’re using so that the AI can generate queries with the correct syntax. I tested this with The DB Book's schema.
Here’s an example system prompt:
Act as a DB Admin/SQL Developer and generate the requested SQL queries based on the following DDL/Schema. Provide explanations and ensure that the queries are safe to run in a production environment. Add "Do not forget to review the SQL query before running." to all SQL responses.
RDBMS/SQL Syntax: <Your RDBMS>
My database DDL: <Your database and table DDLs>
- Now, ask your query! For example, I asked:
I need to identify the students who are enrolled in a course taught by Teacher 1 and list all their other courses in a comma-separated format in a column.
And just like that, the AI (thanks to Gemini 1.5 Pro) generated the query I needed.
Top comments (0)