I've also been using ChatGPT to create SQL queries -- it's genuinely really, really great at this. I do have to say "I'm using PostgreSQL with DataGrip IDE" and feed it back any errors I get, because there are a lot of permutations for SQL, but it's a pretty seamless experience overall.
Here's an example of one that helped me a lot. This takes no time at all to get ChatGPT to generate by giving it anonymized snippets of table data saying:
"here's an example of the relevant tables, I want output that looks like this, what query does that?"
and it just works instantly.
ChatGPT did NOT format it this way, I changed the line breaks and indentation formatting to better fit viewing in Chrome/Safari. I also renamed tables/columns out of personal responsibility, to generic names which make it all the more confusing.
-- Existing Records
WITH search_table_A AS (
SELECT * FROM tableA
WHERE some_primary_key = :searchKey
),
search_table_B AS (
SELECT * FROM tableB
WHERE some_primary_key = :searchKey
),
combined_query AS (
SELECT COALESCE(search_table_A.our_UUID, search_table_B.our_UUID) AS our_UUID
FROM search_table_A
LEFT JOIN search_table_B ON search_table_A.some_primary_key = search_table_B.some_primary_key
)
SELECT tableC.*
FROM tableC
JOIN combined_query ON tableC.our_UUID = combined_query.our_UUID;
Thirded, my only complaint is that it helpfully strips out comments and occasionally forgets which SQL variant I’m using. I have found it helpful to feed it back my query after I’ve implemented the recommended changes and made my tweaks, along with something like, “this is what I did, can you help me improve the section starting with `SELECT * FROM product`… the context seems to improve it’s subsequent recommendations.
Maybe you're using the paid version, or I suck too much at prompting, but so far I wasn't able to rely on the generated SQL and not for lack of trying. Workable queries, yes, but never doing what I intended...
I only just got access to the Plus version. But for the last month or so I've been using the free version with absolutely no problems in the scripts it generates.
I’m curious what it saves you. I’ve done the same for a couple months, but found its results too unreliable to be of any use to me, in terms of time savings.
Here's an example of one that helped me a lot. This takes no time at all to get ChatGPT to generate by giving it anonymized snippets of table data saying:
"here's an example of the relevant tables, I want output that looks like this, what query does that?"
and it just works instantly.
ChatGPT did NOT format it this way, I changed the line breaks and indentation formatting to better fit viewing in Chrome/Safari. I also renamed tables/columns out of personal responsibility, to generic names which make it all the more confusing.