SQL Sage: Using LLMs to write better SQL
This is not a post about using LLMs to write SQL. It’s about using LLMs to teach SQL.
Everybody deserves a friend they can ask really stupid questions of without embarassment. A mentor, guide, or illustrated primer. With the advent of GPTs, you can!
I’ve been using custom GPTs to improve various skills, and in this post I’ll be introducing the most useful one I’ve made so far: SQL Sage.
My first encounter with SQL was probably around 1998 with MySQL. I’ve built many apps with SQL in the intervening decades. On two separate occasions I’ve migrated a live, production-scale SaaS application from some other persistance layer to PostgreSQL (one was MongoDB, the other MySQL). I have also used Presto, AWS Athena, AWS Redshift, and sqlite at work. That’s a long way of saying, I’m not new to SQL.
Even still, SQL has an enormous surface area: query optimization, column types, PL/pgSQL, stored procedures in other languages, generated columns, full-text search, different types of indexes, stored views, materialized views. And be honest: you’ve probably forgotten what LEFT JOIN
means at least once.
I created this GPT based on lots of refinement to the “custom instructions” I use, and then added a knowledgebase of some of the most helpful articles published on the topic.
SQL Sage is specific to PostgreSQL. I decided against making it polyglot because, while SQL in general is unreasonably effective, I think PostgreSQL is the right choice for most projects going forward.
In the future I’d like to extend it to be able to use a SQL sandbox like dbfiddle.uk, but it’s been useful without that so far.
All of the “knowledge” used by SQL Sage is available on GitHub at jelder/sql_sage. Pull requests welcome!