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

21

u/BrupieD Sep 09 '26

I didn't say I don't think it can't absorb it, I simply don't want to write 5 lines of prompt to write a 4-line query.

9

u/j48u Sep 09 '26

This seems risky considering how anti AI the responses are so far, but that's a solved problem. If the AI has access to your table structures, you only need to give it the business context once. A few back and forth describing novel scenarios and you might end up writing 100 lines once, then a single sentence prompt anytime in the future will get you between one and infinity lines of SQL until the end of time. And yes, it will be optimized in any way you want it to be.

The only reason I bring this up is that it's honestly a disservice to people in this field to pretend AI hasn't gotten exponentially better every few months for the past few years. Most people who are not able to use AI properly are not going to have a job in 5 years, unfortunately.

4

u/DharmaPolice Sep 09 '26 edited Sep 09 '26

I think there's a feeling that if it's getting "exponentially better" every few months, is there even much point learning the foibles of today's models when in a few months there'll be a new set of foibles to work around / with when the "exponentially better" model will be out? (And if it really is improving at that rate and that somehow continues indefinitely - which feels unlikely by the way - then it's almost irrelevant what we use now since the tools we'll have in two/three years will make current tools look like banging rocks together.)

I know AI proponents like to get excited about how fast things change, but from an investment of time and energy you need some kind of platform stability. Would you learn a programming language or use a platform that changed dramatically every few months? I browse HackerNews which is fully onboard the hype train and some of the discussions there almost seem like self-parody - "I have this problem, I'm using XYZ which came out three months ago" and someone will unironically reply "Why are you using such an old version? Everyone's moved onto ABC which came out last week."

This is especially true when actually writing new queries is often a fairly small part of a lot of people's jobs. Don't get me wrong, I do use AI tools daily but a lot of the stuff I read is "We've made the easy part of your job even easier". Yeah, that's great and all but not really that helpful.

If you find writing SQL is difficult or time consuming then sure, I can imagine it must be a huge timesaver.

2

u/BplusHuman Sep 10 '26

I have good news for you... You never really needed to hit enter. It can all be one line of query

3

u/geek180 Sep 09 '26

It sounds like you’re still using a pre-agentic era AI setup from 1+ years ago. There’s a lot of ways to document your projects / repositories such that you never have to re-explain anything to the AI.

For me, I just have Claude read an existing ticket (also written by AI), or I just type out any instruction, then it spins up an agent in a new worktree and then just… does the work, usually by delegating individual tasks to sub-agents.

Some tasks just have to be handled manually, often admin duties, but for most work I’m just handing tasks to small swarms of agents and nudging them in the right direction as-needed.

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.