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

128

u/BrupieD Sep 09 '26

AI requires way too much context to understand my goals from my company's db. By the time I've filled in enough of my requirements for AI to be successful, I can just write the SQL myself.

I do a lot of ad hoc query writing that is often not very complicated. I've already internalized enough table and appropriate join information about those tables. SQL seems like the least helpful thing AI can do for me.

10

u/rbobby Sep 09 '26

SQL seems like the least helpful thing AI can do for me.

I concur, but buddy above mentioned narly string extractions and having done those, and passed at least one on after giving up, I am definitely gonna try next time.

7

u/kilopeter Sep 09 '26

Out of curiosity, what makes you think AI can't absorb your particular context and goals? What models have you actually tested this assumption with?

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.

→ More replies (2)

2

u/did-a-chuck Sep 09 '26

Are there any you would recommend? I am quite set in my ways and deeply skeptical that any model can fully "understand" my business context...but I want to remain open minded.

5

u/geek180 Sep 09 '26

You’d really be surprised how well current models can understand complex business operations and tech stacks. At this point, Claude probably has a better grasp of our whole tech system better than any one person.

1

u/rali3gh Sep 09 '26

I'm fairly sure an MCP server setup can serve as a 'translation layer', so to speak, giving an AI a retrievable understanding of data structure and the business-side employees contexrual understanding of what it all actually means day-to-day.

It takes time upfront to construct and configure, but would save time on the backend if you are asking the LLM to generate a lot of queries.

All that being said, I personally rely more heavily on AI for early investigation and analysis of my hypothesis and then less as I near the end goal of my work/project. So, your mileage may vary, as they say.

1

u/CalebAsimov Sep 09 '26

Yeah, it's literally so easy to write queries, like damn, way to take away the only thing that lets me rest my brain for a few minutes.

1

u/mooslar Sep 10 '26

In the codex app, you can select a folder/directory where it can read and write files. You can put a markdown file in the root of that folder and it will read it at the start of every chat.

I manually made a good size mark down, then told it to scan all of my saved queries and csv dumps and blow out a copy with what it thinks it can expand on. I read through everything it generated and made some change and now i have this huge corpus. I also add style preferences

I can treat it like a junior and ask it for basic queries and procs and it nails it most times.

1

u/Few-Spot1905 Sep 11 '26

sounds like bad AI usage tbh

if the ai can write correct SQL with the right context but has trouble repeatedly retrieving it by itself, this is exactly what a skill is for

you prompt it until it gets it right then ask "hey can you write me a skill so next time you can just get this right the first time?"

then the next time you ask it to query it loads the skill and just gets it right

I'm the grand scheme of things SQL isn't that hard and LLMs are pretty good at it

-5

u/elevarq Sep 09 '26

The challenge isn’t giving AI enough context. It’s giving it the right context, and knowing which conclusions can be established without business context.