How to Use GPT-4 Code Interpreter - Write SQL Code

If you work in analytics, and have looked into what GPT-4 code interpreter can do, or searched on Twitter for the sentiment towards the tool, you might panic a bit...maybe a lot.

Yes, many are talking about they don't need analysts or data scientists anymore.

However, think of it this way.

The tool can serve as your "sidekick". It allows you to focus on higher-value tasks such as interpreting results, developing insights, and making strategic decisions based on data, instead of doing time-consuming boring tasks. You can be much more efficient!

I am going to share with you how I use GPT-4, specifically code interpreter, in my projects.

P.S. Use sample data instead of real data to prevent data-leak issues.


Problem

I am querying from a table similar to the one below.

The same user (by phone number) is created as duplicated entries sometimes due to data entry issues.

I want to dedupe it by keeping the entry with the email address, if any.

GPT-4 Code Interpreter

I uploaded the sample file and gave the instruction on how I want the table deduped.

Amazingly, GPT-4 code interpreter quickly gave me the solution, then explained to me what the code means, and it did work!!

WITH ranked AS (
  SELECT *,
         ROW_NUMBER() OVER(PARTITION BY phone 
         ORDER BY CASE WHEN email IS NOT NULL THEN 1 ELSE 2 END, id) as rn
  FROM your_table
)
SELECT *
FROM ranked
WHERE rn = 1;

I would have come to the conclusion anyway but the code interpreter just makes the process so much faster - especially on days you have a brain fart.

The important thing to note in this case though is GPT-4 code interpreter works if you have to have a mechanism to verify what it tells you. Because again, it's a tool to complement your skills as human judgment is always needed to ensure responsible decision-making.