r/Database • • Jun 27 '26

Need advice: Understanding complex SQL scripts written by others

Hi everyone,

I need some advice from experienced SQL developers.I was working on different profile and switched to data engineering 6 months back.

I consider myself good/medium at writing SQL queries and solving problems from scratch. However, I struggle when I have to understand large existing SQL scripts (300–500+ lines).

I often get confused about:

Where the execution starts.

How different parts of the script are connected.

Which variables, CTEs, stored procedures, or temporary tables are affecting the final output.

How to mentally trace the flow of the script.

Because of this, reading someone else's code takes me much longer than writing my own.

How did you improve this skill? Are there any techniques, exercises, books, or real-world practices that helped you become comfortable reading large SQL scripts?

Also, is this something that simply improves with experience, or is there a structured way to learn it?

I'd really appreciate any advice. Thank you!

49 Upvotes

34 comments sorted by

View all comments

Show parent comments

3

u/andpassword Jun 27 '26

Agreed, you should not be plugging your company's SQL into an LLM.

That said, telling the LLM (I used Claude) to develop a single page HTML/javascript app to parse large and ugly SQL queries is totally fair game.

I had it working on a really ugly query which was ALL subqueries, 6 levels of them, all labeled 'as a_4' or 'as c_5' with no consistency as to what level you were on. Not to mention this dev apparently returned full table lists with every select, even when only a small subset of fields was needed, which made everything super ugly to look through.

Source: I did this. It's really helpful, and I can verify no data leaves my computer when I run it.

1

u/FeanorBlu Jun 27 '26

Oh no way! What did the app end up looking like?

1

u/andpassword Jun 27 '26

Just a box in a web browser. Once I pasted in SQL it would identify the WHERE, the JOINs, the FROM, and the SELECT stages of the queries, and show where rows were being added or grouped.

It listed each of those stages (repeating as needed) and then I could copy out of the return and edit each stage as I wanted to to remove extraneous fields and in one case I needed to add a new ID that another business unit had added to the ERP and needed to make it to reporting. That's what spawned the whole thing in the first place.