Back to TILs

Simpler SQL

How to make SQL more readable?

Is it even good question when it is closer to english than any other programming language?

My bet (and hope) is that SQL will be more common knowledge as spreadsheet formulas are.

It's expressive and quite powerful and get works done. I would say that it is much more easier to write than it is to read.

Composing queries in your head will make sense to you as you go step by step. Reading others larger SQL statements will be challenging as you try understand how the other person went to solve a problem.

Quite a lot of "Explain SQL AI" solutions popped up lately. And I get it that there is a need to break down the SQL to natural language. But the question is: Can you trust it? Nope.

So, we are not leaving the code anytime soon. And that's good thing! How to present SQL to novices or people new to the field?

If only there was a SQL to Spreadsheet formulas translation. But there isn't and it wouldn't make things easier based on my experience with large nested formulas from complicated spreadsheets.

Currently, my best solution is to break the SQL query into series of simpler steps and display it as a visual graph. Where you start at the left and work towards to the right. It's not perfect but it is deterministic and doesn't hide any details. The vision is to invite users to learn not isolate them. At the end of the day, who would take responsibility for outputs that they can't verify or trust?

Sql diagram

A nice side-effect of this approach is the ability to run query partially and understand the temporary results. It's not perfect but it's much better than big SQL snippet.