0% found this document useful (0 votes)
6 views2 pages

SQL Query Building with AI Tools

Uploaded by

Mussu Shaik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

SQL Query Building with AI Tools

Uploaded by

Mussu Shaik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Section 6: AI Tools & LLMs (30 mins)

Objective:

We want to see how comfortable you are with the rapidly evolving AI landscape, especially tools
powered by LLMs like ChatGPT or Claude.

Option A: Prompt Engineering


Use ChatGPT or any LLM to assist with one of your earlier tasks (e.g., EDA, SQL query
building, Excel formula generation).

1. Share the exact prompt(s) you used.

2. Copy the response from the AI.

3. Reflect briefly (3–4 lines):

o What did the AI do well?

What did you have to modify or improve?

1. Share the exact prompt(s) you used.

Task: SQL Query Building

I wanted to extract customer data from a sales database, specifically finding the top 5 customers by total
purchase amount in 2023

promt.

Write a SQL query to find the top 5 customers with the highest total purchase amount in the year 2023.
Assume there’s a 'customers' table and a 'purchases' table. The 'purchases' table has 'customer_id',
'amount', and 'purchase_date'.

AI Response
SELECT

c.customer_id,

[Link],

SUM([Link]) AS total_spent

FROM

customers c

JOIN

purchases p ON c.customer_id = p.customer_id

WHERE

YEAR(p.purchase_date) = 2023

GROUP BY

c.customer_id, [Link]

ORDER BY

total_spent DESC

LIMIT 5;

Reflection

What did the AI do well?


The AI quickly understood the schema and gave a syntactically correct, optimized query. It used
the correct SQL clauses (JOIN, WHERE, GROUP BY, ORDER BY) and addressed the key
filtering and aggregation requirements.

What did you have to modify or improve?


I adjusted the date filtering slightly to be more portable across SQL dialects (e.g., using
DATE_TRUNC() in PostgreSQL or BETWEEN for better range control). Also, I made sure the
column names exactly matched our actual database schema.

Common questions

Powered by AI

Large Language Models like ChatGPT can efficiently assist in generating SQL queries by quickly understanding the provided schema and producing syntactically correct SQL statements using appropriate clauses such as JOIN, WHERE, GROUP BY, and ORDER BY . However, their limitations include the need for human adjustments to suit specific SQL dialects or to ensure column names match the actual database schema .

Using ChatGPT for prompt engineering influences outcomes positively by accelerating the query-writing process and providing a solid foundational structure . However, its generalized understanding necessitates modifications to ensure dialect compatibility and adherence to specific schema details, impacting effectiveness in varied database environments .

Crucial aspects included clearly defined task objectives, such as finding the top 5 customers by purchase amount, and providing schema details like table names and relevant columns . This information helped ChatGPT understand the context and necessary SQL operations, ensuring accurate query output .

The AI tool performed well by understanding the schema and providing a correct, optimized query using SQL syntax appropriately . Modifications needed included adjusting date filtering to be compatible across SQL dialects, like using DATE_TRUNC() in PostgreSQL or BETWEEN for better range control, and ensuring column names matched the actual database .

The AI improved efficiency by quickly generating a correct query structure, understanding SQL clauses necessary for the task . Limitations occurred in its handling of specific dialect features, requiring adjustments for better cross-compatibility and alignment with the existing database schema .

The AI's capability to generate optimized SQL queries is shown through its use of essential SQL clauses like JOIN, WHERE, GROUP BY, and ORDER BY, suggesting it understands the necessary steps for data aggregation and filtering . Deviations might occur in contexts where SQL dialect-specific features or precise schema details are not fully aligned with the AI's general output, necessitating manual corrections .

The benefits include efficient generation of syntactically correct and optimized queries, saving time in coding and debugging . Challenges involve handling dialect-specific features and schema nuances that AI might not fully accommodate, requiring human intervention for accuracy and completeness .

The steps involve providing a prompt containing the task and schema information. ChatGPT then generates the query, effectively using SQL syntax and recognizing schema requirements, such as using JOIN and aggregating data. However, adjustments might be necessary to align with specific SQL dialect features or correct schema elements not fully recognized by the AI .

Scenarios requiring further interpretation might include those involving complex custom logic, specific business rules, or integration with other data sources. Customization is necessary when the query must adhere strictly to a particular SQL dialect or when schema modifications, such as extended field recognition, are required .

Modifications included adjusting the date filtering to enhance portability, such as using DATE_TRUNC() in PostgreSQL or BETWEEN for more reliable range control . These modifications are important because they ensure the query runs correctly across different SQL dialects, maintaining functionality and accuracy in diverse database environments .

You might also like