Comparing SQL Tables with AI (and SQL)

Introduction

Recently, I was loading an updated file into a SQL table. Before proceeding, I wanted to compare it with the original SQL table to identify data differences. This can be time consuming, but I knew just who to turn to for help: my friend Chatty-G.

The Prompt:

Both tables had the same column structure, making the comparison straightforward. Using SQL Server Management Studio (SSMS), I right-clicked on my SQL table and selected “Top 1000 rows” to easily get and copy the column names from my table.

I then pasted the select statement from my first table, and the name of the second table into ChatGPT and explained what I wanted in 2 poorly worded sentences:

The Performance:

Then, within 30 seconds, ChatGPT returned the ~100 line comparison CTE below:

…

The Process:

I copied the code into SSMS and executed it. No errors!

The Postscript:

The CTE here is not particularly complex. However, ChatGPT generated error-free code faster than I could type it. And if I had typed it, there certainly would have been an error to two to debug. This task is a good candidate for AI because it is relatively straightforward to verify the accuracy of the answer provided. I can easily see this causing trouble if used to “solve” problems that are not easily verified.

There are higher level questions I could ask about the data. Is this the way I want to verify the changes? Maybe I should return mismatched values instead of a binary identifier. If I needed that, I could update the structure of the code myself or better prompt ChatGPT in that direction.

A last thought is that this was a two-step process, but I can see the compounding advantages coming as Copilot and other AI companions get baked into the core of IT and Data products.