r/SQL • • Sep 09 '26

Discussion How much SQL do you still write manually?

With AI getting better at writing SQL, I’ve been wondering how much SQL analysts actually write from scratch these days.
Do you still write most of your queries manually, or do you use AI for things like joins, CTEs, debugging, optimization, etc.?
And has your use of AI actually changed how much SQL you need to know?

115 Upvotes

240 comments sorted by

View all comments

Show parent comment

1

u/kilopeter Sep 09 '26

What about your 200-line queries?

-6

u/Thadrea Data Science Manager Sep 09 '26

good data organizations don't have 200-line queries.

3

u/kilopeter Sep 09 '26

1

u/Thadrea Data Science Manager Sep 09 '26

I did say "good" data organizations. Yes, you can write really huge queries. Just because you can doesn't mean that you should.

Monitoring and limiting cognitive complexity to ensure your code is understandable and maintainable matters just as much in SQL as it does in any other programming language.

2

u/kilopeter Sep 09 '26

Lines of SQL aren't a super great metric of complexity, though I did use it as a rough proxy to learn more about how people are using AI for nontrivial queries. I do agree when practical to minimize LOC. But I don't think of 200 lines as "really huge" either!

0

u/Thadrea Data Science Manager Sep 10 '26 edited Sep 10 '26

I agree that LoC is imperfect, but honestly if you have such a long spaghetti of CTEs or joins in a single query that it's pushing 200 lines you should really look at refactoring that. Temp tables and views exist for a reason, and chances are a lot of the logic could be more simply built with window functions or better table design.

An overly convoluted query that runs is not a badge of honor if no one but you can figure out what it really does. Simplicity is the ultimate sophistication.

3

u/NawMean2016 Sep 09 '26

Alright I'll hear you out. Elaborate?

-1

u/Thadrea Data Science Manager Sep 09 '26

Elaborate on what, exactly? 200-line queries are typically an unmaintainable mess.

There are many situations where you need 200+ lines of logic to implement a specific set of requirements, yes. A well-managed codebase would separate those 200+ lines into smaller chunks that are individually understandable, versioned and diff-able.

Putting it all into one monster query is a serious anti-pattern and I'm not going to pretend that it isn't--Not only is it bad for the developers but it also bad for the database. Query planners necessarily get less and less effective at optimizing the tasks put to them the more and more convoluted a query gets.

3

u/geek180 Sep 09 '26

I agree with your comment on breaking out logic into multiple pieces, and avoiding extremely long complex queries, but 200 lines is really not that long, especially if you are using CTEs for everything.

0

u/Thadrea Data Science Manager Sep 10 '26

That's kinda the point... you shouldn't be using CTEs for everything.

You should be using temp tables in a lot of those cases and views in many of the others.

Long strings of nested CTEs are literal spaghetti code.

1

u/geek180 Sep 10 '26

I disagree, at least for what I work on. We are a dbt-heavy shop within snowflake and in that context, there’s little reason for the temp tables and chained CTEs are not only extremely easy for anyone to follow and read, but also perform practically the same as any other kind of query.

We also have 1000+ dbt models (mostly views) as it is, so I’m constantly assessing others’ work to find ways to consolidate logic into fewer models. Sometimes that means having a longer chain of CTEs in a single file.